Skip to content

Instantly share code, notes, and snippets.

@ismaild
Created February 23, 2012 12:27
Show Gist options
  • Save ismaild/1892628 to your computer and use it in GitHub Desktop.
Save ismaild/1892628 to your computer and use it in GitHub Desktop.
Crop, Resize & Convert Images
import os
from PIL import Image
from glob import glob
pwd = os.path.dirname(os.path.abspath(__file__))
ext = '.png'
ext_new = '.jpg'
fpath = os.path.join(pwd,'*'+ext)
img_list = glob(fpath)
left = 450
top = 263
width = 1026
height = 636
size = 300,190
# Crop , Resize, Convert
for img_file in img_list:
img_name = os.path.basename(img_file).replace(' ','_').strip(ext)
img_name = img_name.replace('_&_','_').lower() + '_tmb' + ext_new
img = Image.open(img_file)
box = (left, top, left+width, top+height)
area = img.crop(box)
area = area.resize(size, Image.ANTIALIAS)
area.save(img_name, 'jpeg')
print img_name, img_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment