I want to stitch a series of images together using the Python Imaging Library. However, the images I want to stitch together are contained in two separate directories. Is PIL
able to stitch images under this condition?
I have two series of 10 images stored in two directories - let's call them D1
and D2
. I would like to use PIL to stitch Image 1
from D1
with Image 1
from D2
, Image 2
from D1
with Image 2
from D2
, etc. I have a third directory, D3
, in which I would like to save the stitched output images.
I thought the correct way to do this would be to use the code snipped provided by user d3ming
in [this example] (Stitching Photos together) and use nested for loops to loop over D1
and D2
to provide input images.
Here is the code that I have currently:
list_im1 = sorted(glob.glob(in_dir_1+"*")) #make list of first set of images
list_im2 = sorted(glob.glob(in_dir_2+"*")) #make list of second set of images
def merge_images(file1, file2):
"""Merge two images into one, displayed side by side
:param file1: path to first image file
:param file2: path to second image file
:return: the merged Image object
"""
image1 = Image.open(file1)
image2 = Image.open(file2)
(width1, height1) = image1.size
(width2, height2) = image2.size
result_width = width1 + width2
result_height = max(height1, height2)
result = Image.new('RGB', (result_width, result_height))
result.paste(im=image1, box=(0, 0))
result.paste(im=image2, box=(width1, 0))
return result
merged = merge_images(file1, file2)
merged.save(out_dir)
for i in in_dir_1: #first loop through D1
for j in in_dir_2: #second loop through D2
merge_images(i, j)
I expected that this code snippet, combined with the nested loop, would run through in_dir_1 (D1)
, search through the image with the same position in in_dir_2 (D2)
, and return me a series of 10 stitched images in out_dir (D3)
. However, my code is not returning any output images at all.
Any help will be greatly appreciated.