首页 > 解决方案 > 如何在 jupyter notebook 中并排显示图像

问题描述

我使用了以下代码,但它垂直显示图像。我希望它们在 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']))))

标签: pythonjupyter-notebook

解决方案


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

推荐阅读