Skip to content

Instantly share code, notes, and snippets.

@fdcore
Created August 11, 2016 02:03
Show Gist options
  • Save fdcore/c39ca0ea9c339c196d38525c032d4b68 to your computer and use it in GitHub Desktop.
Save fdcore/c39ca0ea9c339c196d38525c032d4b68 to your computer and use it in GitHub Desktop.
Create squere image on python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PIL import Image
def image_squere(filepath):
img = Image.open(filepath)
width, height = img.size
if width > height:
delta = width - height
left = int(delta/2)
upper = 0
right = height + left
lower = height
else:
delta = height - width
left = 0
upper = int(delta/2)
right = width
lower = width + upper
img = img.crop((left, upper, right, lower))
img.save(filepath)
image_squere('horse_porn.jpg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment