在一个IPython Notebook的单元格中显示多个图像?

4 浏览
0 Comments

在一个IPython Notebook的单元格中显示多个图像?

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

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

我尝试了:

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

但我只得到一个损坏的图像链接:

enter image description here

0
0 Comments

在IPython Notebook中,有时候我们需要在一个cell中同时显示多张图片。下面的代码演示了如何实现这个功能:

from IPython.display import Image
from IPython.display import display
x = Image(filename='1.png') 
y = Image(filename='2.png') 
display(x, y)

这段代码中,我们首先导入了`Image`和`display`模块,然后分别创建了两个`Image`对象,分别对应两张图片`1.png`和`2.png`。最后,我们使用`display`函数将这两张图片同时显示在一个cell中。

下面的回答进一步说明了这种方法的适用范围:

> This should very much be the accepted answer. If you have an array of images, this works: `display(*images)`

这段回答中指出,如果我们有一个图片数组,我们可以直接将这个数组传给`display`函数,同样可以实现多张图片的显示。

然而,有人指出了这种方法的一个不足之处:

> I like this, but it displays images much larger than `plt.imshow()`. It doesn't seem possible to control the size.

这位用户认为,使用`display`函数显示的图片比使用`plt.imshow()`函数显示的图片要大很多,并且似乎没有办法控制显示的大小。

使用IPython Notebook中的`display`函数可以很方便地在一个cell中同时显示多张图片。不仅可以直接显示单个图片文件,还可以显示图片数组。然而,显示的图片大小似乎无法控制。

0
0 Comments

在IPython Notebook中,有时需要在一个单元格中显示多个图像。然而,当尝试使用plt.subplot()函数在单元格中显示多个图像时,可能会遇到一些问题。下面的代码是一个示例,展示了如何在一个IPython Notebook单元格中显示多个图像:

import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline
images = []
for img_path in glob.glob('images/*.jpg'):
    images.append(mpimg.imread(img_path))
plt.figure(figsize=(20,10))
columns = 5
for i, image in enumerate(images):
    plt.subplot(len(images) / columns + 1, columns, i + 1)
    plt.imshow(image)

这段代码首先使用glob模块和mpimg模块来读取文件夹中的图像文件,并将它们存储在一个列表中。然后,使用plt.figure()函数创建一个大图像,设置其大小为(20,10)。接下来,使用一个for循环和plt.subplot()函数来在大图像中显示每个图像。plt.subplot()函数接受三个参数:行数、列数和子图的索引。在这个示例中,行数是由图像数量除以列数加一得到的。然而,这里可能会遇到一个问题。如果图像数量不能被列数整除,行数将不会是一个整数,这将导致plt.subplot()函数报错。

为了解决这个问题,可以将plt.subplot()函数的第一个参数修改为int(len(images) / columns + 1),这样行数将会是一个整数。修改后的代码如下:

import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline
images = []
for img_path in glob.glob('images/*.jpg'):
    images.append(mpimg.imread(img_path))
plt.figure(figsize=(20,10))
columns = 5
for i, image in enumerate(images):
    plt.subplot(int(len(images) / columns + 1), columns, i + 1)
    plt.imshow(image)

这样,就可以在一个IPython Notebook单元格中显示多个图像了。这种方法非常有用,特别是在需要比较多个图像时。

0
0 Comments

问题原因:

在IPython Notebook中,如果想要在一个cell中显示多张图片,需要调用plt.figure()来创建新的图形。如果不调用plt.figure(),则会出现图片覆盖的问题。

解决方法:

1. 调用plt.figure()来创建新的图形:

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

这样就可以在一个cell中显示多张图片。

2. 使用IPython.display.Image来显示图片文件。如果想要使用Image来显示numpy数组,需要先将数组转换为文件格式(最简单的方法是使用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)

这样就可以在一个cell中显示numpy数组。

此外,还需要在notebook中添加"%matplotlib inline"命令,以便图片能够显示出来。

在IPython Notebook中,要在一个cell中显示多张图片,需要调用plt.figure()来创建新的图形。另外,如果想要使用Image来显示numpy数组,需要先将数组转换为文件格式。同时,还需要在notebook中添加"%matplotlib inline"命令,以便图片能够显示出来。

0