How can I display an image using Pillow?

孤者浪人 提交于 2019-12-18 03:18:05

问题


I want to display a gif image using Pillow

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!


回答1:


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.

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

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')

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()


来源:https://stackoverflow.com/questions/28139637/how-can-i-display-an-image-using-pillow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!