39 行
1.2 KiB
Python
39 行
1.2 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from io import BytesIO
|
||
|
|
import unittest
|
||
|
|
|
||
|
|
from PIL import Image
|
||
|
|
|
||
|
|
from backend.services.material_service import (
|
||
|
|
VISION_INPUT_MAX_EDGE,
|
||
|
|
_optimized_vision_input,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
class MaterialVisionInputTests(unittest.TestCase):
|
||
|
|
def test_downscales_large_rgba_png_for_multimodal_request(self) -> None:
|
||
|
|
source = BytesIO()
|
||
|
|
Image.new("RGBA", (1209, 1901), (240, 180, 210, 170)).save(source, format="PNG")
|
||
|
|
|
||
|
|
optimized, content_type = _optimized_vision_input(source.getvalue(), "image/png")
|
||
|
|
|
||
|
|
self.assertEqual(content_type, "image/jpeg")
|
||
|
|
with Image.open(BytesIO(optimized)) as image:
|
||
|
|
self.assertLessEqual(max(image.size), VISION_INPUT_MAX_EDGE)
|
||
|
|
self.assertEqual(image.mode, "RGB")
|
||
|
|
|
||
|
|
def test_keeps_small_compact_image_unchanged(self) -> None:
|
||
|
|
source = BytesIO()
|
||
|
|
Image.new("RGB", (320, 320), (40, 80, 120)).save(source, format="JPEG", quality=85)
|
||
|
|
payload = source.getvalue()
|
||
|
|
|
||
|
|
optimized, content_type = _optimized_vision_input(payload, "image/jpeg")
|
||
|
|
|
||
|
|
self.assertEqual(optimized, payload)
|
||
|
|
self.assertEqual(content_type, "image/jpeg")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|