bbox在输入模块内的精确坐标时工作,但由于变量包含相同的d而失败

2024-10-02 02:30:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试对配置文件中由行定义的区域进行屏幕抓取时遇到问题:

以下代码:

def coordstore():                                 # check line 1 of config file (screencap name)
    f=open('config.txt')
    line=f.readlines()
    coordstore.x1,coordstore.y1,coordstore.x2,coordstore.y2 = (line[4]).strip(),(line[5]).strip(),(line[6]).strip(),(line[7]).strip()    # note: confusing.  0=line 1, 1=line2 etc.
    coordstore.coords = coordstore.x1+',',coordstore.y1+',',coordstore.x2+',',coordstore.y2
    coordstore.stripped = ' '.join((coordstore.coords))
    print(coordstore.stripped)
    return(coordstore.stripped)
coordstore()

def screenshot():
    import pyscreenshot as ImageGrab2
    # part of the screen
    if __name__ == '__main__':
        im = ImageGrab2.grab(bbox=(coordstore.stripped))  # X1,Y1,X2,Y2
        im.save('tc.png')
screenshot()

精确打印这个值:10,20,100,300

然后它将失败,并执行此回溯:

Traceback (most recent call last):
  File "file location", line 82, in <module>
    screenshot()

  File "file location", line 80, in screenshot
    im = ImageGrab2.grab(bbox=(coordstore.stripped))  # X1,Y1,X2,Y2

  File "PYCHARM\venv\lib\site-packages\pyscreenshot\__init__.py", line 67, in grab
    to_file=False, childprocess=childprocess, backend=backend, bbox=bbox)

  File "PYCHARM\venv\lib\site-packages\pyscreenshot\__init__.py", line 38, in _grab
    x1, y1, x2, y2 = bbox
ValueError: too many values to unpack (expected 4)

如果我手动地将(bbox=(coordstore.stripped))替换为(bbox=(10,20,100,300)),这与变量打印的内容完全相同,那么代码工作得很好,但对于我的需要来说并不有用。我没看见什么?谢谢你的帮助

更新:

我试过另一种方法

def coordstore():                                 # check line 1 of config file (screencap name)
    f=open('config.txt')
    line=f.readlines()
    coordstore.x1 = (line[4]).strip()
    coordstore.y1 = (line[5]).strip()
    coordstore.x2 = (line[6]).strip()
    coordstore.y2 = (line[7]).strip()
    print(coordstore.x1+',', coordstore.y1+',', coordstore.x2+',', coordstore.y2)
    return(coordstore.x1, coordstore.y1, coordstore.x2, coordstore.y2)
coordstore()

def screenshot():
    import pyscreenshot as ImageGrab2
    # part of the screen
    if __name__ == '__main__':
        im = ImageGrab2.grab(bbox=(coordstore.x1+',', coordstore.y1+',', coordstore.x2+',', coordstore.y2+','))  # X1,Y1,X2,Y2
        im.save('tc.png')
screenshot()

这个指纹

10,20,100,300

但返回以下回溯错误:

Traceback (most recent call last):
  File "module location", line 84, in <module>
    screenshot()
  File "module location", line 82, in screenshot
    im = ImageGrab2.grab(bbox=(coordstore.x1+',', coordstore.y1+',', coordstore.x2+',', coordstore.y2+','))  # X1,Y1,X2,Y2
  File "\PYCHARM\venv\lib\site-packages\pyscreenshot\__init__.py", line 67, in grab
    to_file=False, childprocess=childprocess, backend=backend, bbox=bbox)
  File "\PYCHARM\venv\lib\site-packages\pyscreenshot\__init__.py", line 42, in _grab
    raise ValueError('bbox y2<=y1')
ValueError: bbox y2<=y1

Tags: inlinefilestripx1x2grabscreenshot
1条回答
网友
1楼 · 发布于 2024-10-02 02:30:46

正如您在注释中提到的,coordstore.stripped的值是string。而ImageGrab2.grab所期望的参数是元组的类型,因此,首先必须将其转换为元组

更新方法如下:

def screenshot():
    import pyscreenshot as ImageGrab2
    # part of the screen
    if __name__ == '__main__':
        im = ImageGrab2.grab(bbox=tuple(map(int, coordstore.stripped.split(', '))))  # X1,Y1,X2,Y2
        im.save('tc.png')

相关问题 更多 >

    热门问题