使用PIL预览图片的最快方法是什么?

9
我现在正在使用Python处理PIL图像。 在Python shell中预览PIL图像的最快方法是什么?将其保存到文件中,然后在我的操作系统中打开它非常繁琐。

你可以在浏览器中打开它,并在每次更改后按F5。我认为在Python shell中没有GUI程序是不可能的。 - Serdalis
@Serdalis:正如Michael Aaron Safyan在下面展示的那样,这确实是可能的 :) - Ram Rachum
肯定的!这也会让我的生活更轻松 :D 好问题 +1 - Serdalis
3个回答

4

Image类有一个show(self, title=None, command=None)方法,您可以使用它。


我发现show()在Windows 7中无法工作,这是一个错误。 - Mark Ransom

3

在安装了iPythonPyQT之后,在执行以下代码后,您可以在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 将内置显示已加载的图像。


+1 这也适用于Ipython笔记本,是一种用图像记录REPL实验的好方法。 - Paulo Scardine

2
我建议使用而不是普通的Python解释器。然后,您可以轻松使用函数,并且使用Qt控制台,甚至可以在解释器中内联绘制图像。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接