如何在 Tkinter 中调整从网站上提取的照片的大小 Window

How do you Resize a Photo Pulled from a Website in a Tkinter Window

我正在尝试使用 tkinter window 来显示可以选择的几张照片。我不断遇到错误 "PhotoImage' object has no attribute 'resize",但我查找的所有 guides/forums 都有相同的代码。如何调整从网站上提取的图像的大小?

url="https://cdn.fileviewerplus.com/img/icon/256/cr2-52.png"
u=urllib.request.urlopen(url)
raw_data=u.read()
u.close()
b64_data = base64.encodestring(raw_data)
photo = tk.PhotoImage(data=b64_data)
image=photo.resize((100,100),Image.ANTIALIAS)

您似乎正在尝试使用枕头内置的方法。您可以将文件导入为枕头图像文件,然后使用 resize。然后在你的 tkinter 环境中使用它。

from PIL import Image, ImageTk

url = 'https://cdn.fileviewerplus.com/img/icon/256/cr2-52.png'
u = urllib.request.urlopen(url)
img = Image.open(u)
img = img.resize((100, 100), Image.ANTIALIAS)
image = ImageTk.PhotoImage(img)

此外,由于@acw1668 被抓到,你应该使用ImageTk from pillow here。