我想在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>"))
但它不会输出图像,而是只显示两个图像的占位符:
我也尝试了以下几种方法:
s = """<table>
<tr>
<th><img src="%s"/></th>
<th><img src="%s"/></th>
</tr></table>"""%(img_A, img_B)
t=HTML(s)
display(t)
但是结果是一样的:
图像肯定在路径中,因为我通过在弹出窗口中显示它们进行了验证:
plt.imshow(img_A)
plt.imshow(img_B)
它们确实会出现在弹出窗口中。
如何在iPython中并排显示这两个图像?
发布于 2020-05-04 20:50:05
matplotlib
是一个非常好的绘图工具,但我发现在我只需要一种快速而简单的方式来显示更多图像的情况下,它非常笨重和缓慢。
为了解决这个问题,我使用了IPyPlot package
import ipyplot
ipyplot.plot_images(images_list, max_images=20, img_width=150)
您将得到一个类似于以下内容的图:
发布于 2021-04-30 07:25:02
这个简单的解决方案对我很有效:
from IPython.display import Video, Image, HTML, display
image_path1 = "/myfolder/my_img1.jpg"
image_path2 = "/myfolder/my_img2.jpg"
HTML(f"""
<div class="row">
<img src={image_path1} style="width:30%"> </img>
<img src={image_path1} style="width:53.2%"> </img>
</div>
""")
如果你有一张肖像和一张风景画,我会放入不同的宽度,但这取决于图像和纵横比。
发布于 2021-05-07 02:59:26
有点晚了,但我通过here发现了这件事
你可以通过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)
https://stackoverflow.com/questions/50559000
复制相似问题