Python Tkinter 错误:无法识别图像文件中的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47357090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tkinter error: Couldn't recognize data in image file
提问by Igor234
I'm trying to put a jpg image to a tkinter canvas. tkinter gives me this error:
我正在尝试将 jpg 图像放到 tkinter 画布上。tkinter 给了我这个错误:
couldn't recognize data in image file
无法识别图像文件中的数据
I use the code from the documentation:
我使用文档中的代码:
canv = Canvas(root, width=80, height=80, bg='white')
canv.grid(row=2, column=3)
img = PhotoImage(file="bll.jpg")
canv.create_image(20,20, anchor=NW, image=img)
Same thing with png images. Even tried to put an image into a label widget, but got the same error. What's wrong?
与 png 图像相同。甚至尝试将图像放入标签小部件中,但得到了同样的错误。怎么了?
I am using Python 3 on Mac. Python file and image are in the same folder.
我在 Mac 上使用 Python 3。Python 文件和图像在同一个文件夹中。
采纳答案by bastelflp
Your code seems right, this is running for me on Windows 7 (Python 3.6):
您的代码似乎是正确的,这是在 Windows 7(Python 3.6)上为我运行的:
from tkinter import *
root = Tk()
canv = Canvas(root, width=80, height=80, bg='white')
canv.grid(row=2, column=3)
img = PhotoImage(file="bll.jpg")
canv.create_image(20,20, anchor=NW, image=img)
mainloop()
resulting in this tkinter GUI:
导致这个 tkinter GUI:
(imgur converted it to bll.png
but this is working for me as well.)
(imgur 将其转换为,bll.png
但这也适用于我。)
More options:
更多选择:
- This answermentions, tkinter is working only with
gif
images. Try using a.gif
image. - If this is not working, use
PIL
as stated in this answer.
Update:Solution with PIL
:
更新:解决方案PIL
:
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
canv = Canvas(root, width=80, height=80, bg='white')
canv.grid(row=2, column=3)
img = ImageTk.PhotoImage(Image.open("bll.jpg")) # PIL solution
canv.create_image(20, 20, anchor=NW, image=img)
mainloop()
回答by Nachiket
I was getting the same issue. I have windows and Python 3.6. So I found two solutions for this either you use/convert to .png
image (with the same function you have used):
我遇到了同样的问题。我有 Windows 和 Python 3.6。因此,我为此找到了两种解决方案,您可以使用/转换为.png
图像(使用与您使用的功能相同的功能):
photo = PhotoImage('xyz.png')
l = Label(image = photo)
l.pack()
or if you want to read .jpg
file only then use PIL library to read and display an image like this:
或者,如果您只想读取.jpg
文件,则使用 PIL 库来读取和显示这样的图像:
from PIL import ImageTk, Image
img = ImageTk.PhotoImage(Image.open("xyz.jpg"))
l=Label(image=img)
l.pack()
回答by SF12 Study
Install PIL/Pillow with:
安装 PIL/Pillow:
pip install Pillow
or:
或者:
sudo pip install pillow
from PIL import Image
from PIL import ImageTk
import tkinter
image = Image.open('bll.jpg')
image = image.resize((20, 20))
image = ImageTk.PhotoImage(image)
canv = Canvas(root, width=80, height=80, bg='white')
canv.grid(row=2, column=3)
img = PhotoImage(file=image)
Also using .PNG instead of .JPG is better for Tkinter.
同样使用 .PNG 而不是 .JPG 更适合 Tkinter。
回答by Rohan Shetty
Another alternative solution to the list...
列表的另一种替代解决方案......
filename = ImageTk.PhotoImage(Image.open('imagename.jpeg' ))
background_label = tk.Label(self.root, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
filename = ImageTk.PhotoImage(Image.open('imagename.jpeg' ))
background_label = tk.Label(self.root, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)