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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With