Reading images from a folder using matplotlib.image

Viewed 16130

I am trying to show images one after another in a loop in one figure, meaning that after image 1 is shown, after a few second, image 2 be shown in the same figure. I have the following code so far:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

%matplotlib inline

for i in range(1,4):
     PATH = "kodim01.png"
     N = "%02d" % i
     print PATH.replace("01", N)
     image = mpimg.imread(PATH) # images are color images
     plt.show()
     plt.imshow(image)

However it shows one image (the first image) 3 times. although the path changes. the image does not change. Please see results below: Here

How can I 1) show all the images in one figure one after each other, i.e. the successive image be replaced to the previous one e.g. 1 sec delay between each image. and 2) show all the images, not only repeating one image?

Thanks

4 Answers
import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline

images = []
for img_path in glob.glob('folder/*.png'):
    images.append(mpimg.imread(img_path))

plt.figure(figsize=(20,10))
columns = 5
for i, image in enumerate(images):
    plt.subplot(len(images) / columns + 1, columns, i + 1)
    plt.imshow(image)

where 'folder' contains images with .png extension

Related