SHARE
TWEET

Untitled

a guest Jun 19th, 2017 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from os import listdir
  2. from os.path import abspath, isdir, join, isfile, splitext
  3. from PIL import Image
  4.  
  5. def main():
  6.     paths('.')
  7.  
  8. def compress_img(img_path):
  9.     new_name, ext = splitext(img_path)
  10.     if ext in ['.jpg', '.jpeg', '.png']:
  11.         extent = ''
  12.         img = Image.open(img_path)
  13.         if ext[1:] in ['.jpg', '.jpeg']:
  14.             extent = "JPEG"
  15.         if ext[1:] == '.png':
  16.             extent = "PNG"
  17.         img.save(new_name+ext, extent, optimize=True,quality=90)
  18.  
  19.  
  20. def paths(path):
  21.     for d in listdir(path):
  22.         p = join(path, d)
  23.         if isdir(p) and d != 'env':
  24.             paths(p)
  25.         # if d == 'env':
  26.         #     continue
  27.         _, ext = splitext(p)
  28.         if ext in ['.jpg', '.jpeg', '.png']:
  29.             # print abspath(p)
  30.             try:
  31.                 compress_img(abspath(p))
  32.             except Exception as e:
  33.                 print e
RAW Paste Data
Want to get better at Python?
Learn to code Python in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top