I am writing a script to iterate through a series of folders containing images, stack those images vertically, and save the result.
The issue is that it works for the first two folders and then stops saving the image. cv2.imwrite
fails silently so I’m struggling to find what exactly is causing the issue, notably the images are still stitched together and look perfectly fine using cv2.imshow
, and again the stitched image is saved perfectly fine in the first two loops.
The code is as follows:
import cv2
from os import listdir
from os.path import join
# Designates the main directory where all the subdirectories with images are
path="Folder_Name"
for folder in listdir(path):
# Designates each subdirectory
cpath = f'{path}\\{folder}'
print(f'Combining {folder}')
images = []
for file in listdir(cpath):
img = cv2.imread(join(cpath, file))
if img is not None:
images.append(img)
vert = cv2.vconcat(images)
cv2.imshow('combined_image.jpeg', vert)
cv2.waitKey(0)
out = f'{path}\\jpegs\\{folder}.jpeg'
cv2.imwrite(out, vert)
print(f'\tSaved {folder} to {out}')
The code outputs folder1.jpeg
and folder2.jpeg
in the JPEGs folder but cv2.imwrite
fails after that.
I’ve also tried using an absolute path to see what happens with cv2.imwrite
at each iteration, but curiously after the 2nd iteration cv2.imwrite
just deletes it, as in test.jpeg
will appear as folder1.jpeg
on the first iteration, folder2.jpeg
on the second, and then gets deleted.
There are roughly 200 iterations to go through.