Ali Mohsin commited on
Commit
6204e53
·
1 Parent(s): dc16471
Files changed (1) hide show
  1. utils/image_utils.py +41 -9
utils/image_utils.py CHANGED
@@ -98,6 +98,9 @@ def load_image_from_file(
98
  """
99
  Load an image from a file path, supporting all major formats.
100
 
 
 
 
101
  Args:
102
  filepath: Path to the image file
103
  convert_to_rgb: Convert image to RGB mode (required for models)
@@ -115,18 +118,47 @@ def load_image_from_file(
115
  raise FileNotFoundError(f"Image file not found: {filepath}")
116
  return None
117
 
118
- # Check if it's a supported format
119
- if not is_image_file(path):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  if raise_on_error:
121
- raise ValueError(f"Unsupported image format: {filepath}")
122
- print(f"⚠️ Skipping unsupported format: {filepath}")
123
  return None
124
 
125
- # Open and load image
126
- with Image.open(path) as img:
127
- # Verify it's actually an image
128
- img.verify()
129
-
130
  # Re-open for actual use (verify() closes the file)
131
  img = Image.open(path)
132
 
 
98
  """
99
  Load an image from a file path, supporting all major formats.
100
 
101
+ This function attempts to load images even without file extensions (e.g., Gradio blob files).
102
+ It first checks the extension, but if that fails, it tries to detect the format from file content.
103
+
104
  Args:
105
  filepath: Path to the image file
106
  convert_to_rgb: Convert image to RGB mode (required for models)
 
118
  raise FileNotFoundError(f"Image file not found: {filepath}")
119
  return None
120
 
121
+ # Try to open the image - PIL can detect format from content even without extension
122
+ # This handles Gradio's "blob" files and other files without extensions
123
+ try:
124
+ # Open image to detect format (don't verify yet, just check format)
125
+ test_img = Image.open(path)
126
+ detected_format = test_img.format
127
+ test_img.close()
128
+
129
+ # If format detected, check if it's supported
130
+ if detected_format:
131
+ if detected_format.upper() not in SUPPORTED_FORMATS:
132
+ # Format detected but not in our supported list
133
+ if raise_on_error:
134
+ raise ValueError(f"Unsupported image format: {detected_format} (file: {filepath})")
135
+ print(f"⚠️ Skipping unsupported format: {detected_format} (file: {filepath})")
136
+ return None
137
+ else:
138
+ # No format detected - check extension as fallback
139
+ if not is_image_file(path):
140
+ if raise_on_error:
141
+ raise ValueError(f"Cannot identify image format and no valid extension: {filepath}")
142
+ print(f"⚠️ Cannot identify image format: {filepath}")
143
+ return None
144
+
145
+ # Verify it's actually a valid image
146
+ with Image.open(path) as img:
147
+ img.verify()
148
+
149
+ except UnidentifiedImageError:
150
+ # If PIL can't identify it, check extension as fallback
151
+ if not is_image_file(path):
152
+ if raise_on_error:
153
+ raise ValueError(f"Cannot identify image format and no valid extension: {filepath}")
154
+ print(f"⚠️ Cannot identify image format: {filepath}")
155
+ return None
156
+ # Extension suggests it's an image, but PIL can't read it - might be corrupted
157
  if raise_on_error:
158
+ raise ValueError(f"File has image extension but cannot be read: {filepath}")
159
+ print(f"⚠️ File has image extension but cannot be read: {filepath}")
160
  return None
161
 
 
 
 
 
 
162
  # Re-open for actual use (verify() closes the file)
163
  img = Image.open(path)
164