Skip to content

Instantly share code, notes, and snippets.

@widyakumara
Created November 30, 2012 15:47
Show Gist options
  • Save widyakumara/4176546 to your computer and use it in GitHub Desktop.
Save widyakumara/4176546 to your computer and use it in GitHub Desktop.
simple resize & crop with python & PIL
import PIL
from PIL import Image
def resizecrop(src, out, width, height):
img = Image.open(src)
src_w = float(img.size[0])
src_h = float(img.size[1])
ratio = max(width/src_w, height/src_h)
out_w = int(src_w * ratio)
out_h = int(src_h * ratio)
out_t = (out_h - height) / 2
out_l = (out_w - width) / 2
out_r = out_l + width
out_b = out_t + height
img = img.resize((out_w, out_h), PIL.Image.ANTIALIAS)
img = img.crop((out_l, out_t, out_r, out_b))
img.save(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment