Python 如何使用 Pillow 显示图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28139637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I display an image using Pillow?
提问by Ziph0n
I want to display a gif image using Pillow
我想使用 Pillow 显示 gif 图像
Here is my simple code:
这是我的简单代码:
from tkinter import *
from PIL import Image, ImageTk
import tkinter as Tk
image = Image.open("Puissance4.gif")
image.show()
But nothing happens...
但是什么都没有发生...
All help will be appreciated
所有帮助将不胜感激
Thanks!
谢谢!
采纳答案by unutbu
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.
PIL 提供了show
一种尝试检测您的操作系统并选择合适的查看器的方法。在 Unix 上,它尝试调用 imagemagick 命令display
或xv
. 在 Mac 上它使用open
,在 Windows 上它使用......别的东西。
If it can't find an appropriate viewer, ImageShow._viewers
will be an empty list.
如果找不到合适的查看器,ImageShow._viewers
将是一个空列表。
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:
在 Raspbian 上,您需要安装图像查看器,例如display
、xv
或fim
。(注意在网络上搜索会显示有很多可用的图像查看器。)然后你可以通过指定command
参数告诉 PIL 使用它:
image.show(command='fim')
To display an image in Tkinter, you could use something like:
要在 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()