Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -162,9 +162,25 @@ def askimage():
|
|
| 162 |
return jsonify({"error": "No image provided"}), 400
|
| 163 |
|
| 164 |
try:
|
| 165 |
-
# Read image bytes
|
| 166 |
image_bytes = image_file.read()
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
if not image_url:
|
| 170 |
return jsonify({"error": "Failed to upload image to host"}), 500
|
|
|
|
| 162 |
return jsonify({"error": "No image provided"}), 400
|
| 163 |
|
| 164 |
try:
|
| 165 |
+
# Read image bytes
|
| 166 |
image_bytes = image_file.read()
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
# Load image from bytes
|
| 170 |
+
img = Image.open(io.BytesIO(image_bytes))
|
| 171 |
+
|
| 172 |
+
# Resize if larger than 1024x1024 to reduce size (adjust as needed)
|
| 173 |
+
max_size = 1024
|
| 174 |
+
if img.size[0] > max_size or img.size[1] > max_size:
|
| 175 |
+
img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
| 176 |
+
|
| 177 |
+
# Save compressed version (quality 85 for balance of speed/size)
|
| 178 |
+
output = io.BytesIO()
|
| 179 |
+
img.save(output, format='JPEG', quality=85, optimize=True)
|
| 180 |
+
compressed_bytes = output.getvalue()
|
| 181 |
+
|
| 182 |
+
# Use compressed bytes for upload
|
| 183 |
+
image_url = upload_image_to_imgbb(compressed_bytes, image_file.filename)
|
| 184 |
|
| 185 |
if not image_url:
|
| 186 |
return jsonify({"error": "Failed to upload image to host"}), 500
|