Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from PIL import Image, ImageChops
- def trim(im):
- bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
- diff = ImageChops.difference(im, bg)
- diff = ImageChops.add(diff, diff, 2.0, -100)
- bbox = diff.getbbox()
- if bbox:
- return im.crop(bbox)
- img_types = ['jpg', 'peg', 'bmp', 'png', 'gif']#'peg' in case it says '.jpeg'
- for root, dirs, files in os.walk("/imgroot"):#Folder where all images are stored, will walk recursively
- for file in files:
- if file[-3:] in img_types and "cropped" not in file:
- im = Image.open(os.path.join(root, file))
- im = trim(im)
- im.save(os.path.join(root, "cropped_" + file))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement