39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
"""Characterize image preparation (donor: prepare_image)."""
|
|
import base64
|
|
import io
|
|
|
|
from PIL import Image
|
|
|
|
import photo_analyzer as pa
|
|
from conftest import make_image_array
|
|
|
|
|
|
def _decode(b64: str) -> Image.Image:
|
|
return Image.open(io.BytesIO(base64.b64decode(b64)))
|
|
|
|
|
|
def test_prepare_image_small_passthrough_jpeg(img):
|
|
b64, mime = pa.prepare_image(img)
|
|
assert mime == "image/jpeg"
|
|
out = _decode(b64)
|
|
assert out.size == (800, 600), "under MAX_LONG_EDGE → no resize"
|
|
assert out.format == "JPEG"
|
|
|
|
|
|
def test_prepare_image_resizes_to_max_long_edge(tmp_path):
|
|
big = tmp_path / "big.jpg"
|
|
arr = make_image_array("fx-blocks-01")
|
|
Image.fromarray(arr).resize((4096, 3072)).save(big, quality=90)
|
|
b64, _ = pa.prepare_image(big)
|
|
out = _decode(b64)
|
|
assert max(out.size) == pa.MAX_LONG_EDGE
|
|
assert out.size == (2048, 1536), "aspect ratio preserved"
|
|
|
|
|
|
def test_prepare_image_converts_png_alpha_to_rgb_jpeg(tmp_path):
|
|
p = tmp_path / "alpha.png"
|
|
Image.new("RGBA", (100, 80), (255, 0, 0, 128)).save(p)
|
|
b64, mime = pa.prepare_image(p)
|
|
assert mime == "image/jpeg"
|
|
assert _decode(b64).mode == "RGB"
|