Cotton code
41 lines · 1.5 KB · PythonRaw
#!/usr/bin/env python3"""The mean colour of named patches of a screenshot — how a tone change is measured.    python3 scripts/gen-cards/measure_screen.py <shot.png> name=x,y,w,h [name=x,y,w,h ...]Each patch is a box in FRACTIONS of the image (0–1, the same space Argent's`describe` prints frames in), so one set of boxes reads a simulator shot and apicture of a different pixel size alike. Prints the mean RGB (0–255) and themedian, per patch: the mean is what the eye averages over a textured paper; themedian is what a text-free patch reads when a stroke or a stain wanders in.Pick patches with nothing lettered in them — a bare stretch of ground, theblank lower half of a sheet — or the ink pulls the number down.Needs Pillow (`python3 -m pip install pillow`)."""import sysfrom PIL import Image, ImageStatdef patch(image: Image.Image, box: str) -> Image.Image:    x, y, w, h = (float(v) for v in box.split(","))    width, height = image.size    return image.crop((round(x * width), round(y * height), round((x + w) * width), round((y + h) * height)))def main() -> None:    if len(sys.argv) < 3:        raise SystemExit(__doc__)    image = Image.open(sys.argv[1]).convert("RGB")    for arg in sys.argv[2:]:        name, _, box = arg.partition("=")        stat = ImageStat.Stat(patch(image, box))        mean = tuple(round(v) for v in stat.mean)        median = tuple(stat.median)        print(f"{name:<18} mean {mean}  median {median}")if __name__ == "__main__":    main()