Shafi99 commited on
Commit
4290242
·
verified ·
1 Parent(s): 5d77a3e

Accept both PIL and file-descriptor inputs

Browse files
Files changed (1) hide show
  1. app.py +13 -0
app.py CHANGED
@@ -147,9 +147,22 @@ def _run(image):
147
  return Image.fromarray((_restore(arr, device) * 255.0 + 0.5).astype(np.uint8))
148
 
149
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  def _check(image):
151
  if image is None:
152
  raise gr.Error("Choose a photo first.")
 
153
  if image.width * image.height > MAX_PIXELS:
154
  raise gr.Error("That photo is larger than 40 megapixels. Please resize it a little.")
155
  return image.convert("RGB")
 
147
  return Image.fromarray((_restore(arr, device) * 255.0 + 0.5).astype(np.uint8))
148
 
149
 
150
+ def _as_image(x):
151
+ """Accept what either caller sends: a PIL image from the page, or the file
152
+ descriptor dict that gr.api hands over for an uploaded file."""
153
+ if isinstance(x, Image.Image):
154
+ return x
155
+ if isinstance(x, dict):
156
+ x = x.get("path") or x.get("url")
157
+ if isinstance(x, (str, bytes, os.PathLike)):
158
+ return Image.open(x)
159
+ raise gr.Error("Could not read that image.")
160
+
161
+
162
  def _check(image):
163
  if image is None:
164
  raise gr.Error("Choose a photo first.")
165
+ image = _as_image(image)
166
  if image.width * image.height > MAX_PIXELS:
167
  raise gr.Error("That photo is larger than 40 megapixels. Please resize it a little.")
168
  return image.convert("RGB")