-4

I am using pyautogui to take screenshots of a certain area of the screen. In an attempt to see what is going on with my script that I need to change, I want to save each screenshot into a folder. My thought was to throw a counter on and add it to the screenshot name.

The normal code for a screenshot is:

pyautogui.screenshot('opponent.png', region=(177, 743, 635, 135))

Instead of using the name opponent I would want the name to be opponent + a .png. I would use a = a + 1 inside the loop to change a each time, I am just not sure the proper way to add the variable a.

Example of the file names would be:

opponent1.png
opponent2.png
opponent3.png
opponent4.png 
...

Edit: Based off the Question that this was listed a possible duplicate of I tried this code:

pyautogui.screenshot('opponent%d.png', region=(177, 743, 635, 135)) % won

where won is a variable counting which opponent the screenshot is of. The error I get is:

TypeError: unsupported operand type(s) for %: 'Image' and 'int'

I also tried using the format solution and used this line of code:

pyautogui.screenshot('opponent{0}.png', region=(177, 743, 635, 135)).format(won)

This also through a TypeError: 'NoneType' object is not callable

Brandon
  • 465
  • 3
  • 10
  • 19
  • 1
    Possible duplicate of [insert variable values into a string in python](https://stackoverflow.com/questions/3367288/insert-variable-values-into-a-string-in-python) – jonrsharpe Jun 24 '17 at 15:42
  • @jonrsharpe thank you for pointing me to this post, I am still not understanding from that exactly how this is to be done though? – Brandon Jun 24 '17 at 15:47
  • It gives three different ways to do precisely what you want to do. It's unclear what you're still struggling with or why, but if you really can't figure it out please [edit] to illustrate where you're stuck. – jonrsharpe Jun 24 '17 at 15:57

1 Answers1

0

Enough discussion, eh? The only caution I would mention is that you must ensure that (somehow) this code runs outside the region that you want to get pictures of.

>>> import pyautogui
>>> a = 1
>>> while True:
...     image = pyautogui.screenshot('opponent%s.png' % a, region=(177, 743, 635, 135))
...     a = a+1
...     if a > 6: 
...         break
... 
Bill Bell
  • 21,021
  • 5
  • 43
  • 58