Files
mtusi2026lab5/student-tests/test_thumbnail.py
T
2026-11-16 09:28:38 +03:00

75 lines
3.5 KiB
Python

#!/usr/bin/env python3
"""Run against thumbnail_smoke; requires Pillow for independent JPEG inspection."""
import argparse
import subprocess
import struct
import zlib
import tempfile
from pathlib import Path
from PIL import Image, ImageOps
def run(binary, directory):
original = Image.new('RGB', (80, 40))
for color, box in [((250, 0, 0), (0, 0, 40, 20)),
((0, 250, 0), (40, 0, 80, 20)),
((0, 0, 250), (0, 20, 40, 40)),
((250, 250, 0), (40, 20, 80, 40))]:
original.paste(color, box)
def convert(source):
target = directory / (source.stem + '-thumbnail.jpg')
subprocess.run([binary, str(source), str(target)], check=True, capture_output=True)
result = Image.open(target)
assert result.format == 'JPEG'
assert not result.getexif()
return result
for suffix in ('jpg', 'png'):
for orientation in range(1, 9):
source = directory / f'orientation{orientation}.{suffix}'
exif = Image.Exif()
exif[274] = orientation
original.save(source, exif=exif, **({'quality': 100, 'subsampling': 0} if suffix == 'jpg' else {}))
expected = ImageOps.exif_transpose(Image.open(source)).convert('RGB')
actual = convert(source)
assert actual.size == expected.size, (source, actual.size, expected.size)
for x, y in [(actual.width // 4, actual.height // 4),
(3 * actual.width // 4, 3 * actual.height // 4)]:
assert max(abs(a - b) for a, b in zip(actual.getpixel((x, y)), expected.getpixel((x, y)))) < 15
for name, size, expected in [('standard', (320, 200), (256, 160)),
('round', (501, 333), (256, 170)),
('thin', (1, 300), (1, 256)),
('small', (17, 11), (17, 11))]:
source = directory / (name + '.png')
Image.new('RGB', size, (30, 110, 200)).save(source)
assert convert(source).size == expected
for alpha, expected in [(0, (255, 255, 255)), (128, (255, 127, 127))]:
source = directory / f'alpha{alpha}.png'
Image.new('RGBA', (10, 10), (255, 0, 0, alpha)).save(source)
actual = convert(source).getpixel((5, 5))
assert max(abs(a - b) for a, b in zip(actual, expected)) <= 3
source = directory / 'pixel-limit.png'
Image.new('RGB', (1, 1)).save(source)
data = bytearray(source.read_bytes())
data[16:24] = struct.pack('>II', 8000, 5001)
data[29:33] = struct.pack('>I', zlib.crc32(data[12:29]))
source.write_bytes(data)
rejected = subprocess.run([binary, str(source), str(directory / 'large.jpg')], capture_output=True)
assert rejected.returncode != 0 and b'image_too_large' in rejected.stderr
source = directory / 'invalid.png'
source.write_bytes(b'not an image')
assert subprocess.run([binary, str(source), str(directory / 'invalid.jpg')], capture_output=True).returncode != 0
print('PASS: JPEG dimensions, rounding, no upscale, all 8 EXIF orientations (JPEG/PNG), white alpha, pre-decode pixel limit, corrupt input')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--binary', default='build/thumbnail_smoke')
parser.add_argument('--workdir')
args = parser.parse_args()
binary = str(Path(args.binary).resolve())
with tempfile.TemporaryDirectory(dir=str(Path(args.workdir).resolve()) if args.workdir else None) as directory:
run(binary, Path(directory))