Skip to content

Instantly share code, notes, and snippets.

@jossef
Created January 3, 2015 09:25
Show Gist options
  • Save jossef/60ace700ac81c45ca30a to your computer and use it in GitHub Desktop.
Save jossef/60ace700ac81c45ca30a to your computer and use it in GitHub Desktop.
python jpg thumbnail creator
import os
from PIL import Image
output_dir = '.\\output'
thumbnail_size = (200,200)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for dirpath, dnames, fnames in os.walk(".\\input"):
for f in fnames:
full_path = os.path.join(dirpath, f)
if f.endswith(".jpg"):
filename = 'thumbnail_{0}'.format(f)
new_path = os.path.join(output_dir, filename)
if os.path.exists(new_path):
os.remove(new_path)
im = Image.open(full_path)
im.thumbnail(thumbnail_size)
im.save(new_path, "JPEG")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment