Spaces:
Sleeping
Sleeping
Support all image formats - PNG, JPG, JPEG, GIF, WEBP, BMP, TIFF, SVG, etc.
Browse files
app.py
CHANGED
|
@@ -168,17 +168,31 @@ def gradio_image_to_base64(image_input) -> Optional[str]:
|
|
| 168 |
if pil_image is None:
|
| 169 |
return None
|
| 170 |
|
|
|
|
| 171 |
try:
|
| 172 |
-
#
|
| 173 |
-
pil_image.
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
try:
|
| 181 |
buffered = io.BytesIO()
|
|
|
|
|
|
|
| 182 |
pil_image.save(buffered, format="PNG")
|
| 183 |
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 184 |
return f"data:image/png;base64,{img_str}"
|
|
@@ -1209,9 +1223,9 @@ with gr.Blocks(
|
|
| 1209 |
lines=6
|
| 1210 |
)
|
| 1211 |
bulk_images = gr.File(
|
| 1212 |
-
label="Upload Images (Optional)",
|
| 1213 |
file_count="multiple",
|
| 1214 |
-
file_types=["
|
| 1215 |
height=100
|
| 1216 |
)
|
| 1217 |
btn_import = gr.Button(
|
|
|
|
| 168 |
if pil_image is None:
|
| 169 |
return None
|
| 170 |
|
| 171 |
+
# Convert image to RGB mode if necessary (some formats like RGBA, P, etc. need conversion)
|
| 172 |
try:
|
| 173 |
+
# Convert to RGB if image has transparency or is in a mode that might cause issues
|
| 174 |
+
if pil_image.mode in ('RGBA', 'LA', 'P'):
|
| 175 |
+
# Create a white background for transparent images
|
| 176 |
+
rgb_image = PILImage.new('RGB', pil_image.size, (255, 255, 255))
|
| 177 |
+
if pil_image.mode == 'P':
|
| 178 |
+
pil_image = pil_image.convert('RGBA')
|
| 179 |
+
rgb_image.paste(pil_image, mask=pil_image.split()[-1] if pil_image.mode in ('RGBA', 'LA') else None)
|
| 180 |
+
pil_image = rgb_image
|
| 181 |
+
elif pil_image.mode != 'RGB':
|
| 182 |
+
# Convert other modes to RGB
|
| 183 |
+
pil_image = pil_image.convert('RGB')
|
| 184 |
+
except Exception as convert_error:
|
| 185 |
+
logger.warning(f"Image mode conversion failed, trying to continue: {str(convert_error)}")
|
| 186 |
+
# Try to convert anyway
|
| 187 |
+
try:
|
| 188 |
+
pil_image = pil_image.convert('RGB')
|
| 189 |
+
except Exception:
|
| 190 |
+
pass
|
| 191 |
|
| 192 |
try:
|
| 193 |
buffered = io.BytesIO()
|
| 194 |
+
# Save as PNG (universal format) - PIL will handle conversion from any format
|
| 195 |
+
# PNG supports all color modes and is widely compatible
|
| 196 |
pil_image.save(buffered, format="PNG")
|
| 197 |
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 198 |
return f"data:image/png;base64,{img_str}"
|
|
|
|
| 1223 |
lines=6
|
| 1224 |
)
|
| 1225 |
bulk_images = gr.File(
|
| 1226 |
+
label="Upload Images (Optional) - All formats supported (PNG, JPG, JPEG, GIF, WEBP, BMP, TIFF, etc.)",
|
| 1227 |
file_count="multiple",
|
| 1228 |
+
file_types=[".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".tiff", ".tif", ".svg", ".ico", ".heic", ".heif"],
|
| 1229 |
height=100
|
| 1230 |
)
|
| 1231 |
btn_import = gr.Button(
|