Kri*_*673 5 python image ipython jupyter-notebook
我想在iPython中并排显示2个PNG图像。
我要做的代码是:
from IPython.display import Image, HTML, display
img_A = '\path\to\img_A.png'
img_B = '\path\to\img_B.png'
display(HTML("<table><tr><td><img src=img_A></td><td><img src=img_B></td></tr></table>"))
Run Code Online (Sandbox Code Playgroud)
但是它不输出图像,而只显示两个图像的占位符:
我也尝试了以下方法:
s = """<table>
<tr>
<th><img src="%s"/></th>
<th><img src="%s"/></th>
</tr></table>"""%(img_A, img_B)
t=HTML(s)
display(t)
Run Code Online (Sandbox Code Playgroud)
但是结果是一样的:
这些图像肯定在路径中,因为我通过在弹出窗口中显示它们进行了验证:
plt.imshow(img_A)
plt.imshow(img_B)
Run Code Online (Sandbox Code Playgroud)
并且它们确实出现在弹出窗口中。
如何在iPython中并排显示2张图像?
matplotlib是一个非常好的绘图工具,但我发现它对于我只需要一种快速简便的方法来显示更多图像的场景来说非常沉重和缓慢。
为了解决这个问题,我使用IPyPlot 包:
import ipyplot
ipyplot.plot_images(images_list, max_images=20, img_width=150)
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用matplotlib。您可以numpy通过使用matplotlib中的mpimg.imread(documentation)来读取图像到阵列,然后可以使用subplots(documentation)并为图形创建两列,最后imshow(documetation)显示图像。
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib import rcParams
%matplotlib inline
# figure size in inches optional
rcParams['figure.figsize'] = 11 ,8
# read images
img_A = mpimg.imread('\path\to\img_A.png')
img_B = mpimg.imread('\path\to\img_B.png')
# display images
fig, ax = plt.subplots(1,2)
ax[0].imshow(img_A);
ax[1].imshow(img_B);
Run Code Online (Sandbox Code Playgroud)
有点晚了,但我在这里发现了这个
您可以通过 Hbox 来完成。Hbox 是一个特殊的容器,您可以在其中添加小部件。它旨在提供一种有效的方法来在给定空间中的项目之间布局、对齐和分配空间。虽然定义这么多元素来显示 2 个图像有点麻烦,但您可以添加更多功能,例如滑块、下拉菜单以及按钮,使您的 Jupiter 笔记本更具交互性。
import IPython.display as display
import ipywidgets as widgets
img1=open('path_to_image','rb').read()
wi1 = widgets.Image(value=img1, format='jpg', width=300, height=400)
img2=open('path_to_image','rb').read()
wi2 = widgets.Image(value=img2, format='jpg', width=300, height=400)
a=[wi1,wi2]
wid=widgets.HBox(a)
display.display(wid)
Run Code Online (Sandbox Code Playgroud)