如何在ipython笔记本中显示PIL图像

时间:2021-02-03 00:26:20

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笔记本上显示PIL图像有问题。

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>

6 个解决方案

#1


68  

You can use IPython's Module: display to load the image. You can read more from the Doc.

您可以使用IPython的模块:display来加载映像。你可以从医生那里读到更多。

from IPython.display import Image 
Image(filename='data/empire.jpg')

updated

As OP's requirement is to use PIL, if you want to show inline image, you can use matplotlab.pyplot.imshow with numpy.asarray like this too:

由于OP的要求是使用PIL,如果要显示内联图像,可以使用matplotlab.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 show like this:

如果你只需要预览而不是内联,你可以使用如下的显示:

pil_im = Image.open('data/empire.jpg', 'r')
pil_im.show()

#2


13  

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 print after it

但是这必须是单元格中的最后一行,后面没有打印

#3


8  

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')))

#4


1  

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))

#5


1  

Based on other answers and my tries, best experience would be first installing, pillow and 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))

#6


0  

Use IPython display to render PIL images in notebook.

使用IPython显示在笔记本中呈现图像。

from PIL import Image // to load
from IPython.display import display // to display

pil_im = Image.open('data/empire.jpg')
display(pil_im)

#1


68  

You can use IPython's Module: display to load the image. You can read more from the Doc.

您可以使用IPython的模块:display来加载映像。你可以从医生那里读到更多。

from IPython.display import Image 
Image(filename='data/empire.jpg')

updated

As OP's requirement is to use PIL, if you want to show inline image, you can use matplotlab.pyplot.imshow with numpy.asarray like this too:

由于OP的要求是使用PIL,如果要显示内联图像,可以使用matplotlab.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 show like this:

如果你只需要预览而不是内联,你可以使用如下的显示:

pil_im = Image.open('data/empire.jpg', 'r')
pil_im.show()

#2


13  

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 print after it

但是这必须是单元格中的最后一行,后面没有打印

#3


8  

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')))

#4


1  

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))

#5


1  

Based on other answers and my tries, best experience would be first installing, pillow and 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))

#6


0  

Use IPython display to render PIL images in notebook.

使用IPython显示在笔记本中呈现图像。

from PIL import Image // to load
from IPython.display import display // to display

pil_im = Image.open('data/empire.jpg')
display(pil_im)