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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:10:52  来源:igfitidea点击:

Tkinter error: Couldn't recognize data in image file

pythonmacospython-3.xcanvastkinter

提问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:

GUIwith this image as bll.jpg: image

图形用户界面将此图像作为bll.jpg图片

(imgur converted it to bll.pngbut this is working for me as well.)

(imgur 将其转换为,bll.png但这也适用于我。)



More options:

更多选择:

  • This answermentions, tkinter is working only with gifimages. Try using a .gifimage.
  • If this is not working, use PILas stated in this answer.
  • 这个答案提到,tkinter 仅适用于gif图像。尝试使用.gif图像。
  • 如果这不起作用,请PIL按照本答案中的说明使用。


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 .pngimage (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 .jpgfile 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)