分享一个简单的图片浏览器,一个等比例缩放的算法
使用root.bind方法动态获取窗口大小,不需要重写窗口算法与重新加载图片了,省下了一大笔时间
from PIL import Image, ImageTk
from ttkbootstrap import *
def image_resize(event=None, image=None):
"""
等比例缩放图片
:param event: bind事件
:param image: Image.open(file_path) or Image.open(io.BytesIO(requests.get(url='https://pic2.zhimg.com/v2-2383d0d9ef77715b3cdd61b405dc8aec_1440w.jpg?source=172ae18b').content))
:return:
"""
if event is None:
screen_width, screen_height = win_size()
else:
screen_width, screen_height = event.width, event.height
raw_width, raw_height = image.size[0], image.size[1]
max_width, max_height = raw_width, screen_height
min_width = max(raw_width, max_width)
# 按照比例缩放
min_height = int(raw_height * min_width / raw_width)
# 第1次快速调整
while min_height > screen_height:
min_height = int(min_height * .9533)
# 第2次精确微调
while min_height < screen_height:
min_height += 1
# 按照比例缩放
min_width = int(raw_width * min_height / raw_height)
# 适应性调整
while min_width > screen_width:
min_width -= 1
# 按照比例缩放
min_height = int(raw_height * min_width / raw_width)
images = image.resize((min_width, min_height), Image.Resampling.LANCZOS)
if event is None:
return images
else:
global png
canvas.delete(image1)
png = ImageTk.PhotoImage(images)
canvas.create_image(screen_width // 2, screen_height // 2, image=png)
def win_size():
"""
获取窗口大小,在没有bind事件时使用
"""
root.update()
width = root.winfo_width()
height = root.winfo_height()
return width, height
def handler_adaptor(fun, **kwds):
"""事件处理函数的适配器,相当于中介,那个event是从那里来的呢,我也纳闷,这也许就是python的伟大之处吧"""
return lambda event, fun=fun, kwds=kwds: fun(event, **kwds)
root = Window(size=(1000, 800))
canvas = Canvas(root, bg='#312b39', highlightthickness=0)
canvas.pack(side=LEFT, anchor=CENTER, fill=BOTH, expand=YES)
png = Image.open('1.png')
image = ImageTk.PhotoImage(image=image_resize(image=png, event=None))
image1 = canvas.create_image(win_size()[0] // 2, win_size()[1] // 2, image=image)
root.bind(sequence='<Configure>', func=handler_adaptor(image_resize, image=png))
root.mainloop()