Traverse files in directory

Can someone explain to me How this pic_index works? How can we know that it jumps to the next image?

# Set up matplotlib fig, and size it to fit 4x4 pics
fig = plt.gcf()
fig.set_size_inches(ncols*4, nrows*4)

pic_index+=8

next_cat_pix = [os.path.join(train_cats_dir, fname) 
                for fname in train_cat_fnames[ pic_index-8:pic_index] 
               ]

next_dog_pix = [os.path.join(train_dogs_dir, fname) 
                for fname in train_dog_fnames[ pic_index-8:pic_index]
               ]

for i, img_path in enumerate(next_cat_pix+next_dog_pix):
  # Set up subplot; subplot indices start at 1
  sp = plt.subplot(nrows, ncols, i + 1)
  sp.axis('Off') # Don't show axes (or gridlines)

  img = mpimg.imread(img_path)
  plt.imshow(img)

plt.show()

Hi @Almog_Tovim,

First you get a list with image paths - 8 cats and 8 dogs - and you store them into next_cat_pix and next_dog_pix. Then you join them into the enumeration - next_cat_pix+next_dog_pix.

Now you loop over those image paths. If you noticed, loop iterates through the combined list, returning a number - i - and a path to the image - img_path. As you want to plot 16 images in your figure, you need to set each of the subplots first, read the image and plot it.

Finally you show the plot.

Hope it helps,

Oh, I see now. Thank you :slightly_smiling_face: