Created
September 19, 2018 09:28
-
-
Save makomweb/88c9cc398159390e8a2fa1beda5dcfed to your computer and use it in GitHub Desktop.
Python script for batch resizing images and keeping the aspect ratio
This file contains hidden or 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 PIL import Image | |
import os, sys | |
path = "c:\\Workspace\Images" | |
dirs = os.listdir( path ) | |
mywidth = 640 | |
def resize_keep_aspect_ration(): | |
for item in dirs: | |
img_path = path + "\\" + item | |
if os.path.isfile(img_path): | |
im = Image.open(img_path) | |
f, e = os.path.splitext(img_path) | |
wpercent = (mywidth / float(im.size[0])) | |
hsize = int((float(im.size[1]) * float(wpercent))) | |
im = im.resize((mywidth, hsize), Image.ANTIALIAS) | |
im.save(f + '.png', 'PNG') | |
resize_keep_aspect_ration() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment