Skip to content

Instantly share code, notes, and snippets.

@ashussen
Forked from ShantanuJoshi/compressMeDim.py
Last active October 18, 2018 07:15
Show Gist options
  • Save ashussen/c6d5b40ed0511f94d324bf9aedc854fa to your computer and use it in GitHub Desktop.
Save ashussen/c6d5b40ed0511f94d324bf9aedc854fa to your computer and use it in GitHub Desktop.
#run this in any directory with the max width parameter add -v for verbose
#image will be scalled in proportion to confine within the max width parameter
#this allows you to get images that are within a box dimension for online publishing (i.e. 400x400) without distorting the image
#get Pillow (fork of PIL) from pip before running --> pip install Pillow
#regarding timing: 42 seconds for 4938 so max 15 minutes for 100,000 images
from __future__ import division
from PIL import Image
import os
import sys
def compressMeReturn(file, maxDim, verbose=False):
filepath = os.path.join(os.getcwd(), file)
oldsize = os.stat(filepath).st_size
picture = Image.open(filepath)
dim = picture.size
ratio = (maxDim/dim[0],maxDim/dim[1])
picture.thumbnail((dim[0]*ratio[0],dim[1]*ratio[1]), Image.ANTIALIAS)
picture.save("Compressed_"+file,"JPEG",optimize=True,quality=85)
newsize = os.stat(os.path.join(os.getcwd(),"Compressed_"+file)).st_size
percent = (oldsize-newsize)/float(oldsize)*100
if (verbose):
print "File compressed from {0} to {1} or {2}%".format(oldsize,newsize,percent)
return percent
def main():
maxDimension = sys.argv[1]
verbose = False
if (len(sys.argv)>2):
if (sys.argv[2].lower()=="-v"):
verbose = True
pwd = os.getcwd()
tot = 0
num = 0
for file in os.listdir(pwd):
if os.path.splitext(file)[1].lower() in ('.jpg', '.jpeg'):
num+=1
tot += compressMeReturn(file, int(maxDimension),verbose)
print "Average Compression: %d" % (float(tot)/num)
print "Done"
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment