Advertisement
here2share

# img_1280ht_aspect_ratio.py

Nov 13th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. # img_1280ht_aspect_ratio.py
  2.  
  3. from PIL import Image
  4. import re, os, sys
  5.  
  6. SIZE = 1280,1280
  7. fromDir=r"C:\Users\here2share\myImages\Over 1280ht"
  8.  
  9. def pf(*z): return '\\'.join(z)
  10.  
  11. toDir=pf(fromDir,"Resized to 1280ht")
  12. if not os.path.exists(toDir): os.makedirs(toDir)
  13. skipped=pf(fromDir,"---")
  14. if not os.path.exists(skipped): os.makedirs(skipped)
  15.  
  16. def resize_image(f):
  17.     if not os.path.exists(pf(toDir,f)):
  18.         try:
  19.             img = Image.open(pf(fromDir, f))
  20.             if max(img.size) > 1280:
  21.                 img.thumbnail(SIZE, Image.ANTIALIAS)
  22.             img.save(pf(toDir, f))
  23.             print '#',
  24.         except:
  25.             # Note: shutil.copy will remove metadata in the process meanwhile shutil.copy2 will not do so
  26.             try: shutil.copy(pf(fromDir, f), pf(skipped, f))
  27.             except: pass
  28.             print '>',
  29.     else:
  30.         print '.',
  31.  
  32. for f in os.listdir(fromDir): resize_image(f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement