Created
August 11, 2016 02:03
-
-
Save fdcore/c39ca0ea9c339c196d38525c032d4b68 to your computer and use it in GitHub Desktop.
Create squere image on python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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