如何使用 Pillow 显示图像?

How can I display an image using Pillow?

我想使用 Pillow 显示 gif 图片

这是我的简单代码:

from tkinter import *
from PIL import Image, ImageTk 
import tkinter as Tk 

image = Image.open("Puissance4.gif") 
image.show()

但是什么也没发生...

我们将不胜感激

谢谢!

PIL 提供了一种 show 方法来尝试检测您的 OS 并选择一个 合适的观众。在 Unix 上,它会尝试调用 imagemagick 命令 displayxv。在 Mac 上它使用 open,在 Windows 上它使用...其他东西。

如果找不到合适的查看器,ImageShow._viewers 将是一个空列表。

在 Raspbian 上,您需要安装图像查看器,例如 displayxvfim。 (请注意,在网络上搜索会显示有许多图像查看器可用。)然后 您可以通过指定 command 参数告诉 PIL 使用它:

image.show(command='fim')

要在 Tkinter 中显示图像,您可以使用类似的东西:

from PIL import Image, ImageTk 
import tkinter as tk 

root = tk.Tk()
img = Image.open("image.gif")
tkimage = ImageTk.PhotoImage(img)
tk.Label(root, image=tkimage).pack()
root.mainloop()