Skip to content

Instantly share code, notes, and snippets.

@pulsejet
Created September 28, 2018 15:30
Show Gist options
  • Save pulsejet/7f9c20e47392fbff6ed10e8966ab687b to your computer and use it in GitHub Desktop.
Save pulsejet/7f9c20e47392fbff6ed10e8966ab687b to your computer and use it in GitHub Desktop.
Resize images and convert to progressive JPG with pillow
from os import listdir
from os.path import isfile, join
from PIL import Image
mypath = '.'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f)) and '.jpg' in f]
MAX_DIM = 800
for path in onlyfiles:
print(path)
image = Image.open(path)
(width, height) = image.size
# Resize
factor = min(MAX_DIM / height, MAX_DIM / width)
if factor < 0.95:
size = (int(width * factor), int(height * factor))
image = image.resize(size, Image.ANTIALIAS)
image.save(path, 'JPEG', quality=90, optimize=True, progressive=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment