# 以鼠标位置进行图像非中心zoom import tkinter as tk from PIL import Image, ImageTk image_show_width = 800 image_show_height = 600 window_width = image_show_width window_height = image_show_height+120 root = tk.Tk() root.geometry(f"{window_width}x{window_height}+800+50") # 创建标签用于显示图片 label_imgshow = tk.Label(root, text="拖拽图片到这里", width=image_show_width, height=image_show_height, bg="lightgrey") label_imgshow.place(x = 0, y=0, width=image_show_width, height=image_show_height, bordermode = 'outside') original_image = Image.open('test.png') photo_image = ImageTk.PhotoImage(original_image) # 更新Label中的图像 label_imgshow.config(image = photo_image) label_imgshow.image = photo_image width, height = original_image.size print(width, height) imgAdapting_display = original_image.resize((image_show_width, image_show_height), Image.LANCZOS) zoom_value = 1.0 class Rect: def __init__(self, x = 0, y = 0, width = 0, height = 0): self.x = x self.y = y self.width = width self.height = height class Point: def __init__(self, x = 0, y = 0): self.x = x self.y = y import time def timebegin(): # 记录开始时间 # global start_time start_time = time.time() return start_time def timeend(code_blokc, start_time): # 记录结束时间 # global end_time end_time = time.time() # 计算耗时(单位:秒) elapsed_time = end_time - start_time # print(code_blokc + f" 代码块耗时: {elapsed_time} 秒") def zoom(event): # 1 imgAdapting_display用于控件显示的1x倍率图像,FOV为原始FOV, 大小与控件大小一致 # 2 imgZoom是根据当前zoom_value,缩放的图像,FOV为原始FOV # 3 cropped_show_image 图像大小控件大小一致, FOV不确定 start_time = timebegin() global zoom_value # 鼠标位置在图像显示控件的相对坐标 # 获取鼠标位置或其他指定的缩放触发点位置 ptMouse = Point(event.x, event.y) print("ptMouse ", ptMouse.x, ptMouse.y) scalestep = 1.21 showRect = Rect(0,0,image_show_width,image_show_height) bIsImgSmallToDispCtrl = False if (event.delta > 0):#放大图片 if (zoom_value > 5): return else: zoom_value = zoom_value * scalestep; elif (event.delta < 0): #缩小图片 zoom_value /= scalestep print("zoom_value ", zoom_value) zoom_width = int(image_show_width * zoom_value) zoom_height = int(image_show_height * zoom_value) timeend("mgAdapting_display.resize pre", start_time) start_time = timebegin() imgZoom = imgAdapting_display.resize((zoom_width, zoom_height), Image.LANCZOS) timeend("mgAdapting_display.resize", start_time) start_time = timebegin() #保持图像放大后,鼠标所指向的像素位置与原始像素位置一致 showRect.x = zoom_value * ptMouse.x - ptMouse.x showRect.y = zoom_value * ptMouse.y - ptMouse.y showRect.width = image_show_width showRect.height = image_show_height if showRect.x < 0: showRect.x = abs(showRect.x) bIsImgSmallToDispCtrl = True if showRect.y < 0: showRect.y = abs(showRect.y) bIsImgSmallToDispCtrl = True showRect.x = int(showRect.x) showRect.y = int(showRect.y) print("bIsImgSmallToDispCtrl ", bIsImgSmallToDispCtrl) if bIsImgSmallToDispCtrl == True: # imgZoom的图像,此时属于显示图像已经比原始图像小了,然后进行放大,不需要进行crop,直接显示imgZoom cropped_show_image = imgZoom else: #imgZoom的图像比原始图像大,从imgZoom的图像中crop一块区域进行显示,crop的大小为显示控件的size left = showRect.x top = showRect.y width = showRect.width height = showRect.height crop_rect = (left, top, left + width, top + height) cropped_show_image = imgZoom.crop(crop_rect) timeend("cropped_show_image ", start_time) start_time = timebegin() # 更新Label中的图像 photo_image = ImageTk.PhotoImage(cropped_show_image) label_imgshow.config(image = photo_image) label_imgshow.image = photo_image timeend("更新Label中的图像 ", start_time) label_imgshow.bind("<MouseWheel>", zoom) root.mainloop()
标签:鼠标,show,image,time,zoom,height,width,图像 From: https://www.cnblogs.com/adong7639/p/18445893