Search code examples
pythonscreenshot

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


I'm having an issue trying to make a screen grab of an area defined by lines in a config file:

The following code:

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()

prints exactly this value: 10, 20, 100, 300

it then fails with this Traceback:

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)

If I manually replace (bbox=(coordstore.stripped)) with (bbox=(10, 20, 100, 300)) which is literally identical to what the variable prints, the code works perfectly but is not useful for my needs. What am I not seeing? Thanks for the help!

UPDATE:

I have tried another approach.

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()

This prints

10, 20, 100, 300

but returns the following Traceback errors:

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

Solution

  • As you have mentioned in comments the value of coordstore.stripped is string. And expected argument by ImageGrab2.grab is type of tuple so, first you have to convert it into tuple.

    Update the method like this:

    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')