我需要创建一个小部件,它的作用类似于 Window 的截图工具,并在按钮释放时保存裁剪后的图像。
它工作得很好,除了图像被裁剪和保存,好像它被赋予了错误的坐标。我检查了全屏截图,坐标是 (0,0,1279,719),这是我的屏幕分辨率,所以我不明白为什么 ImageGrab.grab(bbox=) 无法正确裁剪。
这是代码。
from PIL import *
from tkinter import *
from PIL import ImageGrab
class Widget(Frame):
def __init__(self,master):
Frame.__init__(self,master=None)
self.root = master
self.root.attributes("-alpha", 0.2)
self.root.attributes("-fullscreen", True)
self.root.title("")
self.canvas = Canvas(self, cursor="cross", background="blue", width= master.winfo_screenwidth(), height=master.winfo_screenheight() )
self.canvas.pack()
self.canvas.bind("<ButtonPress-1>", self.on_button_press)
self.canvas.bind("<B1-Motion>", self.on_move_press)
self.canvas.bind("<ButtonRelease-1>", self.on_button_release)
self.rect = None
self.start_x = None
self.start_y = None
def on_button_press(self, event):
# save mouse drag start position
self.start_x = self.canvas.canvasx(event.x)
self.start_y = self.canvas.canvasy(event.y)
self.rect = self.canvas.create_rectangle(0, 0, 1, 1, outline='red')
def on_move_press(self, event):
curX = self.canvas.canvasx(event.x)
curY = self.canvas.canvasy(event.y)
# expand rectangle as you drag the mouse
self.canvas.coords(self.rect, self.start_x, self.start_y, curX, curY)
def on_button_release(self, event):
self.endx = self.canvas.canvasx(event.x)
self.endy = self.canvas.canvasy(event.y)
x1 = min(self.start_x, self.endx)
y1 = min(self.start_y, self.endy)
x2 = max(self.start_x, self.endx)
y2 = max(self.start_y, self.endy)
self.root.destroy()
img = ImageGrab.grab(bbox=(x1, y1, x2, y2))
img.save('capture.png')
if __name__ == "__main__":
snip_window = Tk()
width = snip_window.winfo_screenwidth()
height = snip_window.winfo_screenheight()
snip_window.geometry(f'{width}x{height}')
snip_widget = Widget(snip_window)
snip_widget.pack()
snip_window.mainloop()