python - 如何使用 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)
解决方案
您需要将 size 作为tuple
. 所以它看起来像这样:
iimage = ImageTk.PhotoImage(img.resize((50,50), Image.ANTIALIAS))
推荐阅读
- python - 如何在 keras 功能 api 上指定 GRU 模型的输入形状?
- android - android studio上的颤振设置
- r - 为什么 ifelse 返回 NA 为假?
- javascript - 拦截箭头键并发送 Enter 代替
- npm - 私有存储库 GitLab 的版本控制
- php - 如何让我的 apache 网站只接受带有特定条目的主机头
- sql-server - 是否可以生成包含序列化通用(未知)C# 对象的 Kafka 消息,并通过推断消费者中的类型来反序列化它们?
- json - 如何在结构类型的结构中统一变量
- vscode-extensions - 有没有办法使用 Ctrl + Click 来触发命令作为 VS Code 扩展的一部分?
- c# - 在客户端机器上使用 CefSharp 的 Javascript 注册对象问题