8

I'm trying to add a background image to a canvas in Python. So far the code looks like this:

from Tkinter import *
from PIL import ImageTk,Image

... other stuffs

root=Tk()
canvasWidth=600
canvasHeight=400
self.canvas=Canvas(root,width=canvasWidth,height=canvasHeight)
backgroundImage=root.PhotoImage("D:\Documents\Background.png")
backgroundLabel=root.Label(parent,image=backgroundImage)
backgroundLabel.place(x=0,y=0,relWidth=1,relHeight=1)
self.canvas.pack()
root.mainloop()

It's returning an AttributeError: PhotoImage

0

4 Answers 4

10

PhotoImage is not an attribute of the Tk() instances (root). It is a class from Tkinter.

So, you must use:

backgroundImage = PhotoImage("D:\Documents\Background.gif")

Beware also Label is a class from Tkinter...

Edit:

Unfortunately, Tkinter.PhotoImage only works with gif files (and PPM). If you need to read png files you can use the PhotoImage (yes, same name) class in the ImageTk module from PIL.

So that, this will put your png image in the canvas:

from Tkinter import *
from PIL import ImageTk

canvas = Canvas(width = 200, height = 200, bg = 'blue')
canvas.pack(expand = YES, fill = BOTH)

image = ImageTk.PhotoImage(file = "C:/Python27/programas/zimages/gato.png")
canvas.create_image(10, 10, image = image, anchor = NW)

mainloop()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply! So... how do I load the image into the canvas?
Is it still possible to draw other stuffs over the image?
0

just change to :

    image = Image.open("~~~path.png")
    backgroundImage=ImageTk.PhotoImage(image)

believe me this will 100% work

1 Comment

What did you do? I did the same and it worked. Perhaps older version. Btw this is 5 year old thread.
0
from Tkinter import *
from PIL import ImageTk

canvas = Canvas(width = 200, height = 200, bg = 'blue')
canvas.pack(expand = YES, fill = BOTH)

image = ImageTk.PhotoImage(file = "C:/Python27/programas/zimages/gato.png")
canvas.create_image(10, 10, image = image, anchor = NW)

mainloop()

1 Comment

Welcome to StackOverflow. While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
0

You are using root to Attribute PhotoImage this is impossible! root is your window Tk() class so you cant Attribute it for PhotoImage because it's dosen't have it so you see AttributeError tkinter.Tk() and tkinter.PhotoImage is a different classes. and the same with tkinter.Label.

your code will not working with root.PhotoImage and root.Label. try to PhotoImage and Label directly.

to create a Label:

backgroundlabel = Label(parent, image=img)

if use any types of png or jpg and jpeg you can't draw it with just PhotoImage you will need PIL library

pip3 install PIL

when you have it use it like:

from PIL import Image, ImageTk # import image so you can append the path and imagetk so you can convert it as PhotoImage

now get your full path image like:

C:/.../img.png

Now use it :

path = "C:/.../img.png"         # Get the image full path
load = Image.open(path)         # load that path
img  = ImageTk.PhotoImage(load) # convert the load to PhotoImage

now you have your code work.

full code:

from Tkinter import *
from PIL import ImageTk,Image

... other stuffs

root            = Tk()
canvasWidth     = 600
canvasHeight    = 400
self.canvas     = Canvas(root,width=canvasWidth,height=canvasHeight)
path            = "D:\Documents\Background.png"  # Get the image full path
load            = Image.open(path)               # load that path
img             = ImageTk.PhotoImage(load)
backgroundLabel = Label(parent,image=img)
backgroundLabel .place(x=0,y=0,relWidth=1,relHeight=1)
self.canvas     .pack()
root            .mainloop()

Hope this will helpfull.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.