使用 python 截取特定 space 的屏幕截图

Taking screenshot of a specific space using python

我正在尝试使用 python 截取特定区域的屏幕截图。

我写了这段代码:

import pyscreenshot
from pynput.mouse import Listener
x=1
y=1

def on_click(x1, y1, button, pressed):
    global x,y
    x = x1
    y = y1

def on_release(x2,y2, button, pressed):
    global x,y
    im = pyscreenshot.grab(x,y,x2,y2)
    im.save("hello.png")

#Collect events until released
with Listener(
        on_click=on_click,
        on_release=on_release) as listener:
    listener.join()

这里的问题是没有输出anything.Please帮助

我给你做了一个代码。它在我身边工作正常。请在 运行 我的代码后告诉我你的意见。

import pyscreenshot
from pynput.mouse import Listener, Button

global x0, y0


def on_click(x1, y1, button, pressed):
    global x0, y0

    if button == Button.left and pressed:
        x0, y0 = x1, y1

    if button == Button.left and not pressed:
        try:
            im = pyscreenshot.grab(bbox=(x0, y0, x1, y1))
            im.save("hello.png")
            print('Screenshot was taken.')
            return False
        except:
            pass

    return True

# Collect events until released
with Listener(
        on_click=on_click) as listener:
    try:
        listener.join()
    except:
        pass