【问题标题】:tkinker error: couldn´t recognize data in image filetkinter 错误:无法识别图像文件中的数据
【发布时间】:2020-09-27 08:26:38
【问题描述】:

我是 python 新手,对 tkinker 和图像有疑问。

我的错误是:

Traceback (most recent call last):
  File "D:/python/First Project/Weather app.py", line 12, in <module>
    background_image = tk.PhotoImage(file='landscape.jpg')
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3542, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3498, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "landscape.jpg"

相关代码为:

import tkinter as tk

root = tk.Tk()


background_image = tk.PhotoImage(file='D:\python\First Project\landscape.jpg')
background_label = tk.Label(root, image=background_image)
background_label.place(relwidth=1, relheight=1)


root.mainloop()

文件的结尾没有重命名,最初是 .jpg。 我也尝试用

编辑它
background_image = ImageTk.PhotoImage(Image.open('your.png'))

【问题讨论】:

  • 这个问题没有研究的迹象。这个网站上有很多问题都带有确切的错误消息(文件名除外)。

标签: python tkinter python-3.6


【解决方案1】:

PhotoImage 方法似乎不支持 JPG 文件。使用 JPG 文件时,我收到了同样的错误 (couldn't recognise data in "image.jpg")。使用 PNG 文件时,我没有收到任何此类错误。

请注意,您不能只更改 JPG 文件的文件扩展名来将其转换为 PNG 文件,因为 PNG 文件中的数据与 JPG 文件不同。您需要将 JPG 文件转换为 PNG(或 PhotoImage 方法支持的任何其他图像文件类型。)

【讨论】:

    猜你喜欢
    • 2018-05-01
    • 2015-04-28
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 2021-10-18
    • 2019-05-05
    • 2021-07-25
    相关资源
    最近更新 更多