如何在 ipython notebook 中显示 PIL Image
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26649716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show PIL Image in ipython notebook
提问by WebOrCode
This is my code
这是我的代码
from PIL import Image
pil_im = Image.open('data/empire.jpg')
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.
我想对其进行一些图像处理,然后将其显示在屏幕上。
我在 python notebook 中显示 PIL Image 时遇到问题。
I have tried:
我试过了:
print pil_im
And just
而只是
pil_im
But both just give me:
但两者都给我:
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=569x800 at 0x10ECA0710>
采纳答案by Anzel
You can use IPython's Module: displayto load the image. You can read more from the Doc.
您可以使用 IPythonModule: display加载图像。您可以从Doc阅读更多内容。
from IPython.display import Image
pil_img = Image(filename='data/empire.jpg')
display(pil_img)
updated
更新
As OP's requirement is to use PIL, if you want to show inline image, you can use matplotlib.pyplot.imshowwith numpy.asarraylike this too:
作为OP的要求是使用PIL,如果你想显示内嵌图像,你可以用matplotlib.pyplot.imshow与numpy.asarray这样太:
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))
If you only require a preview rather than an inline, you may just use showlike this:
如果您只需要预览而不是内联,您可以这样使用show:
pil_im = Image.open('data/empire.jpg', 'r')
pil_im.show()
回答by Rob
If you are using the pylab extension, you could convert the image to a numpy array and use matplotlib's imshow.
如果您使用的是 pylab 扩展,则可以将图像转换为 numpy 数组并使用 matplotlib 的 imshow。
%pylab # only if not started with the --pylab option
imshow(array(pil_im))
EDIT: As mentioned in the comments, the pylab module is deprecated, so use the matplotlib magic instead and import the function explicitly:
编辑:如评论中所述,不推荐使用 pylab 模块,因此请改用 matplotlib 魔法并显式导入函数:
%matplotlib
from matplotlib.pyplot import imshow
imshow(array(pil_im))
回答by WebOrCode
I found that this is working
我发现这是有效的
# source: http://nbviewer.ipython.org/gist/deeplook/5162445
from io import BytesIO
from IPython import display
from PIL import Image
def display_pil_image(im):
"""Displayhook function for PIL Images, rendered as PNG."""
b = BytesIO()
im.save(b, format='png')
data = b.getvalue()
ip_img = display.Image(data=data, format='png', embed=True)
return ip_img._repr_png_()
# register display func with PNG formatter:
png_formatter = get_ipython().display_formatter.formatters['image/png']
dpi = png_formatter.for_type(Image.Image, display_pil_image)
After this I can just do:
在此之后,我可以这样做:
pil_im
But this must be last line in cell, with no printafter it
但这必须是单元格中的最后一行,print后面没有
回答by gaziya
case python3
案例python3
from PIL import Image
from IPython.display import HTML
from io import BytesIO
from base64 import b64encode
pil_im = Image.open('data/empire.jpg')
b = BytesIO()
pil_im.save(b, format='png')
HTML("<img src='data:image/png;base64,{0}'/>".format(b64encode(b.getvalue()).decode('utf-8')))
回答by Ebrahim Byagowi
Based on other answers and my tries, best experience would be first installing, pillowand scipy, then using the following starting code on your jupyter notebook:
根据其他答案和我的尝试,最佳体验是首先安装、枕头和 scipy,然后在您的 jupyter 笔记本上使用以下起始代码:
%matplotlib inline
from matplotlib.pyplot import imshow
from scipy.misc import imread
imshow(imread('image.jpg', 1))
回答by sathyz
Use IPython display to render PIL images in a notebook.
使用 IPython 显示在笔记本中渲染 PIL 图像。
from PIL import Image # to load images
from IPython.display import display # to display images
pil_im = Image.open('path/to/image.jpg')
display(pil_im)
回答by M . Franklin
You can open an image using the Image class from the package PIL and display it with plt.imshow directly.
您可以使用 PIL 包中的 Image 类打开图像,并直接使用 plt.imshow 显示它。
# First import libraries.
from PIL import Image
import matplotlib.pyplot as plt
# The folliwing line is useful in Jupyter notebook
%matplotlib inline
# Open your file image using the path
img = Image.open(<path_to_image>)
# Since plt knows how to handle instance of the Image class, just input your loaded image to imshow method
plt.imshow(img)
回答by Punnerud
A cleaner Python3 version that use standard numpy, matplotlib and PIL. Merging the answer for opening from URL.
使用标准 numpy、matplotlib 和 PIL 的更干净的 Python3 版本。合并从 URL 打开的答案。
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
pil_im = Image.open('image.jpg')
## Uncomment to open from URL
#import requests
#r = requests.get('https://www.vegvesen.no/public/webkamera/kamera?id=131206')
#pil_im = Image.open(BytesIO(r.content))
im_array = np.asarray(pil_im)
plt.imshow(im_array)
plt.show()
回答by Prabhat
much simpler in jupyter using pillow.
在 jupyter 中使用枕头要简单得多。
from PIL import Image
image0=Image.open('image.png')
image0
回答by Dr.-Ing. Hiesik KIm
I suggest following installation by no image show img.show() (from PIL import Image)
我建议通过 no image show img.show() (from PIL import Image) 安装
$ sudo apt-get install imagemagick
$ sudo apt-get install imagemagick

