Python 在上传到我的tkinter fyi窗口之前,是否有任何方法为用户提供调整其图像大小的选项

Python 在上传到我的tkinter fyi窗口之前,是否有任何方法为用户提供调整其图像大小的选项,python,image,tkinter,python-imaging-library,crop,Python,Image,Tkinter,Python Imaging Library,Crop,我正在进行一个类似Instagram配置文件页面的项目(tkinter python),并为用户提供了一个功能,可以选择需要上传到窗口中的图像,但我希望上传的图像大小应小于特定大小。在上传之前,我应该如何为用户提供调整其图像大小的选项 from tkinter import * from PIL import Image,ImageTk import tkinter.messagebox as tmsg #Profile Photo image=Image.open(f"{name}.jpg")

我正在进行一个类似Instagram配置文件页面的项目(tkinter python),并为用户提供了一个功能,可以选择需要上传到窗口中的图像,但我希望上传的图像大小应小于特定大小。在上传之前,我应该如何为用户提供调整其图像大小的选项

from tkinter import *
from PIL import Image,ImageTk
import tkinter.messagebox as tmsg
#Profile Photo
image=Image.open(f"{name}.jpg")
width,height=image.size
area=width*height
if area<=160012:
  photo=ImageTk.Photoimage(image)
  Label(image=photo).place(X=1000,y=2)
else:
  tmsg.showinfo('Crop image','WIDTH X HEIGHT must be smaller than 160012')
从tkinter导入*
从PIL导入图像,ImageTk
将tkinter.messagebox作为tmsg导入
#个人资料照片
image=image.open(f“{name}.jpg”)
宽度,高度=image.size
面积=宽度*高度

如果区域您可以为用户调整图像大小:

from tkinter import *
from PIL import Image,ImageTk

MAX_AREA = 160012

#Profile Photo
name = 'path/to/user/profile/image'
image = Image.open(f"{name}.jpg")
width, height = image.size
area = width * height

if area > MAX_AREA:
    # determine the resize ratio
    ratio = (MAX_AREA / area) ** 0.5
    # calculate the resized width and height
    new_w = int(width * ratio)
    new_h = int(height * ratio)
    # resize the image
    image = image.resize((new_w, new_h))
    print(f'Image is resized from {width}x{height} ({area}) to {new_w}x{new_h} ({new_w*new_h})')

root = Tk()
root.geometry('1000x600')
photo = ImageTk.PhotoImage(image)
Label(image=photo).place(x=1000, y=0, anchor='ne')
root.mainloop()

tkinter GUI窗口而不是tkinter fyi窗口为什么不为用户调整图像大小?@acw1668如何执行此操作在代码中,检查面积是否大于允许的面积,如果是,请调整图像大小,使其面积小于允许的面积。然后将图像指定给标签。