1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > tkinter+图片等比例+根据窗口大小缩放

tkinter+图片等比例+根据窗口大小缩放

时间:2019-10-12 17:11:40

相关推荐

tkinter+图片等比例+根据窗口大小缩放

分享一个简单的图片浏览器,一个等比例缩放的算法

使用root.bind方法动态获取窗口大小,不需要重写窗口算法与重新加载图片了,省下了一大笔时间

from PIL import Image, ImageTkfrom 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='/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.heightraw_width, raw_height = image.size[0], image.size[1]max_width, max_height = raw_width, screen_heightmin_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 imageselse:global pngcanvas.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, heightdef 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()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。