I'm currently writing a function using pyautogui that takes screenshots of a specific area on my screen, and another function that instructs that screenshot function to repeat itself.
I have time.sleep(3) set at the very top to give myself three seconds to open the e-reader I'm trying to screenshot. As it stands right now, when I run the repeat_pycapture() function, it only saves the first screenshot to my file path, then continues clicking on to new pages without capturing and saving additional images. I set the screenshot names to be the current date and time so that images aren't overwritten. I've also tried writing for loops and while loops to achieve this as well, but ran into more confusing issues with those.
When I run the ranged_pycapture() function several times manually though, everything works as intended, but then defeats the whole purpose of automating everything.
time.sleep(3)
date = datetime.now()
name = str(date).replace(":", "")
pyshot_all = pyautogui.screenshot()
pyshot_range = pyautogui.screenshot(region = (630, 121, 641, 828))
py_save_path = "C:\\(my custom path)"
def ranged_pycapture():
pyshot_range.save("{}.jpg".format(name))
pyautogui.click(1835, 1000)
return py_save_path
def repeat_pycapture():
schedule.every(0.7).seconds.do(ranged_pycapture)
while True:
schedule.run_pending()
time.sleep(0.7)
repeat_pycapture()
I've also written both functions using PIL instead, but ran into the exact same issue. I watched a video where a guy demonstrated how to take screenshots every 5 seconds automatically and provided his code, yet even after copying his code verbatim, I ran into the exact same issue. I've also used different versions of Python (3.8, 3.9, and 3.10) but to no avail. Am I missing something? Any community insight would be much appreciated.
Note: A good way to test these functions is to open a blank PowerPoint presentation and have the code cycle through slides.
If you put print(name)
inside ranged_pycapture()
then you see that all files use the same name.
You create date
and name
only once - at start - and later all images use the same date
and name
. You have to create date
and name
inside function ranged_pycapture()
.
And you have to also run screenshot()
inside this function to get new screenshot.
def ranged_pycapture():
date = datetime.now()
name = str(date).replace(":", "")
pyshot_range = pyautogui.screenshot(region=(630, 121, 641, 828))
pyshot_range.save("{}.jpg".format(name))
pyautogui.click(1835, 1000)
You can create name
directly using strftime
- and you can use directly .jpg
name = datetime.now().strftime('%Y-%m-%d %H%M%S.%f.jpg'))
pyshot_range.save(name)