3Creates a square crop of an image.
6from pathlib import Path
12if __name__ == "__main__":
14 path = Path(sys.argv[1])
16 sys.exit(f"Usage: {__file__} IMAGE_PATH")
20 out_path = path.with_suffix(".square" + path.suffix)
21 assert out_path != path
23 if im.width == im.height:
25 elif im.width > im.height:
26 left = (im.width - im.height) / 2
28 right = im.width - (im.width - im.height) / 2
31 crop_im = im.crop((left, upper, right, lower))
32 crop_im.save(out_path)
34 elif im.height > im.width:
36 upper = (im.height - im.width) / 2
38 lower = im.height - (im.height - im.width) / 2
40 crop_im = im.crop((left, upper, right, lower))
41 crop_im.save(out_path)