# 图片浏览器、
from ttkbootstrap import *
from pathlib import Path
class viewimg:
def __init__(self, imgname):
self.root = Window(size=(1800, 1600))
self.canvas = Canvas(self.root, bg='#312b39', highlightthickness=0)
self.canvas.pack(side=LEFT, anchor=CENTER, fill=BOTH, expand=YES)
self.imgpath = imgname
def run(self):
png = Image.open(self.imgpath)
filename = Path(self.imgpath)
name = filename.name
self.root.title(name)
image = ImageTk.PhotoImage(image=self.image_resize(image=png, event=None))
self.image1 = self.canvas.create_image(self.win_size()[0] // 2, self.win_size()[1] // 2, image=image)
self.root.bind(sequence='<Configure>', func=self.handler_adaptor(self.image_resize, image=png))
self.root.mainloop()
def image_resize(self,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 = self.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
self.canvas.delete(self.image1)
png = ImageTk.PhotoImage(images)
self.canvas.create_image(screen_width // 2, screen_height // 2, image=png)
def win_size(self):
"""
获取窗口大小,在没有bind事件时使用
"""
self.root.update()
width = self.root.winfo_width()
height = self.root.winfo_height()
return width, height
def handler_adaptor(self, fun, **kwds):
"""事件处理函数的适配器,相当于中介,那个event是从那里来的呢,我也纳闷,这也许就是python的伟大之处吧"""
return lambda event, fun=fun, kwds=kwds: fun(event, **kwds)
if __name__ == '__main__':
viewimg(r'D:\LABELAOI\Test\20961189Q0005-M4N0CV11E064168_20210422084634_L_main_CAM_basler.jpg').run()