# Python 3 with Pillow installed. Use two 8-bit PNGs with the same dimensions: # python3 compare-png-alpha.txt original.png edited.png from argparse import ArgumentParser from pathlib import Path from PIL import Image, ImageChops parser = ArgumentParser(description="Count changed decoded alpha samples in two 8-bit PNGs.") parser.add_argument("original") parser.add_argument("edited") args = parser.parse_args() for filename in (args.original, args.edited): with Path(filename).open("rb") as stream: header = stream.read(25) if header[:8] != b"\x89PNG\r\n\x1a\n" or header[12:16] != b"IHDR": parser.error("Both inputs must be PNG files.") if header[24:25] != bytes([8]): parser.error("Use 8-bit PNG files.") with Image.open(args.original) as original, Image.open(args.edited) as edited: if original.format != "PNG" or edited.format != "PNG": parser.error("Both inputs must be PNG files.") if original.size != edited.size: parser.error("Both PNG files must have the same pixel dimensions.") a = original.convert("RGBA").getchannel("A") b = edited.convert("RGBA").getchannel("A") unchanged = ImageChops.difference(a, b).histogram()[0] print(f"Changed alpha samples: {a.width * a.height - unchanged}")