如何在python中从文件中绘制多个图像?

我正在尝试从 jupyter notebook 中的文件绘制多个图像。图像已显示,但它们在一个列中。我在用着:


%matplotlib inline

from os import listdir

form PIL import image as PImage

from matplotlib import pyplot as plt


def loadImages(path):

    imagesList = listdir(path)

    loadedImages = []

    for image in imagesList:

        img = PImage.open(path+image)

        LoadedImages.append(img)

        Return loadedImages

path = 'C:/Users/Asus-PC/flowers/'

imgs = loadImages(path)


for img in imgs

    plt.figure()

    plt.imshow(img)

我希望它们出现在网格布局(行和列)中。部分问题是我不知道 add_subplot 的参数是什么意思。我怎样才能做到这一点?


慕工程0101907
浏览 272回答 2
2回答

开满天机

您可以使用,matplotlib但显示大量图像往往非常缓慢且效率低下。因此,如果您想更快、更轻松地完成此操作 - 我建议您使用我的名为IPyPlot的包:import ipyplotipyplot.plot_images(images_list, max_images=20, img_width=150)它支持以下格式的图像:-string文件路径-PIL.Image对象-numpy.ndarray表示图像的对象它能够在大约 70 毫秒内显示数百张图像

侃侃尔雅

您可以使用创建多个子图plt.subplots。确定加载图像的列数和行数。就像是:from os import listdirimport matplotlib.pyplot as pltpath = 'C:/Users/Asus-PC/flowers/'imagesList = listdir(path)n_images = len(imagesList)    figure, axes = plt.subplots(n_images, 1)   # (columns, rows)    for ax, imgname in zip(axes, imagesList):  # Iterate over images and        img = plt.imread(path+imgname)     # axes to plot one image on each.        ax.imshow(img)                     # You can append them to an empty                                           # list if you need them later.plt.show()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python