(img, max_bytes: int = 10 * 1024 * 1024)
| 781 | return images |
| 782 | |
| 783 | def 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 | |
| 807 | def eval_qa_get_answer(poster_input, questions, answers, aspects, input_type, agent_config): |
| 808 | agent_name = f'answer_question_from_{input_type}' |
no test coverage detected