我想在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中并排显示2张图像?
您可以尝试使用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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句