So, I want to create a utility to stitch images together. I have this code so far:
def merger(file1, file2):
# File I/O: Open source images
image1 = Image.open(file1)
image2 = Image.open(file2)
# Read the source image dimensions using pillow Image class
(width1, height1) = image1.size
(width2, height2) = image2.size
# Calculate the output image dimensions
merged_width = width1 + width2 # The width is a sum of the 2 widths
merged_height = max(height1, height2) # The height is the larger of the two
# Merge the images using pillow Image class
output = Image.new('RGB', (merged_width, merged_height)) # Create new image object (for output)
output.paste(im=image1, box=(0, 0)) # Paste the 1st source image into the output object
output.paste(im=image2, box=(width1, 0)) # Paste the 2nd source image into the output object
return output
How do I cycle through all the image files in a folder? I suppose I'd use a loop, but how do I read each pair of image files present in a folder, recognize their order from the file names, stitch each pair together, then go on to the next pair of files?
Files should be stitched based on the numbers in the filenames. Examples:
1.jpg
and2.jpg
should be stitched first, then3.jpg
and4.jpg
,5.jpg
and6.jpg
and so on.
OR
01.jpg
and02.jpg
should be stitched first, then03.jpg
and04.jpg
,05.jpg
and06.jpg
and so on.
OR
scan01.tif
andscan02.tif
, thenscan03.tif
andscan04.tif
,scan05.tif
andscan06.tif
...
OR
newpic0001.png
andnewpic0002.png
first, thennewpic0003.png
andnewpic0004.png
, thennewpic0005.png
andnewpic0006.png
...
You get the idea: go according to the number at the end of the file, ignore the leading zeroes.
I am using pillow for image processing and tkinter for GUI if that matters.
Thanks.