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.jpgand2.jpgshould be stitched first, then3.jpgand4.jpg,5.jpgand6.jpgand so on.
OR
01.jpgand02.jpgshould be stitched first, then03.jpgand04.jpg,05.jpgand06.jpgand so on.
OR
scan01.tifandscan02.tif, thenscan03.tifandscan04.tif,scan05.tifandscan06.tif...
OR
newpic0001.pngandnewpic0002.pngfirst, thennewpic0003.pngandnewpic0004.png, thennewpic0005.pngandnewpic0006.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.