Render crop and optimized variants next to the thumbnail

The worker decodes the original once (JPEG draft down to the optimized
box, EXIF orientation applied in place, alpha composed on white) and
encodes three variants: the 256 px thumbnail, a centred 128x128 JPEG crop
that may upscale and a WebP fitted into 1280x1280 without upscaling,
quality 80, method 4. No EXIF, ICC or XMP is written. The byte limit is
checked before decoding. ready is set by the transaction that stores all
three rows, so a partial set is never published.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-29 03:00:11 +03:00
co-authored by Claude Opus 5.5
parent 045d631c12
commit 431b82fd43
9 changed files with 222 additions and 82 deletions
+72 -35
View File
@@ -3,11 +3,17 @@ from io import BytesIO
import pytest
from PIL import Image
from messenger.attachments.domain.errors import UnreadableImage
from messenger.attachments.infrastructure.images import render_variants
from messenger.attachments.application.ports import Rendition
from messenger.attachments.domain.errors import ImageTooDemanding, UnreadableImage
from messenger.attachments.infrastructure.images import RenderLimits, render_variants
from pictures import SAMPLE
LIMIT = 40_000_000
LIMITS = RenderLimits(max_bytes=10_485_760, max_pixels=40_000_000)
FORMATS = {
"thumbnail": ("image/jpeg", "jpg", "JPEG"),
"crop": ("image/jpeg", "jpg", "JPEG"),
"optimized": ("image/webp", "webp", "WEBP"),
}
def encoded(image: Image.Image, kind: str = "PNG", **options: object) -> bytes:
@@ -16,62 +22,88 @@ def encoded(image: Image.Image, kind: str = "PNG", **options: object) -> bytes:
return buffer.getvalue()
def thumbnail(data: bytes) -> Image.Image:
(rendition,) = render_variants(data, LIMIT)
assert (rendition.name, rendition.content_type, rendition.extension) == (
"thumbnail",
"image/jpeg",
"jpg",
)
image = Image.open(BytesIO(rendition.data))
assert image.format == "JPEG"
assert image.size == (rendition.width, rendition.height)
assert not image.getexif()
return image
def rendered(data: bytes) -> dict[str, Image.Image]:
renditions: list[Rendition] = render_variants(data, LIMITS)
assert [rendition.name for rendition in renditions] == ["thumbnail", "crop", "optimized"]
images = {}
for rendition in renditions:
content_type, extension, kind = FORMATS[rendition.name]
assert (rendition.content_type, rendition.extension) == (content_type, extension)
image = Image.open(BytesIO(rendition.data))
assert image.format == kind
assert image.size == (rendition.width, rendition.height)
assert not image.getexif()
assert not {"icc_profile", "exif", "xmp"} & set(image.info)
images[rendition.name] = image
return images
def test_fixture_gives_256_by_160() -> None:
assert thumbnail(SAMPLE).size == (256, 160)
def sizes(data: bytes) -> tuple[tuple[int, int], ...]:
images = rendered(data)
return tuple(images[name].size for name in ("thumbnail", "crop", "optimized"))
def test_fixture_gives_three_variants() -> None:
assert sizes(SAMPLE) == ((256, 160), (128, 128), (320, 200))
@pytest.mark.parametrize(
("mode", "size", "fitted"),
("mode", "size", "thumbnail", "optimized"),
[
("L", (17, 1000), (4, 256)),
("RGB", (64, 64), (64, 64)),
("RGB", (3000, 2), (256, 1)),
("I;16", (300, 200), (256, 170)),
("CMYK", (801, 601), (256, 192)),
("L", (17, 1000), (4, 256), (17, 1000)),
("RGB", (64, 64), (64, 64), (64, 64)),
("RGB", (3000, 2), (256, 1), (1280, 1)),
("I;16", (300, 200), (256, 170), (300, 200)),
("CMYK", (801, 601), (256, 192), (801, 601)),
("RGB", (4000, 3000), (256, 192), (1280, 960)),
],
)
def test_sizes_round_down_and_never_upscale(
mode: str, size: tuple[int, int], fitted: tuple[int, int]
def test_sizes_round_down_never_upscale_and_crop_is_square(
mode: str, size: tuple[int, int], thumbnail: tuple[int, int], optimized: tuple[int, int]
) -> None:
kind = "JPEG" if mode == "CMYK" else "PNG"
assert thumbnail(encoded(Image.new(mode, size), kind)).size == fitted
kind = "JPEG" if mode == "CMYK" or size == (4000, 3000) else "PNG"
assert sizes(encoded(Image.new(mode, size), kind)) == (thumbnail, (128, 128), optimized)
def test_crop_takes_the_centre() -> None:
image = Image.new("RGB", (300, 100), (255, 0, 0))
image.paste((0, 0, 255), (100, 0, 200, 100))
crop = rendered(encoded(image))["crop"].convert("RGB")
pixels = [crop.getpixel((x, 64)) for x in (2, 64, 125)]
assert all(isinstance(p, tuple) and p[0] < 60 and p[2] > 200 for p in pixels)
@pytest.mark.parametrize("mode", ["RGBA", "LA"])
def test_transparency_is_composed_on_white(mode: str) -> None:
transparent = Image.new(mode, (300, 300), (0,) * len(mode))
assert thumbnail(encoded(transparent)).convert("RGB").getpixel((5, 5)) == (255, 255, 255)
for image in rendered(encoded(transparent)).values():
assert image.convert("RGB").getpixel((5, 5)) == (255, 255, 255)
def test_palette_transparency_is_composed_on_white() -> None:
palette = Image.new("P", (200, 100))
image = thumbnail(encoded(palette, transparency=0))
image = rendered(encoded(palette, transparency=0))["thumbnail"]
assert image.size == (200, 100)
assert image.convert("RGB").getpixel((5, 5)) == (255, 255, 255)
@pytest.mark.parametrize(("orientation", "fitted"), [(6, (128, 256)), (3, (256, 128))])
@pytest.mark.parametrize(
("orientation", "thumbnail", "optimized"),
[(6, (128, 256), (200, 400)), (3, (256, 128), (400, 200))],
)
def test_exif_orientation_is_applied_before_sizing(
orientation: int, fitted: tuple[int, int]
orientation: int, thumbnail: tuple[int, int], optimized: tuple[int, int]
) -> None:
exif = Image.Exif()
exif[0x0112] = orientation
data = encoded(Image.new("RGB", (400, 200), (10, 200, 10)), "JPEG", exif=exif)
assert thumbnail(data).size == fitted
data = encoded(
Image.new("RGB", (400, 200), (10, 200, 10)), "JPEG", exif=exif, icc_profile=b"x" * 64
)
assert sizes(data) == (thumbnail, (128, 128), optimized)
def test_same_bytes_give_same_variants() -> None:
assert render_variants(SAMPLE, LIMITS) == render_variants(SAMPLE, LIMITS)
@pytest.mark.parametrize(
@@ -81,9 +113,14 @@ def test_exif_orientation_is_applied_before_sizing(
)
def test_undecodable_data_is_a_permanent_error(data: bytes) -> None:
with pytest.raises(UnreadableImage):
render_variants(data, LIMIT)
render_variants(data, LIMITS)
def test_pixel_limit_is_checked_before_decoding() -> None:
with pytest.raises(UnreadableImage):
render_variants(SAMPLE, 320 * 200 - 1)
render_variants(SAMPLE, RenderLimits(LIMITS.max_bytes, 320 * 200 - 1))
def test_byte_limit_is_checked_before_decoding() -> None:
with pytest.raises(ImageTooDemanding):
render_variants(SAMPLE, RenderLimits(len(SAMPLE) - 1, LIMITS.max_pixels))