Switch to Gemini + update requirements
Browse files- app.py +29 -9
- results/{result_20250910134302.png → result_20250911010759.png} +2 -2
- results/result_20250911010857.png +3 -0
- results/result_20250911015256.png +3 -0
- results/result_20250911193252.png +3 -0
- uploads/upload_1757549277.png +3 -0
- uploads/upload_1757549335.png +3 -0
- uploads/upload_1757551973.png +3 -0
- uploads/upload_1757615570.png +3 -0
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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
results/result_20250911015256.png
ADDED
|
Git LFS Details
|
results/result_20250911193252.png
ADDED
|
Git LFS Details
|
uploads/upload_1757549277.png
ADDED
|
Git LFS Details
|
uploads/upload_1757549335.png
ADDED
|
Git LFS Details
|
uploads/upload_1757551973.png
ADDED
|
Git LFS Details
|
uploads/upload_1757615570.png
ADDED
|
Git LFS Details
|