问题描述
我想在iPython中并排显示2个PNG图像.
I want to display 2 PNG images in iPython side by side.
我执行此操作的代码是:
My code to do this is:
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>"))
但是它不输出图像,而只显示2张图像的占位符:
But it doesn't output the images, and instead only displays placeholders for the 2 images:
我也尝试了以下方法:
s = """<table>
<tr>
<th><img src="%s"/></th>
<th><img src="%s"/></th>
</tr></table>"""%(img_A, img_B)
t=HTML(s)
display(t)
但结果是相同的:
这些图像肯定在路径中,因为我通过在弹出窗口中显示它们进行了验证:
The images are in the path for sure, because I verified by displaying them in a pop up:
plt.imshow(img_A)
plt.imshow(img_B)
,它们确实会出现在弹出窗口中.
and they do appear in the pop ups.
如何使2张图像在iPython中并排显示?
How do I make the 2 images appear side by side in iPython?
推荐答案
您可以尝试使用matplotlib
.您可以使用mpimg.imread
( 文档 ),那么您可以使用subplots
( 文档 ),并为图形创建两列,最后创建imshow
( 删除 )以显示图像.
You can try using matplotlib
. You can read image to numpy
array by using mpimg.imread
(documentation) from matplotlib, then you can use subplots
(documentation) and for creating two columns for figures and finally imshow
(documetation) to display images.
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);
这篇关于如何在Jupyter笔记本(iPython)中并排显示2张图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!