Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -115,15 +115,34 @@ async def upload_file(file: UploadFile = File(...)):
|
|
| 115 |
img = Image.open(BytesIO(contents)).convert("RGB")
|
| 116 |
squared_img = fill_square_cropper(img)
|
| 117 |
|
| 118 |
-
# Save the squared image
|
| 119 |
output = BytesIO()
|
| 120 |
squared_img.save(output, format="JPEG")
|
| 121 |
output.seek(0)
|
| 122 |
|
| 123 |
-
#
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
return HTMLResponse(
|
| 126 |
-
content=f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
media_type="text/html"
|
| 128 |
)
|
| 129 |
except Exception as e:
|
|
|
|
| 115 |
img = Image.open(BytesIO(contents)).convert("RGB")
|
| 116 |
squared_img = fill_square_cropper(img)
|
| 117 |
|
| 118 |
+
# Save the squared image (original size)
|
| 119 |
output = BytesIO()
|
| 120 |
squared_img.save(output, format="JPEG")
|
| 121 |
output.seek(0)
|
| 122 |
|
| 123 |
+
# Encode the full-size image for download
|
| 124 |
+
full_size_encoded_img = base64.b64encode(output.getvalue()).decode("utf-8")
|
| 125 |
+
|
| 126 |
+
# Resize the image for display (512px by 512px)
|
| 127 |
+
display_img = squared_img.copy()
|
| 128 |
+
display_img.thumbnail((512, 512)) # Resize for display
|
| 129 |
+
display_output = BytesIO()
|
| 130 |
+
display_img.save(display_output, format="JPEG")
|
| 131 |
+
display_output.seek(0)
|
| 132 |
+
|
| 133 |
+
# Encode the resized display image
|
| 134 |
+
display_encoded_img = base64.b64encode(display_output.getvalue()).decode("utf-8")
|
| 135 |
+
|
| 136 |
+
# Return the HTML response
|
| 137 |
return HTMLResponse(
|
| 138 |
+
content=f"""
|
| 139 |
+
<h3>Image successfully squared!</h3>
|
| 140 |
+
<img src='data:image/jpeg;base64,{display_encoded_img}' width="512" height="512" />
|
| 141 |
+
<br/>
|
| 142 |
+
<a href="data:image/jpeg;base64,{full_size_encoded_img}" download="squared_image.jpg">
|
| 143 |
+
<button>Download Full-Size Image</button>
|
| 144 |
+
</a>
|
| 145 |
+
""",
|
| 146 |
media_type="text/html"
|
| 147 |
)
|
| 148 |
except Exception as e:
|