t-r.de/utils/crop_image_to_bbox.py

21 lines
518 B
Python

from PIL import Image
import sys
# check if filename is present
if len(sys.argv) == 1:
sys.exit("error: no filename spcified")
f = sys.argv[1]
# open, crop and overwrite image
print(f"cropping file {f}", end='')
with Image.open(f) as i:
# do nothing if size and bbox are equivalent
bbox = i.getbbox()
if bbox[0] == 0 and bbox[1] == 0 and bbox[2] == i.size[0] and bbox[3] == i.size[1]:
print("nothing to do")
sys.exit(0)
i.crop(i.getbbox()).save(f)
print("successful")