Skip to content

Instantly share code, notes, and snippets.

@makomweb
Created September 19, 2018 09:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save makomweb/88c9cc398159390e8a2fa1beda5dcfed to your computer and use it in GitHub Desktop.
Save makomweb/88c9cc398159390e8a2fa1beda5dcfed to your computer and use it in GitHub Desktop.
Python script for batch resizing images and keeping the aspect ratio
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