Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quickest way to preview a PIL image

I'm now working with PIL images in Python. What's the quickest way to preview a PIL image in the Python shell? Saving to a file and then opening it in my OS is pretty cumbersome.

like image 341
Ram Rachum Avatar asked Dec 07 '11 01:12

Ram Rachum


3 Answers

The Image class has a show(self, title=None, command=None) method, which you can use.

like image 89
Michael Aaron Safyan Avatar answered Nov 15 '22 09:11

Michael Aaron Safyan


After installing iPython and PyQT, you can display PIL images inline in ipython qtconsole after executing the following code:

# display_pil.py
# source: http://mail.scipy.org/pipermail/ipython-user/2012-March/009706.html
# by 'MinRK'
import Image
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)

Example of usage:

import Image
import display_pil
im = Image.open('test.png')
im

ipython qtconsole will display the loaded image inline.

like image 3
wolfskers Avatar answered Nov 15 '22 08:11

wolfskers


I would recommend to use iPython rather than the vanilla python interpreter. Then you can use matplotlib.pyplot.imshow function with ease, and with the Qt console you can even get the images plotted inline in the interpreter.

enter image description here

like image 2
wim Avatar answered Nov 15 '22 08:11

wim