为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕工程0101907 2022-06-22 16:20:53
我正在尝试从 jupyter notebook 中的文件绘制多个图像。图像已显示,但它们在一个列中。我在用着:%matplotlib inlinefrom os import listdirform PIL import image as PImagefrom matplotlib import pyplot as pltdef loadImages(path):    imagesList = listdir(path)    loadedImages = []    for image in imagesList:        img = PImage.open(path+image)        LoadedImages.append(img)        Return loadedImagespath = 'C:/Users/Asus-PC/flowers/'imgs = loadImages(path)for img in imgs    plt.figure()    plt.imshow(img)我希望它们出现在网格布局(行和列)中。部分问题是我不知道 add_subplot 的参数是什么意思。我怎样才能做到这一点?
查看完整描述

2 回答

?
开满天机

TA贡献1786条经验 获得超13个赞

您可以使用,matplotlib但显示大量图像往往非常缓慢且效率低下。因此,如果您想更快、更轻松地完成此操作 - 我建议您使用我的名为IPyPlot的包:


import ipyplot


ipyplot.plot_images(images_list, max_images=20, img_width=150)

它支持以下格式的图像:

-string文件路径

-PIL.Image对象

-numpy.ndarray表示图像的对象


它能够在大约 70 毫秒内显示数百张图像


查看完整回答
反对 回复 2022-06-22
?
侃侃尔雅

TA贡献1801条经验 获得超16个赞

您可以使用创建多个子图plt.subplots。确定加载图像的列数和行数。就像是:


from os import listdir

import matplotlib.pyplot as plt


path = '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()


查看完整回答
反对 回复 2022-06-22
  • 2 回答
  • 0 关注
  • 250 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号