"""Reproducible synthetic fixtures and CPU-only codec oracle, never browser QA.""" import sys, json, hashlib from pathlib import Path from PIL import Image, ImageOps import numpy as np if sys.argv[1] == 'generate': root = Path(sys.argv[2]); root.mkdir(parents=True, exist_ok=True) rng = np.random.default_rng(20261007) width, height = 4032, 3024 pixels = rng.integers(0, 256, (height, width, 3), dtype=np.uint8) rgb = Image.fromarray(pixels) alpha = Image.new('L', (width, height), 0) alpha.paste(64, (width//2, 0, width, height//2)) alpha.paste(192, (0, height//2, width//2, height)) alpha.paste(255, (width//2, height//2, width, height)) rgba = rgb.copy(); rgba.putalpha(alpha) for name, image, orientation, options in [ ('phone-12mp-rotated.jpg', rgb, 6, {'quality':95}), ('phone-12mp-alpha-rotated.png', rgba, 8, {}), ('phone-12mp-alpha-mirrored.webp', rgba, 2, {'lossless':True}), ]: exif = Image.Exif(); exif[274] = orientation image.save(root / name, exif=exif, **options) # Large pixel count with tiny encoded size exercises the pixel resource guard. Image.new('RGBA', (8000,6000), (19,101,224,128)).save(root/'phone-48mp-alpha.png') print(json.dumps({'directory':str(root),'files':[p.name for p in sorted(root.iterdir())]})) else: path = Path(sys.argv[2]) with Image.open(path) as source: source_width, source_height = source.size orientation = source.getexif().get(274, 1) oriented = ImageOps.exif_transpose(source).convert('RGBA') if len(sys.argv) > 3: oriented = oriented.resize((int(sys.argv[3]), int(sys.argv[4])), Image.Resampling.LANCZOS) corners = [oriented.getpixel(point) for point in [(0,0),(oriented.width-1,0),(0,oriented.height-1),(oriented.width-1,oriented.height-1)]] # Verify transparent/partial-alpha values survive a local lossless PNG adapter. import io encoded = io.BytesIO(); oriented.save(encoded, format='PNG') decoded = Image.open(io.BytesIO(encoded.getvalue())).convert('RGBA') assert decoded.tobytes() == oriented.tobytes() print(json.dumps({'width':oriented.width,'height':oriented.height,'sourceWidth':source_width,'sourceHeight':source_height,'orientation':orientation,'corners':corners,'alphaExtrema':oriented.getchannel('A').getextrema(),'sourceBytes':path.stat().st_size,'workingPngBytes':len(encoded.getvalue()),'sourceRGBABytes':source_width*source_height*4,'workingRGBABytes':oriented.width*oriented.height*4,'sha256':hashlib.sha256(path.read_bytes()).hexdigest(),'pngRoundTripExact':True}))