如何在jupyter笔记本中并排显示图像

问题描述 投票:0回答:4

我使用了以下代码,但它垂直显示图像。我希望它们在 Jupyter Notebook 中并排显示。

display(Image.open(BytesIO(Item[iii][b'imgs'])))
display(Image.open(BytesIO(Item[jjj][b'imgs'])))

我尝试过使用此代码

display(HTML("<table><tr><td>display(Image.open(BytesIO(Item[jjj][b'imgs'])))) 

但是显示文字

display(Image.open(BytesIO(Item[jjj][b'imgs']))))

python jupyter-notebook
4个回答
12
投票

使用 ipywidgets 的布局功能你可以做到这一点

import ipywidgets as widgets
import IPython.display as display
## Read images from file (because this is binary, maybe you can find how to use ByteIO) but this is more easy
img1 = open('image1.jpeg', 'rb').read()
img2 = open('image2.jpeg', 'rb').read()
## Create image widgets. You can use layout of ipywidgets only with widgets.
## Set image variable, image format and dimension.
wi1 = widgets.Image(value=img1, format='png', width=300, height=400)
wi2 = widgets.Image(value=img2, format='png', width=300, height=400)
## Side by side thanks to HBox widgets
sidebyside = widgets.HBox([wi1, wi2])
## Finally, show.
display(sidebyside)

3
投票

并排显示两个图像

import matplotlib.pyplot as plt
img1 = plt.imread('<PATH_TO_IMG1>')
img2 = plt.imread('<PATH_TO_IMG2>')

NUM_ROWS = 1
IMGs_IN_ROW = 2
f, ax = plt.subplots(NUM_ROWS, IMGs_IN_ROW, figsize=(16,6))

ax[0].imshow(img1)
ax[1].imshow(img2)

ax[0].set_title('image 1')
ax[1].set_title('image 2')

title = 'side by side view of images'
f.suptitle(title, fontsize=16)
plt.tight_layout()
plt.show()


1
投票

您可以稍微修改上面的代码以显示图像网格。

import matplotlib.pyplot as plt
img1 = plt.imread('<PATH_TO_IMG1>')
img2 = plt.imread('<PATH_TO_IMG2>')
images = [img1, img2]*3

NUM_ROWS = 2
IMGs_IN_ROW = 3

f, ax_arr = plt.subplots(NUM_ROWS, IMGs_IN_ROW, figsize=(16,8))

for j, row in enumerate(ax_arr):
    for i, ax in enumerate(row):
        ax.imshow(images[j*IMGs_IN_ROW+i])
        ax.set_title(f'image {j*IMGs_IN_ROW+i+1}')

title = 'displaying images matrix'
f.suptitle(title, fontsize=16)
plt.show()  


0
投票

您可以使用子图。

import pylab 
pylab.subplot(1, 2, 1)
pylab.plot(range(10))
pylab.subplot(1, 2, 2)
pylab.plot(range(20))
© www.soinside.com 2019 - 2024. All rights reserved.