75 lines
3.1 KiB
Python
75 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Check image encoders, geometry, EXIF orientation and alpha on local fixtures."""
|
|
from pathlib import Path
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import zlib
|
|
|
|
from PIL import Image
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
FIXTURE = ROOT / 'tests/fixtures/sample.png'
|
|
|
|
|
|
def render(directory, version):
|
|
subprocess.run([sys.executable, '-m', 'src.render_image', str(directory), str(version)],
|
|
cwd=ROOT, check=True)
|
|
outputs = {}
|
|
for variant, format_name in [('thumbnail', 'JPEG'), ('crop', 'JPEG'), ('optimized', 'WEBP')]:
|
|
with Image.open(directory / variant) as image:
|
|
image.load()
|
|
assert image.format == format_name
|
|
assert not image.getexif()
|
|
outputs[variant] = (image.size, (directory / variant).read_bytes())
|
|
return outputs
|
|
|
|
|
|
def validate(path, mime):
|
|
return subprocess.run([sys.executable, '-m', 'src.image_check', str(path), mime],
|
|
cwd=ROOT, timeout=10, check=False).returncode
|
|
|
|
|
|
def main():
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
directory = Path(temp)
|
|
original = directory / 'original'
|
|
data = FIXTURE.read_bytes()
|
|
original.write_bytes(data)
|
|
assert validate(original, 'image/png') == 0
|
|
assert validate(original, 'image/jpeg') == 15
|
|
original.write_bytes(b'corrupt')
|
|
assert validate(original, 'image/png') == 15
|
|
# Rewrite only IHDR, with a valid CRC, to exercise the pre-decode limit.
|
|
header = data[12:16] + struct.pack('>II', 10000, 10000) + data[24:29]
|
|
original.write_bytes(data[:12] + header + struct.pack('>I', zlib.crc32(header)) + data[33:])
|
|
assert validate(original, 'image/png') == 13
|
|
original.write_bytes(data)
|
|
print('PASS: PNG validation, MIME mismatch, corrupt input and pixel cap before decoding')
|
|
first = render(directory, 1)
|
|
assert first == render(directory, 1)
|
|
second = render(directory, 2)
|
|
assert first.keys() == second.keys()
|
|
for variant, shape in [('thumbnail', (256, 160)), ('crop', (128, 128)), ('optimized', (320, 200))]:
|
|
assert first[variant][0] == second[variant][0] == shape
|
|
assert first[variant][1] != second[variant][1]
|
|
assert original.read_bytes() == data
|
|
print('PASS: fixture geometry, original preserved, deterministic v1, different v2')
|
|
transparent = Image.new('RGBA', (7, 3), (255, 0, 0, 0))
|
|
transparent.save(original, format='PNG')
|
|
alpha = render(directory, 1)
|
|
assert alpha['thumbnail'][0] == (7, 3)
|
|
with Image.open(directory / 'thumbnail') as thumbnail:
|
|
assert thumbnail.getpixel((0, 0)) == (255, 255, 255)
|
|
rotated = Image.new('RGB', (17, 31), 'red')
|
|
exif = rotated.getexif()
|
|
exif[274] = 6
|
|
rotated.save(original, format='JPEG', exif=exif)
|
|
assert render(directory, 1)['thumbnail'][0] == (31, 17)
|
|
print('PASS: alpha on white, no upscaling, EXIF orientation applied and metadata removed')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|