使用 PIL.ImageTk 创建 tkinter.PhotoImage 对象时出现 AttributeError

use*_*649 0 python tkinter python-imaging-library photoimage

我试图在 tkinter.PhotoImage 对象中放置一个用 PIL 调整大小的图像。

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)
Run Code Online (Sandbox Code Playgroud)

但是,当我后来尝试打电话时

photoimg.put( "#000000", (0,0) )
Run Code Online (Sandbox Code Playgroud)

我得到一个

AttributError: 'PhotoImage' object has no attribute 'put'
Run Code Online (Sandbox Code Playgroud)

虽然这个:

photoimg=tk.PhotoImage(file=file_name)
photoimg.put( "#000000", (0,0))
Run Code Online (Sandbox Code Playgroud)

不会引发错误。我究竟做错了什么?

Dor*_*ias 6

ImageTk.PhotoImage因为 inPIL.ImageTk.PhotoImagetk.PhotoImage( tkinter.PhotoImage)不是同一个类,它们只是具有相同的名称

这是 ImageTk.PhotoImage 文档:http ://pillow.readthedocs.io/en/3.1.x/reference/ImageTk.html#PIL.ImageTk.PhotoImage 正如您所见,其中没有 put 方法。

ImageTk.PhotoImage有它:http : //epydoc.sourceforge.net/stdlib/Tkinter.PhotoImage-class.html


编辑:

第一个链接现已损坏,这是新链接:

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