Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

iOS Photo Merge for Pythonista 3

$
0
0

One of the changes I've been trying to adopt with shifting more of myworkflows to my iPad has been to also move to my scripting from pythonista to Pythonista 3 . The key here is the original Pythonista app used Python 2 as the core language, while Pythonista 3 uses Python 3. The move to Python 3 has been tougher road to hoe than I originally thought it was going to be. Beyond the syntax changes in Python 3, some of the underlying libraries in Pythonista 3 have also been updated as well.

For example, in one of the most common iOS automation scripts I use is merging two images, applying a background border, and saving the merged image as a new file. The script pops up a dialog box and asks me to select an image from my photo library.

In the original Pythonista app this was two lines of code:

console.alert("Pick first image", "", "Select") im1 = photos.pick_image(show_albums=True)

Pythonista updated it's libraries and that same line of code looks like this now:

console.alert("Pick first image", "", "Select") im1 = photos.pick_asset()

In Pythonista 3, this required an additional step to assign the selected image to a variable:

console.alert("Pick first image", "", "Select") img1 = photos.pick_asset() img1 = img1.get_image()

After spending sometime over the weekend I finally got my photo merge script updated for Pythonista 3.

import clipboard from PIL import Image import console import photos console.clear() console.alert("Pick first image", "", "Select") img1 = photos.pick_asset() img1 = img1.get_image() console.alert("Pick second image", "", "Select") img2 = photos.pick_asset() img2 = img2.get_image() console.show_activity() w1, h1 = img1.size w2, h2 = img2.size # Set the width and height of each image img1_w = img1.size[0] img1_h = img1.size[1] img2_w = img2.size[0] img2_h = img1.size[1] def image_merge(img): if (img1_w * 1.0) / img1_h > 1: # Landscape screenshot print("Landscape screenshot...") background = Image.new('RGB', ((img1_w+20), ((img1_h*2)+30)), (88,88,88)) print ("Generating image...") background.paste(img1,(10,10)) background.paste(img2,(10,(img1_h+20))) photos.save_image(background) print ("Image saved") else: #Portrait screenshot print ("Portrait screenshot...") background = Image.new('RGB', (((img1_w*2)+30),(img1_h+20)), (88, 88, 88)) print ("Generating image...") background.paste(img1,(10,10)) background.paste(img2,((img1_w+20),10)) photos.save_image(background) print("Image saved") if img1_w == img2_w: image_merge(img1) print("Done...") else: console.alert("Incorrect image ratio", "", "Ok")

Here's a GitHub Gist for easier downloading: photo_merge_p3.py

As I've looked at some of my key iOS Pythonista scripts I'm finding more subtle changes. I'll continue to point out some of the discoveries I make in my move to Pythonista 3/Python 3.


Viewing all articles
Browse latest Browse all 9596

Trending Articles