"""Check an exact pixel match within a protected rectangle in opaque PNGs."""
import argparse
from PIL import Image, ImageChops

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("original")
parser.add_argument("edited")
parser.add_argument("box", nargs=4, type=int, metavar="EDGE")
args = parser.parse_args()
left, top, right, bottom = args.box
with Image.open(args.original) as a, Image.open(args.edited) as b:
    if any(im.format != "PNG" or im.mode != "RGB" or im.info.get("icc_profile")
           or im.getexif().get(274, 1) != 1 or getattr(im, "n_frames", 1) != 1
           for im in (a, b)):
        parser.error("Use single-frame RGB PNGs with matching color space and normal orientation")
    if a.size != b.size or not (0 <= left < right <= a.width and 0 <= top < bottom <= a.height):
        parser.error("Use same-size images and a rectangle inside both")
    change = ImageChops.difference(a.crop(args.box), b.crop(args.box)).getbbox()
if change:
    print("Changed region in source coordinates:",
          (left + change[0], top + change[1], left + change[2], top + change[3]))
    raise SystemExit(1)
print("Protected rectangle matches pixel for pixel.")
