首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >截图工具无法拍摄左侧截图

截图工具无法拍摄左侧截图
EN

Stack Overflow用户
提问于 2022-04-24 15:15:37
回答 1查看 50关注 0票数 0

我有一个捕捉截图的工具,但是这个工具只在你按住按钮-1并移动到右边,当你按住按钮-1并将它移动到左侧或上方时,这个工具才能捕捉截图,它没有捕获截图,原因是什么,我如何修复它?我想过了,但我认为这是个数学题,我在等你的帮助。

代码语言:javascript
复制
import tkinter as tk
from PIL import Image, ImageTk, ImageGrab, ImageEnhance

root = tk.Tk()
root.resizable(0, 0)

def show_image(image):
    win = tk.Toplevel()
    win.image = ImageTk.PhotoImage(image)
    tk.Label(win, image=win.image).pack()
    win.grab_set()
    win.wait_window(win)

def area_sel():
    x1 = y1 = x2 = y2 = 0
    roi_image = None

    def on_mouse_down(event):
        nonlocal x1, y1
        x1, y1 = event.x, event.y
        canvas.create_rectangle(x1, y1, x1, y1, outline='red', tag='roi')

    def button_release(event):
        print("ok")

        win.destroy()
    def on_mouse_move(event):
        nonlocal roi_image, x2, y2
        x2, y2 = event.x, event.y
        canvas.delete('roi-image') # remove old overlay image
        canvas.update()

        roi_image = image.crop((x1, y1, x2, y2)) # get the image of selected region
        canvas.image = ImageTk.PhotoImage(roi_image)
        canvas.create_image(x1, y1, image=canvas.image, tag=('roi-image'), anchor='nw')
        canvas.coords('roi', x1, y1, x2, y2)
        # make sure the select rectangle is on top of the overlay image
        canvas.lift('roi')

    root.withdraw()  # hide the root window
    image = ImageGrab.grab()  # grab the fullscreen as select region background
    bgimage = ImageEnhance.Brightness(image).enhance(0.3)  # darken the capture image
    # create a fullscreen window to perform the select region action
    win = tk.Toplevel()
    win.attributes('-fullscreen', 1)
    win.attributes('-topmost', 1)
    canvas = tk.Canvas(win, highlightthickness=0)
    canvas.pack(fill='both', expand=1)
    tkimage = ImageTk.PhotoImage(bgimage)
    canvas.create_image(0, 0, image=tkimage, anchor='nw', tag='images')
    # bind the mouse events for selecting region
    win.bind('<ButtonPress-1>', on_mouse_down)
    win.bind('<ButtonRelease>', button_release)
    win.bind('<B1-Motion>', on_mouse_move)
    # use Esc key to abort the capture
    win.bind('<Escape>', lambda e: win.destroy())
    # make the capture window modal
    win.focus_force()
    win.grab_set()
    win.wait_window(win)
    root.deiconify()  # restore root window
    # show the capture image
    if roi_image:
        show_image(roi_image)

tk.Button(root, text='select area', width=30, command=area_sel).pack()

root.mainloop()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-24 15:42:34

在裁剪图像和创建roi矩形时,需要保留x1、<=、x2y1 <= y2

代码语言:javascript
复制
def normalize(x1, y1, x2, y2):
    if x1 > x2:
        x1, x2 = x2, x1
    if y1 > y2:
        y1, y2 = y2, y1
    return x1, y1, x2, y2

def on_mouse_move(event):
    nonlocal roi_image, x2, y2
    x2, y2 = event.x, event.y
    rect = normalize(x1, y1, x2, y2)

    canvas.delete('roi-image') # remove old overlay image
    roi_image = image.crop(rect) # get the image of selected region
    canvas.image = ImageTk.PhotoImage(roi_image)
    canvas.create_image(rect[:2], image=canvas.image, tag=('roi-image'), anchor='nw')
    canvas.coords('roi', rect)
    # make sure the select rectangle is on top of the overlay image
    canvas.lift('roi')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71989744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档