Reputation: 434
I have 50.000 in a folder that have names "1.png" to "50000.png". And I want to save them to some array in spesicfic order, because I have labels for them in order to classify them. So when order of the images get messed up, labels become useless. (i.e: 1.png will be saved to first index and 2.png will be saved to second index etc.). I try this code for this purpose:
#Read and save all Images to images array.
images=[]
image_folder=[img for img in glob.glob("C:/Users/yazilim1/Desktop/Jupyter/cifar-10/train/*.png")]
image_folder.sort()
for image in image_folder:
images.append(cv2.imread(image))
print(image)
print("Number of Images added:" + str(len(images)))
However, print(image) output looks like this:
How can I achive to save that images to "images" array with order?
Upvotes: 1
Views: 154
Reputation: 44926
It is in order though.
Since image_folder
is a list of strings, image_folder.sort()
will perform lexicographical sorting, where "1" < "10" < "100" < "1000" < "1001" < ... < "2" < "20"
and so on.
If you want to treat "100.png"
like "integer 100 + file extension", do this:
image_folder.sort(key=lambda file_name: int(file_name.split('.')[0]))
Upvotes: 2