I have a directory (sdir) with 23 sub directories. Each sub directory has about 100 jpg image files. I want to go to each subdirectory and display the first 5 images in a row using matplotlib, then go to the next sub directory and plot the first 5 images from that subdirectory in the second row of the plot. etc. So in the end there would be 23 rows with 5 images per row and each row contains 5 images from a specific sub directory. For the first row in the plot, it plots the first 4 images of the first sub directory ( not the first 5 as I expected) and 1 image from the second sub directory. After that it produces 5 images from each sub directory as expected. Why does it only produce 4 images for the first sub directory? Code is shown below:
sdir=r'../input/dermnet/train'
plt.figure(figsize=(20,80))
classlist=sorted(os.listdir(sdir))
row =0
for klass in classlist:
classpath=os.path.join(sdir,klass) # path to sub directory
flist=os.listdir(classpath) # list of sub directories
for i, f in enumerate(flist): # enumerate files in each sub directory
if i <5:
fpath=os.path.join(classpath,f)
img=plt.imread(fpath)
plt.title(klass[:15], color='blue', fontsize=12)
print(i, row, row +i +1, img.shape, klass) # check if i, row and row + i +1 are correct, also shape say img was read in
plt.subplot(23,5, row + i + 1 ) # 23 rows, one for each sub directory, 5 images per sub directory
plt.imshow(img) # get the images plotted
else:
row +=5 # increment row by 5 to start next row
break # break after first 5 images from a sub directory are plotted and go one to the next sub directory
plt.show() # show all the images
The printed out values for i, row and row + i +1 and the sub directory name appear to be correct as shown below:
i row row + i + 1 klass (subdir name)
0 0 1 a
1 0 2 a
2 0 3 a
3 0 4 a
4 0 5 a
0 5 6 b
1 5 7 b
etc etc etc etc
The image shapes I printed out were correct so plt.imread successfully read in each image. I thought at first the 5th image in the first sub directory might be invalid but that is not the case. Any help would be most appreciated.