hansoneze commited on
Commit
e85935e
·
1 Parent(s): 30530ca

Switch to Gemini + update requirements

Browse files
app.py CHANGED
@@ -5,6 +5,7 @@ from fastapi import FastAPI, UploadFile, Form
5
  from fastapi.responses import FileResponse, JSONResponse
6
  from fastapi.middleware.cors import CORSMiddleware
7
  from PIL import Image
 
8
  from rembg import remove
9
  import google.generativeai as genai # Gemini SDK
10
  import gradio as gr
@@ -69,31 +70,54 @@ def replace_background(input_path, bg_choice):
69
  return result_path
70
 
71
  #def process_image(input_img, bg_choice, bg_upload, logo_upload, logo_transparency, logo_position, brand_color):
72
- def process_image(input_img, bg_choice, bg_upload, logo_upload, logo_transparency, logo_position):
73
  if input_img is None:
74
  return []
75
 
76
  temp_path = os.path.join(UPLOAD_DIR, f"upload_{int(time.time())}.png")
77
  input_img.save(temp_path)
78
 
 
79
  # Background selection
 
80
  if bg_upload is not None:
81
  bg = bg_upload.convert("RGBA").resize(input_img.size)
82
  else:
83
  bg_path = os.path.join(BG_DIR, bg_choice)
84
  bg = Image.open(bg_path).convert("RGBA").resize(input_img.size)
85
 
86
- # Foreground (removed background)
 
 
 
 
 
 
87
  fg = remove(input_img.convert("RGBA"))
88
 
 
 
 
 
 
 
 
 
 
 
89
  # Merge main photo with background
 
90
  result = Image.alpha_composite(bg, fg)
91
 
 
92
  # If logo uploaded
 
93
  if logo_upload is not None:
94
  logo = logo_upload.convert("RGBA")
95
  scale = result.width // 5
96
  logo.thumbnail((scale, scale))
 
 
97
  alpha = logo.split()[3].point(lambda p: p * (logo_transparency / 100))
98
  logo.putalpha(alpha)
99
 
@@ -105,14 +129,10 @@ def process_image(input_img, bg_choice, bg_upload, logo_upload, logo_transparenc
105
  "Center": ((result.width - logo.width) // 2, (result.height - logo.height) // 2),
106
  }
107
  result.paste(logo, pos_map[logo_position], logo)
108
-
109
- '''
110
- # Apply brand colour tint
111
- if brand_color:
112
- tint = Image.new("RGBA", result.size, brand_color + "20")
113
- result = Image.alpha_composite(result, tint)
114
- '''
115
 
 
 
 
116
  timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
117
  result_path = os.path.join(RESULTS_DIR, f"result_{timestamp}.png")
118
  result.save(result_path)
 
5
  from fastapi.responses import FileResponse, JSONResponse
6
  from fastapi.middleware.cors import CORSMiddleware
7
  from PIL import Image
8
+ from PIL import ImageFilter, ImageOps
9
  from rembg import remove
10
  import google.generativeai as genai # Gemini SDK
11
  import gradio as gr
 
70
  return result_path
71
 
72
  #def process_image(input_img, bg_choice, bg_upload, logo_upload, logo_transparency, logo_position, brand_color):
73
+ def process_image(input_img, bg_choice, bg_upload, logo_upload, logo_transparency, logo_position, blur_background=False):
74
  if input_img is None:
75
  return []
76
 
77
  temp_path = os.path.join(UPLOAD_DIR, f"upload_{int(time.time())}.png")
78
  input_img.save(temp_path)
79
 
80
+ # ---------------------------
81
  # Background selection
82
+ # ---------------------------
83
  if bg_upload is not None:
84
  bg = bg_upload.convert("RGBA").resize(input_img.size)
85
  else:
86
  bg_path = os.path.join(BG_DIR, bg_choice)
87
  bg = Image.open(bg_path).convert("RGBA").resize(input_img.size)
88
 
89
+ # Optionally blur background
90
+ if blur_background:
91
+ bg = bg.filter(ImageFilter.GaussianBlur(radius=3))
92
+
93
+ # ---------------------------
94
+ # Foreground (with feathered mask)
95
+ # ---------------------------
96
  fg = remove(input_img.convert("RGBA"))
97
 
98
+ # Extract mask from alpha channel
99
+ mask = fg.split()[3]
100
+
101
+ # Feather the edges (soft transition)
102
+ mask = mask.filter(ImageFilter.GaussianBlur(radius=2))
103
+
104
+ # Rebuild foreground with feathered alpha
105
+ fg.putalpha(mask)
106
+
107
+ # ---------------------------
108
  # Merge main photo with background
109
+ # ---------------------------
110
  result = Image.alpha_composite(bg, fg)
111
 
112
+ # ---------------------------
113
  # If logo uploaded
114
+ # ---------------------------
115
  if logo_upload is not None:
116
  logo = logo_upload.convert("RGBA")
117
  scale = result.width // 5
118
  logo.thumbnail((scale, scale))
119
+
120
+ # Apply transparency
121
  alpha = logo.split()[3].point(lambda p: p * (logo_transparency / 100))
122
  logo.putalpha(alpha)
123
 
 
129
  "Center": ((result.width - logo.width) // 2, (result.height - logo.height) // 2),
130
  }
131
  result.paste(logo, pos_map[logo_position], logo)
 
 
 
 
 
 
 
132
 
133
+ # ---------------------------
134
+ # Save final result
135
+ # ---------------------------
136
  timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
137
  result_path = os.path.join(RESULTS_DIR, f"result_{timestamp}.png")
138
  result.save(result_path)
results/{result_20250910134302.png → result_20250911010759.png} RENAMED
File without changes
results/result_20250911010857.png ADDED

Git LFS Details

  • SHA256: 3c88b3f6e72ff08862a9592184d033605de0fe01f58d0d72782db7acb4253605
  • Pointer size: 132 Bytes
  • Size of remote file: 1.7 MB
results/result_20250911015256.png ADDED

Git LFS Details

  • SHA256: abb119dae1971312ac1efa992e6338f77d996b1ebf7b40cae671f3c4cd5186af
  • Pointer size: 132 Bytes
  • Size of remote file: 9.61 MB
results/result_20250911193252.png ADDED

Git LFS Details

  • SHA256: e9d8a833498b171ef27b864bf913baae98fde1f4fd9ac4333ea4ed5ed5169576
  • Pointer size: 132 Bytes
  • Size of remote file: 1.32 MB
uploads/upload_1757549277.png ADDED

Git LFS Details

  • SHA256: 4e359bd22027703a760fb48f7cd772b8bec5230b4a0417718fabf49f309708d4
  • Pointer size: 132 Bytes
  • Size of remote file: 1.64 MB
uploads/upload_1757549335.png ADDED

Git LFS Details

  • SHA256: 4e359bd22027703a760fb48f7cd772b8bec5230b4a0417718fabf49f309708d4
  • Pointer size: 132 Bytes
  • Size of remote file: 1.64 MB
uploads/upload_1757551973.png ADDED

Git LFS Details

  • SHA256: 9581e57dcd3d5519c99014ff920e682c31add4775ed7c890837bc9ea2e1c1c08
  • Pointer size: 133 Bytes
  • Size of remote file: 11.9 MB
uploads/upload_1757615570.png ADDED

Git LFS Details

  • SHA256: 2f0ba43dc289b7febc996c0369d9f0b48c4c509dbb30cdf6bf23bd5b4a9f8d92
  • Pointer size: 131 Bytes
  • Size of remote file: 836 kB