【问题标题】:how can I make png image to gif image?如何将 png 图像制作为 gif 图像?
【发布时间】:2017-09-22 05:12:17
【问题描述】:

我想使用 python3.6 将 png 图像更改为 gif 图像,我该怎么做?我正在用 tkinter 制作一个 GUI 程序,我必须将 png 更改为 gif 图像,以便我可以在我的程序上显示,请帮助

tk = Tk()
tk.configure(bg = 'white')

tk.title("SearchKeyWord V2.0")

canvas = Canvas(tk,width = 880, height = 550,bd = 0,highlightthickness = 0)

canvas.pack()
txt = Entry(tk)
txt.place(relx = .9,rely = .3,anchor = "c")
LOGO = PhotoImage(file = 'SKW-LOGO.gif')
button_1 = PhotoImage(file = 'button.gif')
button_2 = PhotoImage(file = 'button_news_1d.gif')
button_3 = PhotoImage(file = 'button_news_1w.gif')




button = tkinter.Button(tk,image = button_1,command = search)
button_news_1d = tkinter.Button(tk,image = button_2)
button_news_1w = tkinter.Button(tk,image = button_3)

canvas.create_image(0,0,anchor = NW, image = LOGO)
button.place(relx=.8, rely=.5, anchor="c")
button_news_1d.place(relx =.8,rely = .7, anchor = "c" )
button_news_1w.place(relx =.8,rely = .9, anchor = "c" )


tk.update()
tk.mainloop()

【问题讨论】:

标签: python python-3.x tkinter photoimage


【解决方案1】:

可以使用pillow的ImageTk直接加载图片为png格式。

import tkinter as tk
from PIL import Image, ImageTk
image_o = Image.open("photo.png")
image = ImageTk.PhotoImage(image_o)

#  Use image as to how you would in tkinter 

root = tk.Tk()
button = tk.Button(root, image = image)
button.pack()
root.mainloop()

与 tkinter 的 PhotoImage 不同,它限制您只能使用特定类型的图像,包括广泛使用的 gif 文件,ImageTk 的 PhotoImage 可以通过先加载枕头来采用任何 PIL 可识别的图像类型。

http://pillow.readthedocs.io/en/4.1.x/reference/ImageTk.html

如果你还没有枕头,请pip install pillow


添加:(感谢 Mike - SMT 的宝贵意见)

在 tk/tkl 8.6+ 的较新版本中,tk 原生支持 png 图像格式。

警告:某些 python 解释器不支持此版本的 tk。

https://www.tcl.tk/software/tcltk/8.6.html

【讨论】:

  • 嗯。那么我可以使用枕头将加载的图像保存为 gif 吗?
  • 你可以......但你不需要,除非你真的希望它是 gif,使用枕头将帮助你避免需要转换为 gif 并允许你直接在 tkinter 中使用 png /跨度>
  • 哦,我明白了,谢谢 :)
  • 补充说明最新版本的'tkinter 8.6+'原生支持'png'。
  • @Mike-SMT 感谢您添加此内容,我已将其纳入答案
猜你喜欢
  • 2011-02-01
  • 1970-01-01
  • 2023-04-04
  • 2021-06-01
  • 2017-09-15
  • 2011-05-11
  • 2013-08-21
  • 2019-10-20
  • 2013-06-17
相关资源
最近更新 更多