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.
-
you could open it in a browser and press F5 every time you make a change. I don't think it's possible in python shell without a GUI for your program. – Serdalis Dec 7 '11 at 1:02
-
@Serdalis: As Michael Aaron Safyan showed below, it's indeed possible :) – Ram Rachum Dec 7 '11 at 11:23
-
definatly! that'll make my life easier too :D good question +1 – Serdalis Dec 7 '11 at 11:32
The Image
class has a show(self, title=None, command=None)
method, which you can use.
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.
-
+1 This works for Ipython notebooks as well, a good way to document REPL experiments with images. – Paulo Scardine Jun 10 '14 at 15:31
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.