如何使用 Pillow 显示图像? [英] How can I display an image using Pillow?
问题描述
我想使用 Pillow 显示 gif 图像
I want to display a gif image using Pillow
这是我的简单代码:
from tkinter import *
from PIL import Image, ImageTk
import tkinter as Tk
image = Image.open("Puissance4.gif")
image.show()
但是什么都没发生...
But nothing happens...
感谢所有帮助
谢谢!
推荐答案
PIL 提供了一个 show
方法,该方法尝试检测您的操作系统并选择一个合适的观众.在 Unix 上,它尝试调用 imagemagick 命令 display
或 xv
.在 Mac 上它使用 open
,在 Windows 上它使用......别的东西.
PIL provides a show
method which attempts to detect your OS and choose an
appropriate viewer. On Unix it tries calling the imagemagick command display
or xv
. On Macs it uses open
, on Windows it uses... something else.
如果找不到合适的查看器,ImageShow._viewers
将是一个空列表.
If it can't find an appropriate viewer, ImageShow._viewers
will be an empty list.
在 Raspbian 上,您需要安装图像查看器,例如 display
、xv
或 fim
.(请注意,在网络上搜索会显示有许多可用的图像查看器.)然后您可以通过指定 command
参数告诉 PIL 使用它:
On Raspbian, you'll need to install an image viewer such as display
, xv
or fim
. (Note a search on the web will show that there are many image viewers available.) Then
you can tell PIL to use it by specifying the command
parameter:
image.show(command='fim')
<小时>
要在 Tkinter 中显示图像,您可以使用以下内容:
To display an image in Tkinter, you could use something like:
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()
这篇关于如何使用 Pillow 显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!