如何在 ipython notebook 中显示 PIL Image [英] How to show PIL Image in ipython notebook
问题描述
这是我的代码
from PIL import Image
pil_im = Image.open('data/empire.jpg')
我想对其进行一些图像处理,然后将其显示在屏幕上.
我在 python notebook 中显示 PIL Image 时遇到问题.
I would like to do some image manipulation on it, and then show it on screen.
I am having problem with showing PIL Image in python notebook.
我试过了:
print pil_im
而且只是
pil_im
但两者都给我:
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=569x800 at 0x10ECA0710>
推荐答案
更新于 2021/11/17
当使用 PIL/Pillow 时,Jupyter Notebooks 现在有一个内置的 display
可以直接显示图像,没有额外的麻烦.
Updated 2021/11/17
When using PIL/Pillow, Jupyter Notebooks now have a display
built-in that will show the image directly, with no extra fuss.
display(pil_im)
如果图像只是单元格中的最后一行,Jupyter 也会显示该图像(自原始帖子以来已更改).感谢 @Dean 和 @Prabhat 指出这一点.
Jupyter will also show the image if it is simply the last line in a cell (this has changed since the original post). Thanks to answers from @Dean and @Prabhat for pointing this out.
您也可以使用 IPython 的 display
模块来加载图像.您可以从 文档一>.
You can also use IPython's display
module to load the image. You can read more from the doc.
from IPython.display import Image
pil_img = Image(filename='data/empire.jpg')
display(pil_img)
来自 PIL.Image 对象
由于OP的要求是使用PIL
,如果你想显示内联图像,你可以使用matplotlib.pyplot.imshow
和numpy.asarray代码>也像这样:
From PIL.Image Object
As OP's requirement is to use PIL
, if you want to show inline image, you can use matplotlib.pyplot.imshow
with numpy.asarray
like this too:
from matplotlib.pyplot import imshow
import numpy as np
from PIL import Image
%matplotlib inline
pil_im = Image.open('data/empire.jpg', 'r')
imshow(np.asarray(pil_im))
如果您只需要预览而不是内联,您可以像这样使用 show
:
If you only require a preview rather than an inline, you may just use show
like this:
pil_im = Image.open('data/empire.jpg', 'r')
pil_im.show()
这篇关于如何在 ipython notebook 中显示 PIL Image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!