如何在Jupyter Notebook中并排显示图像

时间:2018-07-12 23:00:12

标签: python jupyter-notebook

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

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']))))

4 个答案:

答案 0 :(得分:1)

使用ipywidget的布局功能,您可以做到

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.display(sidebyside)

答案 1 :(得分:1)

并排显示两个图像

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()

enter image description here

答案 2 :(得分: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()  

enter image description here

答案 3 :(得分:0)

您可以使用子图。

import pylab 
pylab.subplot(1, 2, 1)
pylab.plot(range(10))
pylab.subplot(1, 2, 2)
pylab.plot(range(20))