Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -111,7 +111,7 @@ def validate_image(image_content: bytes):
|
|
| 111 |
raise HTTPException(status_code=400, detail=f"Image validation error: {str(e)}")
|
| 112 |
|
| 113 |
def generate_gemini_image(images: List[Image.Image], prompt: str):
|
| 114 |
-
"""Generate or edit image using Gemini API and resize to 1024x1024 pixels"""
|
| 115 |
try:
|
| 116 |
contents = images + [prompt]
|
| 117 |
response = genai.GenerativeModel(GEMINI_MODEL).generate_content(contents)
|
|
@@ -126,15 +126,31 @@ def generate_gemini_image(images: List[Image.Image], prompt: str):
|
|
| 126 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 127 |
temp_path = tmp.name
|
| 128 |
generated_image = Image.open(BytesIO(part.inline_data.data))
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
# Verify dimensions
|
| 132 |
-
if
|
| 133 |
-
logger.error(f"
|
| 134 |
-
raise HTTPException(status_code=500, detail="Failed to
|
| 135 |
-
|
|
|
|
| 136 |
image_path = temp_path
|
| 137 |
-
logger.info(f"Generated and
|
| 138 |
|
| 139 |
if image_path:
|
| 140 |
return image_path, ""
|
|
|
|
| 111 |
raise HTTPException(status_code=400, detail=f"Image validation error: {str(e)}")
|
| 112 |
|
| 113 |
def generate_gemini_image(images: List[Image.Image], prompt: str):
|
| 114 |
+
"""Generate or edit image using Gemini API and resize to 1024x1024 pixels with aspect ratio preservation"""
|
| 115 |
try:
|
| 116 |
contents = images + [prompt]
|
| 117 |
response = genai.GenerativeModel(GEMINI_MODEL).generate_content(contents)
|
|
|
|
| 126 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 127 |
temp_path = tmp.name
|
| 128 |
generated_image = Image.open(BytesIO(part.inline_data.data))
|
| 129 |
+
|
| 130 |
+
# Preserve aspect ratio by padding to 1024x1024
|
| 131 |
+
original_width, original_height = generated_image.size
|
| 132 |
+
target_size = 1024
|
| 133 |
+
ratio = min(target_size / original_width, target_size / original_height)
|
| 134 |
+
new_width = int(original_width * ratio)
|
| 135 |
+
new_height = int(original_height * ratio)
|
| 136 |
+
|
| 137 |
+
# Resize while preserving aspect ratio
|
| 138 |
+
generated_image = generated_image.resize((new_width, new_height), Image.LANCZOS)
|
| 139 |
+
|
| 140 |
+
# Create a new 1024x1024 canvas with black background
|
| 141 |
+
new_image = Image.new('RGB', (target_size, target_size), (0, 0, 0))
|
| 142 |
+
# Paste the resized image in the center
|
| 143 |
+
offset = ((target_size - new_width) // 2, (target_size - new_height) // 2)
|
| 144 |
+
new_image.paste(generated_image, offset)
|
| 145 |
+
|
| 146 |
# Verify dimensions
|
| 147 |
+
if new_image.size != (1024, 1024):
|
| 148 |
+
logger.error(f"Output image dimensions {new_image.size} do not match expected 1024x1024")
|
| 149 |
+
raise HTTPException(status_code=500, detail="Failed to create 1024x1024 image")
|
| 150 |
+
|
| 151 |
+
new_image.save(temp_path, format="PNG")
|
| 152 |
image_path = temp_path
|
| 153 |
+
logger.info(f"Generated and padded image to 1024x1024 saved to: {temp_path} with prompt: {prompt}")
|
| 154 |
|
| 155 |
if image_path:
|
| 156 |
return image_path, ""
|