forked from mtusi2026/mtusi2026lab5
lab5
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Independent inspection of crop/WebP through variants_smoke (requires Pillow)."""
|
||||
import argparse
|
||||
import json
|
||||
import subprocess
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from PIL import Image, ImageOps
|
||||
|
||||
|
||||
def run(binary, directory):
|
||||
def convert(source):
|
||||
output = directory / (source.stem + '-' + source.suffix[1:])
|
||||
command = subprocess.run([binary, str(source), str(output)], check=True, capture_output=True)
|
||||
manifest = json.loads(command.stdout)
|
||||
result = {}
|
||||
for name, extension, fmt in [('thumbnail', 'jpg', 'JPEG'), ('crop', 'jpg', 'JPEG'), ('optimized', 'webp', 'WEBP')]:
|
||||
path = output / (name + '.' + extension)
|
||||
img = Image.open(path)
|
||||
assert img.format == fmt and not img.getexif()
|
||||
assert img.size == (manifest[name]['width'], manifest[name]['height'])
|
||||
assert path.stat().st_size == manifest[name]['size_bytes']
|
||||
result[name] = img
|
||||
return result
|
||||
|
||||
for size, optimized in [((320, 200), (320, 200)), ((2560, 1600), (1280, 800)),
|
||||
((17, 11), (17, 11)), ((1, 300), (1, 300)),
|
||||
((2001, 1333), (1280, 852))]:
|
||||
source = directory / f'fit{size[0]}x{size[1]}.png'
|
||||
Image.new('RGB', size, (40, 180, 80)).save(source)
|
||||
images = convert(source)
|
||||
assert images['crop'].size == (128, 128)
|
||||
assert images['optimized'].size == optimized
|
||||
# Cropping must discard both outer strips and keep the centered square.
|
||||
source = directory / 'center.png'
|
||||
image = Image.new('RGB', (200, 100), (255, 0, 0))
|
||||
image.paste((0, 220, 0), (50, 0, 150, 100))
|
||||
image.paste((0, 0, 255), (150, 0, 200, 100))
|
||||
image.save(source)
|
||||
cropped = convert(source)['crop']
|
||||
for point in [(5, 5), (64, 64), (122, 122)]:
|
||||
assert max(abs(a-b) for a, b in zip(cropped.getpixel(point), (0, 220, 0))) < 10
|
||||
|
||||
image = 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))]:
|
||||
image.paste(color, box)
|
||||
for suffix in ('jpg', 'png'):
|
||||
for orientation in range(1, 9):
|
||||
source = directory / f'orientation{orientation}.{suffix}'
|
||||
exif = Image.Exif(); exif[274] = orientation
|
||||
image.save(source, exif=exif, **({'quality': 100, 'subsampling': 0} if suffix == 'jpg' else {}))
|
||||
expected = ImageOps.exif_transpose(Image.open(source)).convert('RGB')
|
||||
result = convert(source)
|
||||
actual = result['optimized']
|
||||
assert 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)))) < 20
|
||||
source = directory / 'large-exif.png'
|
||||
exif = Image.Exif(); exif[274] = 6; exif[37510] = b'A' * 70000
|
||||
image.save(source, exif=exif)
|
||||
assert convert(source)['optimized'].size == (40, 80)
|
||||
for alpha, color 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)
|
||||
for image in convert(source).values():
|
||||
assert max(abs(a-b) for a,b in zip(image.getpixel((5,5)),color)) <= 6
|
||||
print('PASS: 3 variants, centered/upscaled crop, WebP fit/rounding/no upscale, EXIF stripped, orientations, alpha')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--binary', default='build/variants_smoke')
|
||||
parser.add_argument('--workdir')
|
||||
args = parser.parse_args()
|
||||
with tempfile.TemporaryDirectory(dir=str(Path(args.workdir).resolve()) if args.workdir else None) as directory:
|
||||
run(str(Path(args.binary).resolve()), Path(directory))
|
||||
Reference in New Issue
Block a user