Skip to content

Instantly share code, notes, and snippets.

@jsalbert
Last active August 6, 2019 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsalbert/ba469279e123276ea16ef55bfd5df1d3 to your computer and use it in GitHub Desktop.
Save jsalbert/ba469279e123276ea16ef55bfd5df1d3 to your computer and use it in GitHub Desktop.
from PIL import Image
def center_crop(image):
# crop to ratio, center
w, h = image.size
w_center = w/2
h_center = h/2
if w > h:
image = image.crop((w_center - h/2, 0, w_center + h/2, h))
else:
image = image.crop((0, h_center - w/2, w, h_center + w/2))
return image
image = center_crop(Image.open(file_source))
image.save(file_destination)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment