Skip to content

Instantly share code, notes, and snippets.

@Compro-Prasad
Forked from jweir/stitch.py
Last active January 19, 2020 12:13
Show Gist options
  • Save Compro-Prasad/47947260e7fb79a1390a00b171367525 to your computer and use it in GitHub Desktop.
Save Compro-Prasad/47947260e7fb79a1390a00b171367525 to your computer and use it in GitHub Desktop.
stitch images in python
"""
First make sure you have the Python Image libraries installed
sudo pip install pillow
Down below you will need to define the absolute path to the images see "dir=..."
Run this by entering
python stitch.py
"""
from PIL import Image
import os
# change this to the director that has your images
dir = "/Users/johnweir/Desktop"
out_file = "joined_images.jpg"
def getFiles(dir):
files = []
for c in os.listdir(dir):
_, ext = os.path.splitext(c)
if ext == '.jpg':
files.append(c)
return files
def merge_images(dir, file):
image = Image.open(os.path.join(dir, file))
base = Image.open(out_file)
(width1, height1) = image.size
(width2, height2) = base.size
result_width = max(width1, width2)
result_height = height1 + height2
out = Image.new('RGB', (result_width, result_height))
out.paste(im=base, box=(0, 0))
out.paste(im=image, box=(0, height2))
out.save(out_file,"JPEG")
return out
def createTarget():
result = Image.new('RGB', (1, 1))
result.save(out_file,"JPEG")
print("Finding image files")
ff = getFiles(dir)
ff.sort()
print("Found", len(ff), "files to process")
print("Stiching files")
createTarget()
for f in ff:
merge_images(dir, f)
print("all done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment