AttributeError when creating tkinter.PhotoImage object with PIL.ImageTk

user7043649 picture user7043649 · Oct 19, 2016 · Viewed 7.9k times · Source

I am trying to place an image resized with PIL in a tkinter.PhotoImage object.

import tkinter as tk # I use Python3
from PIL import Image, ImageTk

master = tk.Tk()
img =Image.open(file_name)
image_resized=img.resize((200,200))
photoimg=ImageTk.PhotoImage(image_resized)

However, when I later try to call

photoimg.put( "#000000", (0,0) )

I get an

AttributError: 'PhotoImage' object has no attribute 'put'

While this:

photoimg=tk.PhotoImage(file=file_name)
photoimg.put( "#000000", (0,0))

doesn't raise an error. What am I doing wrong?

Answer

DorElias picture DorElias · Oct 19, 2016

ImageTk.PhotoImage as in PIL.ImageTk.PhotoImage is not the same class as tk.PhotoImage (tkinter.PhotoImage) they just have the same name

here is ImageTk.PhotoImage docs: http://pillow.readthedocs.io/en/3.1.x/reference/ImageTk.html#PIL.ImageTk.PhotoImage as you can see there is no put method in it.

but ImageTk.PhotoImage do have it: http://epydoc.sourceforge.net/stdlib/Tkinter.PhotoImage-class.html


edit:

first link is now broken, this is the new link:

https://pillow.readthedocs.io/en/stable/reference/ImageTk.html?highlight=ImageTK#PIL.ImageTk.PhotoImage