【问题标题】:How can I display an image using Pillow?如何使用 Pillow 显示图像?
【发布时间】:2015-03-24 05:55:57
【问题描述】:

我想使用 Pillow 显示 gif 图像

这是我的简单代码:

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

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

但是什么也没发生……

所有帮助将不胜感激

谢谢!

【问题讨论】:

  • from PIL import ImageShow 是什么意思; ImageShow._viewers返回?
  • 返回[] 谢谢!
  • 你使用的是什么操作系统?
  • 我在树莓派上使用 Raspbian
  • 您使用什么命令从命令行显示图像?

标签: python tkinter pillow


【解决方案1】:

PIL 提供了一个show 方法,它会尝试检测您的操作系统并选择一个 合适的观众。在 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()

【讨论】:

猜你喜欢
  • 2020-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-21
  • 1970-01-01
  • 2019-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多