Using plt.imshow () to display multiple images

How to use matlib plt.imshow (image) function to display multiple images?

For example, my code is as follows:

for file in images: process(file) def process(filename): image = mpimg.imread(filename) <something gets done here> plt.imshow(image) 

My results show that only the last processed image is shown efficiently, overwriting other images

+9
source share
2 answers

You can configure the framework to display multiple images using the following:

 for file in images: process(file) def process(filename): image = mpimg.imread(filename) <something gets done here> plt.figure() plt.imshow(image) 

This will stack the images vertically

+11
source

First, load the image from the file into a digital matrix

 import numpy import cv2 import os def load_image(image: Union[str, numpy.ndarray]) -> numpy.ndarray: # Image provided ad string, loading from file .. if isinstance(image, str): # Checking if the file exist if not os.path.isfile(image): print("File {} does not exist!".format(imageA)) return None # Reading image as numpy matrix in gray scale (image, color_param) return cv2.imread(image, 0) # Image alredy loaded elif isinstance(image, numpy.ndarray): return image # Format not recognized else: print("Unrecognized format: {}".format(type(image))) print("Unrecognized format: {}".format(image)) return None 

Then you can build some images using the following method:

 import matplotlib.pyplot as plt def show_images(images: list) -> None: n: int = len(images) f = plt.figure() for i in range(n): # Debug, plot figure f.add_subplot(1, n, i + 1) plt.imshow(images[i]) plt.show(block=True) 
0
source

Source: https://habr.com/ru/post/1013265/


All Articles