3

我目前正在尝试获取一张我拥有的图片,甚至是我可以链接到的网络上的一张图片,以从 if 语句中的 python 代码输出。

这是代码:

if c >= 50:
    print '\nYou have been mauled by a bear\n'
    # I want to output a picture of a bear here
    quit()

我有一个 c 的计数器,如果它达到 50,我希望它打印出你被熊伤害了,然后在屏幕上弹出一个熊,熊的图像是否只是同一个文件夹中的一个文件,或者如果它链接到我托管熊图像的网页。

这可能吗?

4

2 回答 2

2

在 Mac 上:

from subprocess import call
call(["open", "hi.jpg"])

如果您将“打开”替换为用于打开图像的任何程序,这也应该适用于其他系统。

于 2012-11-28T04:40:54.213 回答
2

有很多方法可以做到这一点,我会使用PIL的图像模块

from PIL import Image
im = Image.open("bear.png")
im.show()

其他方法是使用以下方法之一:wxWindows、pyQt、pyGTK 或 Tkinter

于 2012-11-28T04:42:44.513 回答