Tkinter.Photoimage Doesn't Not Support Png Image

Tkinter.PhotoImage doesn't not support png image

tkinter only supports 3 file formats off the bat which are GIF, PGM, and PPM. You will either need to convert the files to .GIF then load them (Far easier, but as jonrsharpe said, nothing will work without converting the file first) or you can port your program to Python 2.7 and use the Python Imaging Library (PIL) and its tkinter extensions to use a PNG image.

A link that you might find useful: http://effbot.org/tkinterbook/photoimage.htm

tkinter 8.6 still doesn't support png files?

tkinter version 8.6 does support .png iamges.

The reason it wasn't working was the image I thought was a .png file was actually not a .png file. I downloaded the image originally as a .jpg file and I simply changed the extension. Changing extensions does not change the file type itself.

I grabbed other .png image and it worked fine.

Tkinter error: Couldn't recognize data in image file

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()

tkinter PhotoImage doesn't exist?

It doesn't matter very much that the image is in the same folder as the script, when you call the file like that without a path python assumes it's in the same folder that you were working on when you started the script. For example, if both the script and the image are in temp folder, and you started your script like this:

python temp/script.py

The interpreter doesn't realize that blueface.png is also in temp and looks for it in the folder that you were in, in this case the parent of temp

What you should do, is either use absolute paths, or use the __file__ to get the full script address first. For example:

photo = PhotoImage(file='/absolute/path/to/image/blueface.png')

Or using the current script's location to build the image's path:

import os
base_folder = os.path.dirname(__file__)
image_path = os.path.join(base_folder, 'blueface.png')
photo = PhotoImage(file=image_path)

Unable to put transparent PNG over a normal image Python Tkinter

You just need to use the canvas widget in Tkinter. Only the canvas widget supports transparency. This has to do with how Tkinter draws the display. As of right now, your code is just overlaying two images. Tkinter does not know how to compose them with transparency unless you use the canvas widget.

See the following code:

from tkinter import *
from PIL import Image


root = Tk()
root.title("Game")


frame = Frame(root)
frame.pack()


canvas = Canvas(frame, bg="black", width=700, height=400)
canvas.pack()


background = PhotoImage(file="background.png")
canvas.create_image(350,200,image=background)

character = PhotoImage(file="hero.png")
canvas.create_image(30,30,image=character)

root.mainloop()

All I did was download the images you provided. I did not modify the images. So, the bottom line is that you just need to use the canvas widget.

Sample Image

Voilà!


Note: The question asked is a duplicate of How do I make Tkinter support PNG transparency?



Related Topics



Leave a reply



Submit