4

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

2
  • Unfortunately it is not clear from the question if you want all images to be shown one below the other, or if you want the successive image to replace the previous one. Commented Aug 6, 2017 at 9:16
  • @ImportanceOfBeingErnest I made it clear in the post. Commented Aug 7, 2017 at 7:38

4 Answers 4

1

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()
     
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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.

4 Comments

Thanks for your answer. I am very new to python. I am doing this, but I received an error, do you know why? user-images.githubusercontent.com/12434910/…
Try that ^^. Otherwise, you can try: stackoverflow.com/a/30231168/8100895
can you please explain what you mean by ^^?
Opps, sorry I meant, try the edit I have written above. "^^" is to supposed to point to the above post.
1
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.