我现在正在使用Python处理PIL图像。 在Python shell中预览PIL图像的最快方法是什么?将其保存到文件中,然后在我的操作系统中打开它非常繁琐。
Image
类有一个show(self, title=None, command=None)
方法,您可以使用它。
show()
在Windows 7中无法工作,这是一个错误。 - Mark Ransom在安装了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
将内置显示已加载的图像。