Spaces:
Running
Running
handle MPO image format (iPhone JPEG with depth-map)
Browse files
llm.py
CHANGED
@@ -156,9 +156,10 @@ class LLM:
|
|
156 |
|
157 |
tokens = calculate_tokens(img.width, img.height)
|
158 |
long_edge = max(img.width, img.height)
|
|
|
159 |
|
160 |
# Check if the image already meets all requirements
|
161 |
-
if long_edge <= 1568 and tokens <= 1600 and len(image_data) <= 5 * 1024 * 1024:
|
162 |
return {
|
163 |
"format": original_format,
|
164 |
"source": {"bytes": image_data}
|
@@ -181,17 +182,16 @@ class LLM:
|
|
181 |
|
182 |
# Try to save in original format first
|
183 |
buffer = io.BytesIO()
|
184 |
-
img.save(buffer, format=
|
185 |
image_data = buffer.getvalue()
|
186 |
|
187 |
# If the image is still too large, switch to WebP and compress
|
188 |
if len(image_data) > 5 * 1024 * 1024:
|
189 |
-
format_to_use = "webp"
|
190 |
quality = 95
|
191 |
while len(image_data) > 5 * 1024 * 1024:
|
192 |
quality = max(int(quality * 0.9), 20)
|
193 |
buffer = io.BytesIO()
|
194 |
-
img.save(buffer, format=
|
195 |
image_data = buffer.getvalue()
|
196 |
if quality == 20:
|
197 |
# If we've reached quality 20 and it's still too large, resize
|
@@ -200,11 +200,9 @@ class LLM:
|
|
200 |
new_height = int(img.height * scale_factor)
|
201 |
img = img.resize((new_width, new_height), Image.LANCZOS)
|
202 |
quality = 95 # Reset quality for the resized image
|
203 |
-
else:
|
204 |
-
format_to_use = original_format
|
205 |
|
206 |
return {
|
207 |
-
"format":
|
208 |
"source": {"bytes": image_data}
|
209 |
}
|
210 |
|
|
|
156 |
|
157 |
tokens = calculate_tokens(img.width, img.height)
|
158 |
long_edge = max(img.width, img.height)
|
159 |
+
format_ok = original_format in ["jpg", "jpeg", "png", "webp"]
|
160 |
|
161 |
# Check if the image already meets all requirements
|
162 |
+
if format_ok and (long_edge <= 1568 and tokens <= 1600 and len(image_data) <= 5 * 1024 * 1024):
|
163 |
return {
|
164 |
"format": original_format,
|
165 |
"source": {"bytes": image_data}
|
|
|
182 |
|
183 |
# Try to save in original format first
|
184 |
buffer = io.BytesIO()
|
185 |
+
img.save(buffer, format="webp", quality=95)
|
186 |
image_data = buffer.getvalue()
|
187 |
|
188 |
# If the image is still too large, switch to WebP and compress
|
189 |
if len(image_data) > 5 * 1024 * 1024:
|
|
|
190 |
quality = 95
|
191 |
while len(image_data) > 5 * 1024 * 1024:
|
192 |
quality = max(int(quality * 0.9), 20)
|
193 |
buffer = io.BytesIO()
|
194 |
+
img.save(buffer, format="webp", quality=quality)
|
195 |
image_data = buffer.getvalue()
|
196 |
if quality == 20:
|
197 |
# If we've reached quality 20 and it's still too large, resize
|
|
|
200 |
new_height = int(img.height * scale_factor)
|
201 |
img = img.resize((new_width, new_height), Image.LANCZOS)
|
202 |
quality = 95 # Reset quality for the resized image
|
|
|
|
|
203 |
|
204 |
return {
|
205 |
+
"format": "webp",
|
206 |
"source": {"bytes": image_data}
|
207 |
}
|
208 |
|