如何将png图像制作成gif图像?

时间:2017-09-22 05:12:18

标签: python python-3.x tkinter photoimage

我想使用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()

1 个答案:

答案 0 :(得分:2)

您可以使用枕头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

相关问题