Python 使用 plt.imshow() 显示多张图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41210823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:36:49  来源:igfitidea点击:

Using plt.imshow() to display multiple images

pythonmatplotlib

提问by zooter

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

如何使用 matlib 函数 plt.imshow(image) 来显示多个图像?

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 effectively overwriting the other images

我的结果表明,只有最后处理的图像显示有效覆盖其他图像

采纳答案by datawrestler

You can set up a framework to show 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

这将垂直堆叠图像

回答by Aadhar Bhatt

To display the multiple images use subplot()

要显示多个图像,请使用 subplot()

plt.figure()

#subplot(r,c) provide the no. of rows and columns
f, axarr = plt.subplots(4,1) 

# use the created array to output your multiple images. In this case I have stacked 4 images vertically
axarr[0].imshow(v_slice[0])
axarr[1].imshow(v_slice[1])
axarr[2].imshow(v_slice[2])
axarr[3].imshow(v_slice[3])

回答by alessiosavi

In first instance, load the image from file into a numpy matrix

首先,将文件中的图像加载到 numpy 矩阵中

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 plot multiple image 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)