Screenshot in Python

Asked

Viewed 125 times

0

Hello! My problem is this: I want to make a screenshot of my pc screen using python, I can even beat the screenshot, however, the screenshot is only saved after running the program, I say, the print is only saved after my code finishes running... Is there any way to save the screenshot while running the program? Follows the code:

import pyautogui
import time

capturar = pyautogui.screenshot()
capturar.save('foto.jpg')

for a in range(1,10):
    print(a)
    time.sleep(1) 

As I said, screen capture is only saved after the for, that is when the code stops running. It can be saved before the execution of the program?

1 answer

0


MODIFIED IN 5/Jan/2021

I believe that’s what you’re trying to do:

import os
import pyautogui
import time

foto = 'foto.jpg'

pyautogui.screenshot(foto)  # Desta forma o pyautogui salva a imagem

if os.path.isfile(foto):
   print("Foto salva")

for a in range(1,10):
    print(a)
    time.sleep(1)

The exit will be

% python salva_screenshot.py
Foto salva
1
2
3
4
5
6
7
8
9

I hope it helps

  • The problem still persists... It saves the image only at the end of code execution, I would like it saved during execution.

  • It would be an image sequence ?

  • What do you mean image sequence? Do you mean multiple photos? If that’s what you mean, I should say I just want to take a screenshot and save it, but before the program stops running

  • @Rcoletto, how are you testing whether the photo exists or not? I modified the post showing that saved before the loop.

  • I ran the code you sent, apparently it works, but the photo only appears in my files when it stops running.

  • The os.path.isfile(foto) test if the file is on your operating system... See that it is! As you are testing "only appears in my files"?

  • Thanks for the help! I had to restart the pc, and after it did that I tested the code again and it worked! I don’t know exactly what happened, but it worked! Thank you.

  • @Rcoletto I’m glad it’s working... If you think it’s relevant, give a read here

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.