Created
September 28, 2018 15:30
-
-
Save pulsejet/7f9c20e47392fbff6ed10e8966ab687b to your computer and use it in GitHub Desktop.
Resize images and convert to progressive JPG with pillow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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