预览PIL图像的最快方法
我现在在用Python处理PIL图像。有没有什么简单快捷的方法可以在Python的命令行中预览PIL图像?把图像保存到文件里,然后再在我的操作系统中打开,感觉太麻烦了。
3 个回答
2
我建议你使用 iPython
,而不是普通的 Python 解释器。这样你就可以轻松使用 matplotlib.pyplot.imshow
这个函数,而且在 Qt 控制台中,你甚至可以直接在解释器里看到绘制的图像。
3
在安装了 iPython 和 PyQT 之后,你可以在 ipython qtconsole
中直接显示PIL图片,只需执行以下代码:
# 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)
使用示例:
import Image
import display_pil
im = Image.open('test.png')
im
ipython qtconsole
会在界面中直接显示加载的图片。
4
Image
类里面有一个叫做show(self, title=None, command=None)
的方法,你可以使用这个方法。