[IPython-User] PIL Display Hooks
Thu Mar 15 19:40:08 CDT 2012
This should do it:
from IPython.core import display
from io import BytesIO
"""displayhook function for PIL Images, rendered as PNG"""
b = BytesIO()
data = b.getvalue()
ip_img = display.Image(data=data, format='png', embed=True)
# register display func with PNG formatter:
png_formatter = get_ipython().display_formatter.formatters['image/png']
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 <firstname.lastname@example.org> 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 <email@example.com> 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
> firstname.lastname@example.org and email@example.com
> IPython-User mailing list
More information about the IPython-User