Python screenshot – is it possible? I asked myself after I did not believe the results obtained by collecting data on the number of voters in the petitions (I wrote about this earlier ), after which I realized that the statistics are quite true, but for the sake of truthfulness there are not enough screenshots attached. Let’s fix this mistake and see how to take a screenshot in Python.
The magic library PyAutoGUI will help us with this, which, according to the developers, will help in emulating keystrokes on the keyboard, mouse movements and even hot keys 🙂 We will talk about this later (the library is really good, and therefore I will try to make it small in the near future , but a more intelligible review with code examples), but for now – we’ll deal with creating screenshots.
import pyautogui #connect the library
import time #We connect the library to work with time
x = 1 #Created a variable that will help in working with time
while x < 5: #Create a loop that will work until the x value is 5
pyautogui.screenshot('c:\\Users\\oleks\\Desktop\\Git\\Netology\\PrintScr\\image' + str(x) + '.png') #We ask the library to take a screenshot, and save it to a folder at the specified address with the name "image.png", moreover, the serial number will change depending on the value of x (I think it will be more correct, I will use a time stamp as a number, but first, let's make the code in this form)
x = x + 1 #Increase x by one
time.sleep(5) #5 second pause
And yes, if you need to take a screenshot of a certain size, use the following construction:
pyautogui.screenshot('c:\\Users\\oleks\\Desktop\\Git\\Netology\\PrintScr\\image' + str(x) + '.png', region=(0, 0, 300, 400)) #Where the values in parentheses are: 0, 0 - the initial value of the selected piece vertically, 300, 400 - the final values of the selected piece vertically