toshas commited on
Commit
59d338e
·
1 Parent(s): cb0a9b1

fix rotation due to invalid exif check order

Browse files
gradio_dualvision/gradio_patches/image_utils.py CHANGED
@@ -29,6 +29,7 @@ def patched_preprocess_image(
29
  ) -> np.ndarray | PIL.Image.Image | str | None:
30
  if payload is None:
31
  return payload
 
32
  if payload.url and payload.url.startswith("data:"):
33
  if type == "pil":
34
  print("Preprocessing payload as PIL image")
@@ -39,9 +40,12 @@ def patched_preprocess_image(
39
  elif type == "filepath":
40
  print("Preprocessing payload as file path")
41
  return decode_base64_to_file(payload.url, cache_dir, format)
 
42
  if payload.path is None:
43
  raise ValueError("Image path is None.")
 
44
  file_path = Path(payload.path)
 
45
  if payload.orig_name:
46
  p = Path(payload.orig_name)
47
  name = p.stem
@@ -57,29 +61,35 @@ def patched_preprocess_image(
57
  return str(file_path)
58
  raise Error("SVG files are not supported as input images for this app.")
59
 
60
- # Check for heif or heic suffix, treat it as a special case and drop resolution immediately
61
- if suffix.lower() in ["heif", "heic"] and type == "filepath":
62
- im = PIL.Image.open(file_path).convert("RGB")
63
- scale = min(1.0, 1024 / max(im.width, im.height))
64
- im = im.resize((round(im.width * scale), round(im.height * scale)), PIL.Image.BILINEAR)
65
- file_path = processing_utils.save_pil_to_cache(im, cache_dir=cache_dir)
66
-
67
  im = PIL.Image.open(file_path)
68
- if type == "filepath" and (image_mode in [None, im.mode]):
69
- return str(file_path)
70
-
71
  exif = im.getexif()
72
- # 274 is the code for image rotation and 1 means "correct orientation"
 
 
 
73
  if exif.get(274, 1) != 1 and hasattr(ImageOps, "exif_transpose"):
 
74
  try:
75
  im = ImageOps.exif_transpose(im)
 
76
  except Exception:
77
  warnings.warn(f"Failed to transpose image {file_path} based on EXIF data.")
 
 
 
 
 
 
 
 
 
 
78
  if suffix.lower() != "gif" and im is not None:
79
  with warnings.catch_warnings():
80
  warnings.simplefilter("ignore")
81
  if image_mode is not None:
82
  im = im.convert(image_mode)
 
83
  return format_image(
84
  im,
85
  type=cast(Literal["numpy", "pil", "filepath"], type),
 
29
  ) -> np.ndarray | PIL.Image.Image | str | None:
30
  if payload is None:
31
  return payload
32
+
33
  if payload.url and payload.url.startswith("data:"):
34
  if type == "pil":
35
  print("Preprocessing payload as PIL image")
 
40
  elif type == "filepath":
41
  print("Preprocessing payload as file path")
42
  return decode_base64_to_file(payload.url, cache_dir, format)
43
+
44
  if payload.path is None:
45
  raise ValueError("Image path is None.")
46
+
47
  file_path = Path(payload.path)
48
+
49
  if payload.orig_name:
50
  p = Path(payload.orig_name)
51
  name = p.stem
 
61
  return str(file_path)
62
  raise Error("SVG files are not supported as input images for this app.")
63
 
 
 
 
 
 
 
 
64
  im = PIL.Image.open(file_path)
 
 
 
65
  exif = im.getexif()
66
+
67
+ if suffix.lower() in ["heif", "heic"] and type == "filepath":
68
+ im = im.convert("RGB")
69
+
70
  if exif.get(274, 1) != 1 and hasattr(ImageOps, "exif_transpose"):
71
+ # 274 is the code for image rotation and 1 means "correct orientation"
72
  try:
73
  im = ImageOps.exif_transpose(im)
74
+ Path(file_path).resolve().write_bytes(processing_utils.encode_pil_to_bytes(im, format="webp"))
75
  except Exception:
76
  warnings.warn(f"Failed to transpose image {file_path} based on EXIF data.")
77
+
78
+ max_dim = max(im.width, im.height)
79
+ if max_dim > 2048:
80
+ scale = min(1.0, 2048 / max_dim)
81
+ im = im.resize((round(im.width * scale), round(im.height * scale)), PIL.Image.BILINEAR)
82
+ Path(file_path).resolve().write_bytes(processing_utils.encode_pil_to_bytes(im, format="webp"))
83
+
84
+ if type == "filepath" and (image_mode in [None, im.mode]):
85
+ return str(file_path)
86
+
87
  if suffix.lower() != "gif" and im is not None:
88
  with warnings.catch_warnings():
89
  warnings.simplefilter("ignore")
90
  if image_mode is not None:
91
  im = im.convert(image_mode)
92
+
93
  return format_image(
94
  im,
95
  type=cast(Literal["numpy", "pil", "filepath"], type),