HaanIlango commited on
Commit
820a9fe
·
verified ·
1 Parent(s): d890f0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -0
app.py CHANGED
@@ -22,6 +22,11 @@ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as RL
22
 
23
  import requests
24
 
 
 
 
 
 
25
  _msi_model = None
26
  LOGO_PATH = "logo.png"
27
  COUNTAPI_BASE = "https://countapi.mileshilliard.com/api/v1"
@@ -699,10 +704,24 @@ def flatten_transparency(pil_image):
699
  return pil_image.convert("RGB")
700
 
701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
702
  def analyze(pil_image, overlay_strength):
703
  if pil_image is None:
704
  return None, None, "Upload an image first.", gr.update()
705
  pil_rgb = flatten_transparency(pil_image)
 
706
  img_bgr = cv2.cvtColor(np.array(pil_rgb), cv2.COLOR_RGB2BGR)
707
  h, w = img_bgr.shape[:2]
708
 
 
22
 
23
  import requests
24
 
25
+ # Raise Pillow's decompression-bomb limit so large print-resolution files
26
+ # (banners, posters) aren't rejected outright. 400 million pixels covers
27
+ # very large print files while still blocking truly absurd inputs.
28
+ Image.MAX_IMAGE_PIXELS = 400_000_000
29
+
30
  _msi_model = None
31
  LOGO_PATH = "logo.png"
32
  COUNTAPI_BASE = "https://countapi.mileshilliard.com/api/v1"
 
704
  return pil_image.convert("RGB")
705
 
706
 
707
+ def downscale_if_huge(pil_image, max_dim=3000):
708
+ # Large print files (banners, posters) can be tens of megapixels.
709
+ # The heatmap and PDF don't need print resolution, and running
710
+ # saliency + OCR on a huge image would be slow on CPU-only hardware,
711
+ # so shrink anything oversized before analysis.
712
+ w, h = pil_image.size
713
+ if max(w, h) <= max_dim:
714
+ return pil_image
715
+ scale = max_dim / max(w, h)
716
+ new_size = (max(1, int(w * scale)), max(1, int(h * scale)))
717
+ return pil_image.resize(new_size, Image.LANCZOS)
718
+
719
+
720
  def analyze(pil_image, overlay_strength):
721
  if pil_image is None:
722
  return None, None, "Upload an image first.", gr.update()
723
  pil_rgb = flatten_transparency(pil_image)
724
+ pil_rgb = downscale_if_huge(pil_rgb)
725
  img_bgr = cv2.cvtColor(np.array(pil_rgb), cv2.COLOR_RGB2BGR)
726
  h, w = img_bgr.shape[:2]
727