I've been trying to screenshot data from a program so I could then convert it to text because I wanted to automate the whole process. But from some reason, four separate screenshots are taken at the same time although I set time.sleep()
in multiple places — but when I do the same thing, but with no running the program (just desktop is visible) the screenshots are taken separately. How can I delay screenshots while inside program?
This is the full code (sorry for code's messiness):
import os
import pyautogui as pag
import time
#import pyscreenshot as pscr
from PIL import ImageGrab as scr
from datetime import datetime
from datetime import date
import subprocess
try:
from PIL import Image
except ImportError:
import Image
import pytesseract
def ocr_core(filename):
"""
This function will handle the core OCR processing of images.
"""
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
# We'll use Pillow's Image class to open the image and pytesseract to
# detect the string in the image.
text = pytesseract.image_to_string(Image.open(filename))
return text
os.startfile('C:\Program Files\Stellarium\stellarium.exe')
time.sleep(8)
pag.hotkey('f3')
pag.typewrite('Delta Cep')
pag.hotkey('enter')
time.sleep(4)
now_cep=datetime.now()
vrijeme_cep=now_cep.strftime('%m-%d-%Y %H-%M-%S')
folder='images/'
filename=' Delta Cep.png'
output_cep=folder+vrijeme_cep+filename
time.sleep(2)
im=scr.grab(bbox=(0,0,1919,1079))
im.save(output_cep)
#im=pag.screenshot(region=(0,0,1919,1079))
#im=scr.grab(bbox=(0,0,1919,1079))
#im=scr.grab(bbox=(7,274,330,290))
#im.save(output_cep)
#im.show()
pag.hotkey('f3')
pag.typewrite('Polaris')
pag.hotkey('enter')
time.sleep(4)
now_pol=datetime.now()
vrijeme_pol=now_pol.strftime('%m-%d-%Y %H-%M-%S')
folder='images/'
filename=' Polaris.png'
output_pol=folder+vrijeme_pol+filename
time.sleep(2)
im1=scr.grab(bbox=(0,0,1919,1079))
#im=scr.grab(bbox=(8,299,326,314))
im1.save(output_pol)
#im.show()
pag.hotkey('f3')
pag.typewrite('X Cyg')
pag.hotkey('enter')
time.sleep(4)
now_x=datetime.now()
vrijeme_x=now_x.strftime('%m-%d-%Y %H-%M-%S')
folder='images/'
filename=' X Cyg.png'
output_x=folder+vrijeme_x+filename
time.sleep(2)
im2=scr.grab(bbox=(0,0,1919,1079))
#im=scr.grab(bbox=(7,290,328,305))
im2.save(output_x)
#im.show()
pag.hotkey('f3')
pag.typewrite('SU Cyg')
pag.hotkey('enter')
time.sleep(4)
now_su=datetime.now()
vrijeme_su=now_su.strftime('%m-%d-%Y %H-%M-%S')
folder='images/'
filename=' SU Cyg.png'
output_su=folder+vrijeme_su+filename
im3=scr.grab(bbox=(0,0,1919,1079))
#im=scr.grab(bbox=(6,289,327,305))
im3.save(output_su)
#im.show()
x=ocr_core(output_cep)
x=x.split('\n',1)
x=x[0]
x=x.replace('Next maximum light: ','')
x=x.replace(' UTC','')
print(x)
x=x.replace('2020-08-','')
x=x.replace(x[x.find(' '):len(x)],'')
x=date(2020,8,int(x)).toordinal()+1721425
print(x)
y=ocr_core(output_pol)
y=y.split('\n',1)
y=y[0]
y=y.replace('Next maximum light: ','')
y=y.replace(' UTC','')
y=date(2020,8,int(y)).toordinal()+1721425
z=ocr_core(output_cep)
z=z.split('\n',1)
z=z[0]
z=z.replace('Next maximum light: ','')
z=z.replace(' UTC','')
z=date(2020,8,int(z)).toordinal()+1721425
q=ocr_core(output_cep)
q=q.split('\n',1)
q=q[0]
q=q.replace('Next maximum light: ','')
q=q.replace(' UTC','')
q=date(2020,8,int(q)).toordinal()+1721425
print(x+'\n'+y+'\n'+z+'\n'+q)
os.system('TASKKILL /F /IM stellarium.exe')
subprocess.Popen('explorer "D:\Dokumenti\INFORMATIKA\images"')
Here is the screenshot inside the program and all of the screenshots i got were the same (time at the bottom of the screen was identical). screenshot link
But on desktop you can see that screenshots were taken at different times. desktop screenshot desktop screenshot 2
Any kind of help is appreciated!
Code took screenshots as soon as program loaded because it was in fullscreen mode. Solution is to run the program in windowed mode.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.