Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -183,26 +183,30 @@ def application_page():
|
|
| 183 |
</body>
|
| 184 |
</html>
|
| 185 |
"""
|
| 186 |
-
|
| 187 |
@app.post("/upload/")
|
| 188 |
-
async def upload_file(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
try:
|
| 190 |
# Await file upload
|
| 191 |
contents = await file.read()
|
| 192 |
img = Image.open(BytesIO(contents)).convert("RGB")
|
| 193 |
-
# Apply padding based on user's choice
|
| 194 |
-
rectangled_img = cropper(img,padding_type)
|
| 195 |
|
| 196 |
-
#
|
|
|
|
|
|
|
|
|
|
| 197 |
output = BytesIO()
|
| 198 |
-
|
| 199 |
output.seek(0)
|
| 200 |
|
| 201 |
# Encode the full-size image for download
|
| 202 |
full_size_encoded_img = base64.b64encode(output.getvalue()).decode("utf-8")
|
| 203 |
|
| 204 |
-
# Resize the image for display (512px
|
| 205 |
-
display_img =
|
| 206 |
desired_width = 512
|
| 207 |
aspect_ratio = display_img.height / display_img.width
|
| 208 |
desired_height = int(desired_width * aspect_ratio)
|
|
@@ -224,21 +228,11 @@ async def upload_file(file: UploadFile = File(...), padding_type: str = Form(...
|
|
| 224 |
</head>
|
| 225 |
<body>
|
| 226 |
<img class="banner" src="/static/images/banner.jpg" alt="Banner" width="100%">
|
| 227 |
-
<h2>Image successfully
|
| 228 |
-
<!--<img src='data:image/jpeg;base64,display_encoded_img'/>-->
|
| 229 |
<img src='data:image/jpeg;base64,{display_encoded_img}' width="512"/>
|
| 230 |
-
<p><a href="data:image/jpeg;base64,{full_size_encoded_img}" download="
|
| 231 |
Download Full-Size Image</a></p>
|
| 232 |
-
<div style="margin: 0.75em 0;"><a href="https://www.buymeacoffee.com/Artgen" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a></div>
|
| 233 |
-
<div style="margin: 0.75em 0;">But what would really help me is a <strong>PRO subscription</strong> to Google Colab, Kaggle or Hugging Face. Many thanks.</div>
|
| 234 |
<p><a href="/">Back</a></p>
|
| 235 |
-
<div id="credit">Image credit
|
| 236 |
-
<a href="https://stock.adobe.com/es/contributor/212598146/UMAMI%20LAB" target="_blank">Adobe Stock User Umami Lab</a>
|
| 237 |
-
and
|
| 238 |
-
<a href="https://www.shutterstock.com/g/Idoia+Lerchundi?rid=430751957" target="_blank">Shutterstock User PhoArt101</a>.
|
| 239 |
-
</div>
|
| 240 |
-
<div style="margin: 0.75em 0;"><a href="https://www.buymeacoffee.com/Artgen" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a></div>
|
| 241 |
-
<div style="margin: 0.75em 0;">But what would really help me is a <strong>PRO subscription</strong> to Google Colab, Kaggle or Hugging Face. Many thanks.</div>
|
| 242 |
</body>
|
| 243 |
</html>
|
| 244 |
""",
|
|
@@ -248,5 +242,5 @@ async def upload_file(file: UploadFile = File(...), padding_type: str = Form(...
|
|
| 248 |
return HTMLResponse(content=f"<h3>An error occurred: {e}</h3>", media_type="text/html")
|
| 249 |
|
| 250 |
if __name__ == "__main__":
|
| 251 |
-
import uvicorn
|
| 252 |
-
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|
|
|
|
| 183 |
</body>
|
| 184 |
</html>
|
| 185 |
"""
|
|
|
|
| 186 |
@app.post("/upload/")
|
| 187 |
+
async def upload_file(
|
| 188 |
+
file: UploadFile = File(...),
|
| 189 |
+
target_width: int = Form(...),
|
| 190 |
+
target_height: int = Form(...)
|
| 191 |
+
):
|
| 192 |
try:
|
| 193 |
# Await file upload
|
| 194 |
contents = await file.read()
|
| 195 |
img = Image.open(BytesIO(contents)).convert("RGB")
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
# Crop to the user’s chosen dimensions (cropper clamps to max 1024px)
|
| 198 |
+
cropped_img = cropper(img, target_width, target_height)
|
| 199 |
+
|
| 200 |
+
# Save the cropped image (original size)
|
| 201 |
output = BytesIO()
|
| 202 |
+
cropped_img.save(output, format="JPEG")
|
| 203 |
output.seek(0)
|
| 204 |
|
| 205 |
# Encode the full-size image for download
|
| 206 |
full_size_encoded_img = base64.b64encode(output.getvalue()).decode("utf-8")
|
| 207 |
|
| 208 |
+
# Resize the image for display (512px wide, keep aspect ratio)
|
| 209 |
+
display_img = cropped_img.copy()
|
| 210 |
desired_width = 512
|
| 211 |
aspect_ratio = display_img.height / display_img.width
|
| 212 |
desired_height = int(desired_width * aspect_ratio)
|
|
|
|
| 228 |
</head>
|
| 229 |
<body>
|
| 230 |
<img class="banner" src="/static/images/banner.jpg" alt="Banner" width="100%">
|
| 231 |
+
<h2>Image successfully cropped!</h2>
|
|
|
|
| 232 |
<img src='data:image/jpeg;base64,{display_encoded_img}' width="512"/>
|
| 233 |
+
<p><a href="data:image/jpeg;base64,{full_size_encoded_img}" download="cropped_image.jpg">
|
| 234 |
Download Full-Size Image</a></p>
|
|
|
|
|
|
|
| 235 |
<p><a href="/">Back</a></p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
</body>
|
| 237 |
</html>
|
| 238 |
""",
|
|
|
|
| 242 |
return HTMLResponse(content=f"<h3>An error occurred: {e}</h3>", media_type="text/html")
|
| 243 |
|
| 244 |
if __name__ == "__main__":
|
| 245 |
+
import uvicorn, os
|
| 246 |
+
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|