MCPcopy Create free account
hub / github.com/Paper2Poster/Paper2Poster / ensure_under_limit_pil

Function ensure_under_limit_pil

utils/poster_eval_utils.py:783–805  ·  view source on GitHub ↗
(img, max_bytes: int = 10 * 1024 * 1024)

Source from the content-addressed store, hash-verified

781 return images
782
783def ensure_under_limit_pil(img, max_bytes: int = 10 * 1024 * 1024) -> Image.Image:
784 # Ensure RGB mode for JPEG compatibility
785 if img.mode in ("RGBA", "P"):
786 img = img.convert("RGB")
787
788 # Try saving at decreasing qualities until under the limit
789 for quality in (90, 80, 70, 60, 50):
790 buf = io.BytesIO()
791 img.save(buf, format="JPEG", quality=quality)
792 new_raw = buf.getvalue()
793 if len(new_raw) <= max_bytes:
794 return Image.open(io.BytesIO(new_raw))
795
796 # Fallback: resize by half and save at low quality
797 w, h = img.size
798 img_resized = img.resize((w // 2, h // 2), Image.LANCZOS)
799 buf = io.BytesIO()
800 img_resized.save(buf, format="JPEG", quality=50)
801 new_raw = buf.getvalue()
802 if len(new_raw) > max_bytes:
803 raise RuntimeError("Could not reduce image under size limit")
804
805 return Image.open(io.BytesIO(new_raw))
806
807def eval_qa_get_answer(poster_input, questions, answers, aspects, input_type, agent_config):
808 agent_name = f'answer_question_from_{input_type}'

Callers 2

Calls 2

convertMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected