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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:47:40  来源:igfitidea点击:

How can I display an image using Pillow?

pythontkinterpillow

提问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 showmethod which attempts to detect your OS and choose an appropriate viewer. On Unix it tries calling the imagemagick command displayor xv. On Macs it uses open, on Windows it uses... something else.

PIL 提供了show一种尝试检测您的操作系统并选择合适的查看器的方法。在 Unix 上,它尝试调用 imagemagick 命令displayxv. 在 Mac 上它使用open,在 Windows 上它使用......别的东西。

If it can't find an appropriate viewer, ImageShow._viewerswill be an empty list.

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

On Raspbian, you'll need to install an image viewer such as display, xvor 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 commandparameter:

在 Raspbian 上,您需要安装图像查看器,例如displayxvfim。(注意在网络上搜索会显示有很多可用的图像查看器。)然后你可以通过指定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()