Albert
Albert

Reputation: 194

Reading images from a folder using matplotlib.image

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

Upvotes: 4

Views: 22462

Answers (4)

user2110417
user2110417

Reputation:

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

Upvotes: 1

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339765

Using the %matplotlib inline backend

The %matplotlib inline backend displays the matplotlib plots as png images. You can use IPython.display to display the image and in this case display another image after some time.

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from IPython import display
import time

%matplotlib inline

PATH = "../data/kodim{0:02d}.png"

for i in range(1,4):
    p = PATH.format(i)
    #print p
    image = mpimg.imread(p) # images are color images
    plt.gca().clear()
    plt.imshow(image);
    display.display(plt.gcf())
    display.clear_output(wait=True)
    time.sleep(1.0) # wait one second

Note that in this case you are not showing the image in the same figure, but rather replace the image of the old figure with the image of the new figure.

Using the %matplotlib notebook backend

You may directly use an interactive backend to show your plot. This allows to animate the plot using matplotlib.animation.FuncAnimation.

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

%matplotlib notebook

PATH = "../data/kodim{0:02d}.png"

def update(i):
    p = PATH.format(i)
    #print p
    image = mpimg.imread(p) # images are color images
    plt.gca().clear()
    plt.imshow(image)

ani = animation.FuncAnimation(plt.gcf(), update, range(1,4), interval = 1000, repeat=False)
plt.show()
     

Upvotes: 1

Jonathan
Jonathan

Reputation: 1936

The more coherent way to do this would be to:

  1. use dir() to obtain all the image names into a variable. For example images = dir(path) Images will hold all the names of the images in your directory pointed to by your path.
  2. Then loop through the images like so:

    for image in images:
       cur_img =mpimg.imread(image) 
       ... 
       plt.imshow(cur_img)
       sleep(5)    #in seconds
    

The reason I don't like the string manipulation is because it is a short term fix. It's nicer to do it this way so that it will work in a more general format.

Upvotes: 1

Alex
Alex

Reputation: 1264

Here is my solution:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from time import sleep

%matplotlib inline

for i in range(1,3):
     PATH = "kodim01.png"
     N = "%02d" % i
     print PATH.replace("01", N)
     image = mpimg.imread(PATH.replace("01", N))
#      plt.show()
     plt.imshow(image)
     sleep(5)    #in seconds

Upvotes: 1

Related Questions