Created
May 30, 2017 18:03
-
-
Save dorukgezici/b6be9d11f1389f7c7b225fbf40e23a0b to your computer and use it in GitHub Desktop.
A Pythonista Share Extension script that makes your photo square (instagramifies your photo) by adding black background to the narrower side and saves it to your photo library.
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
import appex, photos, sys | |
from PIL import Image | |
img = appex.get_image() | |
box = img.getbbox() # left upper right lower | |
width = box[2] - box[0] | |
height = box[3] - box[1] | |
if width > height: | |
size = (width, width) | |
new_box = (0, (width - height) // 2, width, width - (width - height) // 2) | |
elif height >= width: | |
size = (height, height) | |
new_box = ((height - width) // 2, 0, height - (height - width) // 2, height) | |
else: | |
sys.exit() | |
im = Image.new("RGB", size, "black") | |
im.paste(img, new_box) | |
im.save('tmp.png') | |
try: | |
album = [a for a in photos.get_albums() if a.title == 'Instagram'][0] | |
except IndexError: | |
album = photos.create_album('Instagram') | |
asset = photos.create_image_asset('tmp.png') | |
album.add_assets([asset]) | |
im.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment