在一个IPython笔记本电脑单元中显示多个图像?
如果我有多个图像(加载为NumPy阵列),如何在一个IPython笔记本单元中显示 我知道我可以使用在一个IPython笔记本电脑单元中显示多个图像?,ipython,ipython-notebook,Ipython,Ipython Notebook,如果我有多个图像(加载为NumPy阵列),如何在一个IPython笔记本单元中显示 我知道我可以使用plt.imshow(ima)来显示一个图像…但我希望一次显示多个图像 我试过: for ima in images: display(Image(ima)) 但我只得到一个断开的图像链接: 简短回答: 调用plt.figure() 对于图像中的ima: plt.图() plt.imshow(ima) 但是为了澄清与图像的混淆: IPython.display.Image用于显示图
plt.imshow(ima)
来显示一个图像…但我希望一次显示多个图像
我试过:
for ima in images:
display(Image(ima))
但我只得到一个断开的图像链接:
简短回答:
调用plt.figure()
对于图像中的ima:
plt.图()
plt.imshow(ima)
但是为了澄清与图像的混淆
:
IPython.display.Image
用于显示图像文件,而不是数组数据。如果要使用图像显示numpy数组,必须首先将其转换为文件格式(PIL最简单):
从io导入字节io
进口PIL
从IPython.display导入显示,图像
def显示img阵列(ima):
im=PIL.Image.fromarray(ima)
bio=BytesIO()
im.save(bio,format='png')
显示(图像(bio.getvalue(),format='png'))
对于图像中的ima:
显示img阵列(ima)
A说明这两种方法。通过使用显示和HTML功能,您可以在一个IPython笔记本单元中显示多个图像。您需要创建一组html img标记作为字符串,如下所示
from IPython.display import Image, HTML, display
from glob import glob
imagesList=''.join( ["<img style='width: 120px; margin: 0px; float: left; border: 1px solid black;' src='%s' />" % str(s)
for s in sorted(glob('yourimage*.png')) ])
display(HTML(imagesList))
从IPython.display导入图像、HTML、显示
从全局导入全局
imagesList=''.join([''%str)
对于排序中的s(glob('yourimage*.png'))])
显示(HTML(图像列表))
请参见中的使用示例
您可能需要刷新浏览器(shift+load)以查看新图像(如果已更新)
已从以前的单元格更改。这更容易操作:
from IPython.display import Image
from IPython.display import display
x = Image(filename='1.png')
y = Image(filename='2.png')
display(x, y)
不知何故,这与这个问题有关(因为我在试图解决这个问题时被引导到了这个答案),我可以通过在调用Image()
时简单地键入完整的文件路径来解决类似的问题。在我的例子中,我必须从存储在列表您的_文件夹中的不同文件夹路径中选择一个随机图像,并显示它们
import random, os
for i in range(len(your_folder)):
ra1 = "../"+your_folder[i]+"/"+random.choice(os.listdir(your_folder[i]))
image = Image(ra1)
display(image)
水平布局
简短回答
长话短说
基于@Michael answer基于@ChaosPredictor answer
from matplotlib.pyplot import figure, imshow, axis
from matplotlib.image import imread
def showImagesMatrix(list_of_files, col=10, wSize=5, hSize=5, mypath='.'):
fig = figure(figsize=(wSize, hSize))
number_of_files = len(list_of_files)
row = number_of_files / col
if (number_of_files % col != 0):
row += 1
for i in range(number_of_files):
a=fig.add_subplot(row, col, i + 1)
image = imread(mypath + '/' + list_of_files[i])
imshow(image, cmap='Greys_r')
axis('off')
然后
如果您不介意附加依赖项,这里有一个两行程序,使用:
为彩色图像设置multichannel=True
,为灰度图像设置multichannel=False
。此线程中的答案帮助我:
使用matplotlib的问题是显示图像的定义非常糟糕。我根据自己的需要修改了其中一个答案:
下面的代码显示jupyter笔记本中横向连接的图像。如果您愿意,请注意带有代码的注释行以保存图像
import numpy as np
import PIL
from IPython.display import display
list_im = ['Test1.jpg', 'Test2.jpg', 'Test3.jpg']
imgs = [ PIL.Image.open(i) for i in list_im ]
# pick the image which is the smallest, and resize the others to match it (can be arbitrary image shape here)
min_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]
imgs_comb = np.hstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )
# save that beautiful picture
imgs_comb = PIL.Image.fromarray( imgs_comb)
#imgs_comb.save( 'combo.jpg' )
display(imgs_comb)
您可以通过以下方式快速轻松地完成:
您将得到类似的绘图:
它使用的是IPython。在引擎盖下显示
和HTML
,它可以拍摄以下格式的图像:
string
文件路径
PIL.Image
对象
numpy.ndarray
表示图像的对象
如果使用imshow
是一个选项,那么显示500幅图像的numpy数组只需几秒钟,为什么不将其与子图
结合使用来创建一个图像数组呢?图像大小变化很大吗?我可以使用子图…但这在我刚刚进行实验时变得很棘手:我需要设置行数和列数,如果行数和列数太多,我需要增加图形大小,以确保它们各自的大小合理,我需要等待整个图形渲染,然后才能看到单独的图像(与生成图像时显示的图像相反),是否使用python 3.3?有东西写入控制台吗?没有-这是Py2.7。你是什么意思,“有东西写在控制台上吗”?你是说当我进入显示(图像(…)
?在这种情况下,图像看起来有点像src=“data:np.array([…])”
。链接笔记本中的微妙之处:如果您希望图像显示,您需要执行%matplotlib inline
。是否有什么好的解释说明为什么我必须plt.figure()
在每个映像之前?是否有方法避免将映像发送到磁盘?我确实有很多在内存中被多次操作的图像-在内存中。这正是我所需要的-我没有使用numpy数组或实际绘图,我只需要显示.png,这非常完美。谢谢这应该是公认的答案。如果你有一个图像数组,它可以工作:display(*images)
我喜欢这个,但是它显示的图像比plt.imshow()大得多。是否有可能以某种方式将此渲染保存到文件中?您应该使用os.path.join
或pathlib.path
以与操作系统无关的方式组合路径。这是一个很棒且更有用的答案!这是列举多个数字的好方法
from matplotlib.pyplot import figure, imshow, axis
from matplotlib.image import imread
mypath='.'
hSize = 5
wSize = 5
col = 4
def showImagesMatrix(list_of_files, col=10):
fig = figure( figsize=(wSize, hSize))
number_of_files = len(list_of_files)
row = number_of_files/col
if (number_of_files%col != 0):
row += 1
for i in range(number_of_files):
a=fig.add_subplot(row,col,i+1)
image = imread(mypath+'/'+list_of_files[i])
imshow(image,cmap='Greys_r')
axis('off')
showImagesMatrix(listOfImages,col)
from matplotlib.pyplot import figure, imshow, axis
from matplotlib.image import imread
def showImagesMatrix(list_of_files, col=10, wSize=5, hSize=5, mypath='.'):
fig = figure(figsize=(wSize, hSize))
number_of_files = len(list_of_files)
row = number_of_files / col
if (number_of_files % col != 0):
row += 1
for i in range(number_of_files):
a=fig.add_subplot(row, col, i + 1)
image = imread(mypath + '/' + list_of_files[i])
imshow(image, cmap='Greys_r')
axis('off')
from pathlib import Path
p = Path('.')
num_images = 30
list_of_image_paths = [str(x) for x in list(p.glob('../input/train/images/*'))[:num_images]]
showImagesMatrix(list_of_image_paths)
# or with named args
showImagesMatrix(list_of_image_paths, wSize=20, hSize=10, col=5)
from skimage.util import montage
plt.imshow(montage(np.array(images), multichannel=True))
import numpy as np
import PIL
from IPython.display import display
list_im = ['Test1.jpg', 'Test2.jpg', 'Test3.jpg']
imgs = [ PIL.Image.open(i) for i in list_im ]
# pick the image which is the smallest, and resize the others to match it (can be arbitrary image shape here)
min_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]
imgs_comb = np.hstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )
# save that beautiful picture
imgs_comb = PIL.Image.fromarray( imgs_comb)
#imgs_comb.save( 'combo.jpg' )
display(imgs_comb)
import ipyplot
ipyplot.plot_images(images_array, max_images=20, img_width=150)