Skip to content

Instantly share code, notes, and snippets.

@erenkeskin
Created November 28, 2019 12:23
Show Gist options
  • Save erenkeskin/9481de9b6e7874860fe856f535272a91 to your computer and use it in GitHub Desktop.
Save erenkeskin/9481de9b6e7874860fe856f535272a91 to your computer and use it in GitHub Desktop.
################################################################################################
#
# image resize: https://www.techtrekking.com/how-to-compress-images-using-python/
# resize in directory: http://herenkeskin.com/python-dizin-icerisindeki-gorsellerin-boyutlarini-kucultme
#
################################################################################################
from PIL import Image, ImageDraw, ImageFont
import os
import glob
path = os.getcwd()
extensions = ['jpg', 'jpeg', 'png']
totalInputSize = 0
totalOutputSize = 0
files = [f for ext in extensions for f in glob.glob(path + "/**/*." + ext, recursive = True)]
print('*** Program Started ***')
for file in files:
im = Image.open(file)
print('Input Name : ', file)
print('Input Image Size : ', os.path.getsize (file))
totalInputSize += os.path.getsize (file)
im.save(file, optimize = True, quality = 50)
print('Output Image Size : ', os.path.getsize (file))
totalOutputSize += os.path.getsize (file)
print('Total Compressed Size = ', (totalInputSize - totalOutputSize) / 1000, 'kb')
print('Total Compressed Size %', (totalInputSize - totalOutputSize) / totalInputSize * 100)
print('*** Program Ended ***')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment