Forked from SKaplanOfficial/select_photos_for_mosaic.py
Created
September 15, 2023 16:22
-
-
Save marceloneppel/8bfb4222a76db18145fdf37609820541 to your computer and use it in GitHub Desktop.
Using PyXA, Automator, and PIL to create a mosaic of selected images.
This file contains 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 PyXA, math | |
from PIL import Image | |
# Execute Automator workflow and receive list of image paths | |
automator = PyXA.Application("Automator") | |
workflow = automator.open("/Users/exampleuser/Library/Mobile Documents/com~apple~Automator/Documents/Ask For Photos.workflow") | |
image_paths = workflow.execute() | |
# Set base dimensions of mosaic images | |
base_width = 400 | |
base_height = 200 | |
# Get number of rows and columns | |
root = int(math.sqrt(len(image_paths))) | |
# Create empty canvas | |
dim = (root * base_width, root * base_width) | |
mosaic = Image.new("RGB", dim) | |
# Populate the canvas | |
for row in range(0, root): | |
for col in range(0, root): | |
# Load image from path | |
path = image_paths[row + col * root] | |
img = Image.open(path) | |
# Resize | |
width = int(max(base_width, base_width/img.size[0] * img.size[1])) | |
img = img.resize((base_width, width), Image.ANTIALIAS) | |
mosaic.paste(img, (base_width * col, base_width * row)) | |
mosaic.show() |
This file contains 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
# Tested with PyXA 0.1.1 | |
import PyXA, math | |
# Execute Automator workflow and receive list of image paths | |
automator = PyXA.Application("Automator") | |
workflow = automator.open("/Users/exampleuser/Library/Mobile Documents/com~apple~Automator/Documents/Ask For Photos.workflow") | |
image_paths = workflow.execute() | |
# Set base dimensions of mosaic images | |
base_width = 400 | |
base_height = 200 | |
# Get number of rows and columns | |
root = int(math.sqrt(len(image_paths))) | |
mosaic = None | |
for index in range(0, len(image_paths), root): | |
row_image = None | |
if index + 1 < len(image_paths): | |
# Construct rows by stacking images horizontally | |
images = PyXA.XAImage.open(image_paths[index], image_paths[index + 1]) | |
images.resize(base_width, base_height) | |
row_image = images.horizontal_stitch() | |
# Construct mosaic by stacking rows vertically | |
if mosaic is None: | |
mosaic = row_image | |
else: | |
mosaic = PyXA.XAImage.vertical_stitch([row_image, mosaic]) | |
mosaic.show_in_preview() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment