bbox 在模块内输入精确坐标时有效,但在包含相同数据的变量时失败

bbox works when inputting exact coordinates within module but fails with variable containing identical data

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

以下代码:

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

但是 returns 以下回溯错误:

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

正如您在评论中提到的,coordstore.stripped 的值是字符串。 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')