1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import os from PIL import Image
def imgToProgressive(path): if not path.split('.')[-1:][0] in ['png','JPG','jpg','jpeg']: return if os.path.isdir(path): return img = Image.open(path) exif = img.info['exif'] destination = path.split('.')[:-1][0]+'_destination.'+path.split('.')[-1:][0] try: print(path.split('/')[-1:][0],'compressing image') img.save(destination, "JPEG", quality=80, optimize=True, progressive=True, exif=exif) print(path.split('/')[-1:][0],'done') except IOError: PIL.ImageFile.MAXBLOCK = img.size[0] * img.size[1] img.save(destination, "JPEG", quality=80, optimize=True, progressive=True, exif=exif) print(path.split('/')[-1:][0],'done') print('renamed') os.remove(path) os.rename(destination,path)
for d,_,fl in os.walk(os.getcwd()): for f in fl: try: imgToProgressive(d+'/'+f) except: pass
|