【问题标题】:Tkinter error: Couldn't recognize data in image fileTkinter 错误:无法识别图像文件中的数据
【发布时间】:2018-05-01 14:39:27
【问题描述】:

我正在尝试将 jpg 图像放到 tkinter 画布上。 tkinter 给了我这个错误:

无法识别图像文件中的数据

我使用文档中的代码:

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)

png 图像也是如此。甚至尝试将图像放入标签小部件,但得到了同样的错误。怎么了?

我在 Mac 上使用 Python 3。 Python 文件和图像在同一个文件夹中。

【问题讨论】:

  • 大多数版本的 tkinter 不支持 jpg 或 png

标签: python macos python-3.x canvas tkinter


【解决方案1】:

您的代码似乎正确,这是在 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()

导致这个 tkinter GUI:

,此图像为bll.jpg

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


更多选项:

  • This answer 提到,tkinter 仅适用于 gif 图像。尝试使用.gif 图片。
  • 如果这不起作用,请按照this answer 中的说明使用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()

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。我有 Windows 和 Python 3.6。因此,我找到了两种解决方案,您可以使用/转换为.png 图像(使用您使用的相同功能):

    photo = PhotoImage('xyz.png')
    l = Label(image = photo)
    l.pack()
    

    或者如果您只想读取 .jpg 文件,则使用 PIL 库读取并显示如下图像:

    from PIL import ImageTk, Image
    img = ImageTk.PhotoImage(Image.open("xyz.jpg"))  
    l=Label(image=img)
    l.pack()
    

    【讨论】:

    • 这有帮助。虽然我建议添加 window 和 mainloop() 来实际显示图像。 window = Tk() 结尾是:window.mainloop()
    【解决方案3】:

    使用以下方式安装 PIL/Pillow:

    pip install Pillow
    

    或:

    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)
    

    同时使用 .PNG 而不是 .JPG 更适合 Tkinter。

    【讨论】:

      【解决方案4】:

      另一种替代解决方案:

      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)
      

      【讨论】:

        【解决方案5】:

        为 Python 安装 OpenCV 包:

        pip install opencv-python
        

        然后试试这段代码:

        import cv2
        Img = cv2.imread("xxxxx.png") 
        cv2.imwrite("xxxxx.png",img) 
        # Your code goes here!
        

        【讨论】:

          【解决方案6】:

          由于文件路径中的relative file pathnon-English 字符,可能会发生此错误。所以我做了这个函数,它在 Windows 和任何类型的文件路径中都非常好用:

          def loadrelimages(relativepath):
              from PIL import ImageTk, Image
              import os
              directory_path = os.path.dirname(__file__)
              file_path = os.path.join(directory_path, relativepath)
              img = ImageTk.PhotoImage(Image.open(file_path.replace('\\', "/")))  
              return img
          

          例如,加载与此代码在同一目录下的photo_2021-08-16_18-44-28.jpg

          from tkinter import *
          import os
          
          def loadrelimages(relativepath):
              from PIL import ImageTk, Image
              import os
              directory_path = os.path.dirname(__file__)
              file_path = os.path.join(directory_path, relativepath)
              img = ImageTk.PhotoImage(Image.open(file_path.replace('\\',"/")))  
              return img
          
          root = Tk()
          
          canvas = Canvas(root, width=500, height=500)
          canvas.pack()
          
          loadedimage=loadrelimages('photo_2021-08-16_18-44-28.jpg')
          canvas.create_image(250, 250, image=loadedimage)
          
          root.mainloop()
          

          【讨论】:

          • 如果问题与文件路径有关,则 OP 会收到不同的错误。问题中的错误不是因为找不到文件。
          【解决方案7】:

          使用 Python 包 Pillow 对我有用。

          只需执行 pip install 枕头即可。

          然后使用 ImageTk.PhotoImage(Image.open("Your image path goes here...")) 代替 tkinter.PhotoImage()

          【讨论】:

            【解决方案8】:
            # in terminal pip install pillow
            
            from tkinter import *
            from PIL import Image, ImageTk
            
            a_root = Tk()
            
            # WINDOW SIZING
            
            a_root.geometry("600x400")
            a_root.minsize(400, 400)
            a_root.maxsize(800, 800)
            
            # for PNG IMG
            # photo = PhotoImage(file="mark1.png")
            # for JPG IMG
            
            image = Image.open("mark.jpg")
            photo = ImageTk.PhotoImage(image)
            
            img_label = Label(image=photo)
            img_label.pack()
            
            a_root.mainloop()
            

            【讨论】:

            • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
            • 那些不是 Python cmets
            【解决方案9】:

            我遇到了同样的错误。您只需要使用 PNG 文件而不是 jpeg/jpg。 如果你没有 png 版本,你可以在这里转换你的图片 ---> https://cloudconvert.com/

            from tkinter import *
            
            
            root = Tk()
            
            root.geometry("1920x1080")
            photo = PhotoImage(file="Akatsuki.png")
            
            
            label = Label(image=photo)
            label.pack()
            
            root.mainloop()
            

            【讨论】:

              猜你喜欢
              • 1970-01-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
              相关资源
              最近更新 更多