Reading images from a folder using matplotlib.image

I am trying to show images one by one in a loop on one shape, which means that after showing image 1 after a few seconds, image 2 will be shown in the same figure. So far, I have the following code:

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 (first image) 3 times. although the path is changing. the image does not change. See Results below: Here

How can I 1) show all images in one picture one by one, i.e. the sequential image will be replaced with the previous one for example 1 with a delay between each image. and 2) show all images, not just repeat one image?

thank

+3


source to share


4 answers


Using the %matplotlib inline

backend

The backend %matplotlib inline

renders matplotlib plots as png images. You can use IPython.display

to display an image and in this case display another image after a while.

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 drawing, but rather replacing the image of the old shape with the image of the new drawing.



Using the %matplotlib notebook

backend

You can directly use the interactive backend to show your plot. This allows you to animate the graph with 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()

      

+1


source


A more consistent way to do it:

  • use dir()

    to get all the image names in a variable. For example images = dir(path)

    Images will contain all the names of the images in your directory that your path points to.
  • 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 string manipulation is because it is a short term fix. Better to do it so that it works in a more general format.

+1


source


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

      

+1


source


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

0


source







All Articles