Skip to content

Instantly share code, notes, and snippets.

@jonaqp
Forked from ShantanuJoshi/compressMe.py
Created November 13, 2017 22:24
Show Gist options
  • Select an option

  • Save jonaqp/e2c2c4ee098fcc650bf3d37a92d2ec10 to your computer and use it in GitHub Desktop.

Select an option

Save jonaqp/e2c2c4ee098fcc650bf3d37a92d2ec10 to your computer and use it in GitHub Desktop.

Revisions

  1. @ShantanuJoshi ShantanuJoshi revised this gist Sep 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion compressMe.py
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ def main():
    for file in os.listdir(pwd):
    if os.path.splitext(file)[1].lower() in ('.jpg', '.jpeg'):
    num += 1
    tot += compressMeNoDim(file, verbose)
    tot += compressMe(file, verbose)
    print "Average Compression: %d" % (float(tot)/num)
    print "Done"

  2. @ShantanuJoshi ShantanuJoshi revised this gist Sep 8, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion compressMe.py
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,9 @@ def compressMe(file, verbose=False):
    oldsize = os.stat(filepath).st_size
    picture = Image.open(filepath)
    dim = picture.size


    #set quality= to the preferred quality.
    #I found that 85 has no difference in my 6-10mb files and that 65 is the lowest reasonable number
    picture.save("Compressed_"+file,"JPEG",optimize=True,quality=85)

    newsize = os.stat(os.path.join(os.getcwd(),"Compressed_"+file)).st_size
  3. @ShantanuJoshi ShantanuJoshi revised this gist Sep 8, 2016. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions compressMe.py
    Original file line number Diff line number Diff line change
    @@ -7,13 +7,10 @@

    def compressMe(file, verbose=False):
    filepath = os.path.join(os.getcwd(), file)

    oldsize = os.stat(filepath).st_size

    picture = Image.open(filepath)

    dim = picture.size

    picture.save("Compressed_"+file,"JPEG",optimize=True,quality=85)

    newsize = os.stat(os.path.join(os.getcwd(),"Compressed_"+file)).st_size
    @@ -24,7 +21,6 @@ def compressMe(file, verbose=False):

    def main():
    verbose = False

    #checks for verbose flag
    if (len(sys.argv)>1):
    if (sys.argv[1].lower()=="-v"):
  4. @ShantanuJoshi ShantanuJoshi revised this gist Sep 8, 2016. No changes.
  5. @ShantanuJoshi ShantanuJoshi renamed this gist Sep 8, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @ShantanuJoshi ShantanuJoshi created this gist Sep 8, 2016.
    46 changes: 46 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    #run this in any directory add -v for verbose
    #get Pillow (fork of PIL) from pip before running --> pip install Pillow

    import os
    import sys
    from PIL import Image

    def compressMe(file, verbose=False):
    filepath = os.path.join(os.getcwd(), file)

    oldsize = os.stat(filepath).st_size

    picture = Image.open(filepath)

    dim = picture.size

    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():
    verbose = False

    #checks for verbose flag
    if (len(sys.argv)>1):
    if (sys.argv[1].lower()=="-v"):
    verbose = True

    #finds present working dir
    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 += compressMeNoDim(file, verbose)
    print "Average Compression: %d" % (float(tot)/num)
    print "Done"

    if __name__ == "__main__":
    main()