[IPython-User] PIL Display Hooks
Fri Mar 16 14:03:54 CDT 2012
Thanks Adam and Min,
I did eventually decide to go with the imshow(numpy.asarray(im))
method since it is more compact. Min's display hook is a bit more
seamless so it would be nice to see something like this integrated and
active by default. Looks like you would have to detect the PIL import
before you try to insert the display hook though.
On Fri, Mar 16, 2012 at 12:39 AM, Adam Davis
> This also works:
> imshow(numpy.asarray(im)) #im is the PIL image
> See http://effbot.org/zone/pil-changes-116.htm for more on numpy <-> PIL conversion.
> -----Original Message-----
> From: email@example.com [mailto:firstname.lastname@example.org] On Behalf Of MinRK
> Sent: 16 March 2012 00:40
> To: Discussions about using IPython. http://ipython.org
> Subject: Re: [IPython-User] PIL Display Hooks
> This should do it:
> from IPython.core import display
> from io import BytesIO
> 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']
> png_formatter.for_type(Image.Image, display_pil_image)
> And now the default repr of PIL Images will be to render them as PNGs.
> It seems like this (or something like it -PIL's class structure is a bit funky) should perhaps be on by default.
> On Thu, Mar 15, 2012 at 14:21, Brian Granger <email@example.com> wrote:
>> It should definitely be possible to do this. Here is what I would advise:
>> * Look into generating raw JPEG/PNG data from an image. You could
>> write this to a file and then read it back in, or possible write
>> straight to a python bytes object.
>> * Then look into IPython's display protocol. The best resource on
>> this is the display_protocol.ipynb example notebook in
>> Once you have the raw JPEG, it shouldn't be more than a few lines of
>> code to hook into the IPython display system. Once that is done
>> images will "just work" in the Notebook.
>> On Thu, Mar 15, 2012 at 1:38 PM, Michael Hadmack <firstname.lastname@example.org> wrote:
>>> Does anyone know if it is possible to display PIL objects inline in a
>>> notebook? It would be nice it these displayed the same way as
>>> Matplotlib objects. I cannot find a way to get raw image data out of
>>> a PIL.Image object right now. Looks like you can only save to a file
>>> or file object. I can't remember right now how to create a file
>>> object that is just a memory buffer, don't know if this would do the
>>> IPython-User mailing list
>> Brian E. Granger
>> Cal Poly State University, San Luis Obispo email@example.com and
>> firstname.lastname@example.org _______________________________________________
>> IPython-User mailing list
> IPython-User mailing list
> IPython-User mailing list
More information about the IPython-User