在一个 IPython Notebook 单元格中显示多个图像? [英] Display multiple images in one IPython Notebook cell?

查看:28
本文介绍了在一个 IPython Notebook 单元格中显示多个图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有多个图像(作为 NumPy 数组加载),如何在一个 IPython Notebook 单元格中显示?

If I have multiple images (loaded as NumPy arrays) how can I display the in one IPython Notebook cell?

我知道我可以使用 plt.imshow(ima) 来显示一个图像……但我想一次显示多个.

I know that I can use plt.imshow(ima) to display one image… but I want to show more than one at a time.

我试过了:

 for ima in images:
     display(Image(ima))

但我得到了一个损坏的图片链接:

But I just get a broken image link:

推荐答案

简答:

调用 plt.figure() 如果你想在一个单元格中创建多个图形:

call plt.figure() to create new figures if you want more than one in a cell:

for ima in images:
    plt.figure()
    plt.imshow(ima)

但是为了澄清与 Image 的混淆:

But to clarify the confusion with Image:

IPython.display.Image 用于显示图像文件,而不是数组数据.如果你想用 Image 显示 numpy 数组,你必须首先将它们转换为文件格式(使用 PIL 最简单):

IPython.display.Image is for displaying Image files, not array data. If you want to display numpy arrays with Image, you have to convert them to a file-format first (easiest with PIL):

from io import BytesIO
import PIL
from IPython.display import display, Image

def display_img_array(ima):
    im = PIL.Image.fromarray(ima)
    bio = BytesIO()
    im.save(bio, format='png')
    display(Image(bio.getvalue(), format='png'))

for ima in images:
    display_img_array(ima)

说明这两种方法的笔记本.

这篇关于在一个 IPython Notebook 单元格中显示多个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆