首页 > 解决方案 > 如何使用 tkinter 调整此图像的大小?

问题描述

我想使用 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)

标签: pythonimagetkinterresizeurllib

解决方案


您需要将 size 作为tuple. 所以它看起来像这样:

iimage = ImageTk.PhotoImage(img.resize((50,50), Image.ANTIALIAS))


推荐阅读