Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 553
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 4
Kenil Vasani
Asked: December 14, 20202020-12-14T21:04:45+00:00 2020-12-14T21:04:45+00:00In: macos, Python

Tkinter error: Couldn’t recognize data in image file

  • 4

I’m trying to put a jpg image to a tkinter canvas. tkinter gives me this error:

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?

I am using Python 3 on Mac. Python file and image are in the same folder.

canvasmacospythonpython-3.xtkinter
  • 1 1 Answer
  • 13 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-14T21:02:23+00:00Added an answer on December 14, 2020 at 9:02 pm

      Your code seems right, this is running for me on 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:

      GUI with this image as bll.jpg: image

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


      More options:

      • This answer mentions, 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:

      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()
      
      • 5
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      Error: PostCSS plugin tailwindcss requires PostCSS 8

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com