【发布时间】:2021-09-18 06:04:10
【问题描述】:
我想显示图像并根据标签大小设置图像大小。
img = Image.open("filepath")
img = img.resize((34, 26), Image.ANTIALIAS)
Label(master, image=img).place(x=20,y=10,width=80,height=80)
但是出现此错误消息。
Exception has occurred: TclError
image "<PIL.Image.Image image mode=RGBA size=34x26 at 0x20B9C0199D0>" doesn't exist
【问题讨论】:
-
你需要保存图片并用
ImageTk.PhotoImage(file=...)打开它 -
您需要先将图像转换为
PIL.ImageTk.PhotoImage对象。查看here 了解更多信息。 -
@Sujay 保存然后打开它是浪费时间。您可以直接将
PIL.Image.Image对象作为第一个参数传递给PIL.ImageTk.PhotoImage(...)。 -
我对实现还是一头雾水,能否给我看看正确的代码示例。