Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. import os
  2. from PIL import Image, ImageChops
  3.  
  4. def trim(im):
  5. bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
  6. diff = ImageChops.difference(im, bg)
  7. diff = ImageChops.add(diff, diff, 2.0, -100)
  8. bbox = diff.getbbox()
  9. if bbox:
  10. return im.crop(bbox)
  11.  
  12. img_types = ['jpg', 'peg', 'bmp', 'png', 'gif']#'peg' in case it says '.jpeg'
  13.  
  14. for root, dirs, files in os.walk("/imgroot"):#Folder where all images are stored, will walk recursively
  15. for file in files:
  16. if file[-3:] in img_types and "cropped" not in file:
  17. im = Image.open(os.path.join(root, file))
  18. im = trim(im)
  19. im.save(os.path.join(root, "cropped_" + file))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement