Skip to content

Instantly share code, notes, and snippets.

@siddharth96
Last active August 29, 2015 14:05
Show Gist options
  • Save siddharth96/758c7baf33ebc513d491 to your computer and use it in GitHub Desktop.
Save siddharth96/758c7baf33ebc513d491 to your computer and use it in GitHub Desktop.
Python script to create image thumbnails while preserving Aspect ratio
import PIL
from os import listdir
from os.path import isfile, join
from PIL import Image
directory_path = "<some directory>"
onlyfiles = [ f for f in listdir(directory_path) if isfile(join(directory_path,f)) ]
basewidth = 64
for fl_name in onlyfiles:
if not fl_name or fl_name.endswith('.db'):
continue
img = Image.open(fl_name)
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
img.save(fl_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment