我想使用urllib和Tkinter来调整图像的大小。但是,我不知道如何调整图像的大小。这不是我的代码,但我想看看如何使用这些代码来完成它。
from io import BytesIO
import urllib
import urllib.request
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
root.geometry("500x500")
url = "https://www.thoughtco.com/thmb/O8fvqeXnAtOZ_L4eQ-aCRFKou_I=/768x0/filters:no_upscale():max_bytes(150000):strip_icc()/atlantic-bottlenose-dolphin--jumping-high-during-a-dolphin-training-demonstration-154724035-59ce93949abed50011352530.jpg"
with urllib.request.urlopen(url) as u:
raw_data = u.read()
img = Image.open(BytesIO(raw_data))
iimage = ImageTk.PhotoImage(img)
label = tk.Label(image=iimage)
label.pack()
root.mainloop()当我试图调整它的尺寸时:
iimage = ImageTk.PhotoImage(img.resize(50,50, Image.ANTIALIAS))它给了我这个错误:
ValueError: Unknown resampling filter (50). Use Image.NEAREST (0), Image.LANCZOS (1), Image.BILINEAR (2), Image.BICUBIC (3), Image.BOX (4) or Image.HAMMING (5)
发布于 2021-07-27 15:37:00
您需要将大小作为tuple传递。所以看起来是这样:
iimage = ImageTk.PhotoImage(img.resize((50,50), Image.ANTIALIAS))
发布于 2021-07-27 19:47:10
我建议您使用PhotoImage和SubSample,例如:
Example = PhotoImage(file='photo.png')然后再进行次抽样,例如:
Example = Example.subsample(2, 2)“守则”共计:
Example = PhotoImage(file='Photo.png')
Example = Example.subsample(3, 3)总结:您不需要导入任何特殊类型的Libarys,并且可以在任何python3 IDE的上使用
https://stackoverflow.com/questions/68547488
复制相似问题