user7043649 user7043649 - 1 month ago 11
Python Question

AttributeError when creating tkinter.PhotoImage object with PIL.ImageTk

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

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