Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将png图像转换为gif图像?_Python_Python 3.x_Tkinter_Photoimage - Fatal编程技术网

Python 如何将png图像转换为gif图像?

Python 如何将png图像转换为gif图像?,python,python-3.x,tkinter,photoimage,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()

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

您可以使用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图像类型

如果您还没有枕头,请安装枕头


添加了:(感谢Mike-SMT的宝贵评论)

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

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


hmm。那么我可以使用枕头将加载的图像保存为gif吗?您可以。。。但是你不需要,除非你真的希望它是gif,使用枕头可以帮助你避免转换成gif,并允许你在tkinteroh中直接使用png。我知道了,谢谢:)补充说明,tkinter 8.6+的最新版本本机支持“png”。@Mike SMT感谢你补充了这一点,我在回答中加入了PIL或枕头