如何在屏幕截图之间延迟?

时间:2020-08-23 14:58:00

标签: python python-imaging-library screenshot delay pyautogui

我一直在尝试对程序中的数据进行屏幕截图,因此我可以将其转换为文本,因为我想使整个过程自动化。但是由于某种原因,尽管我在多个位置设置了time.sleep(),但同时拍摄了四个单独的屏幕截图-但是,当我执行相同的操作但没有运行程序(仅显示桌面)时,就会截取屏幕截图分别。 在程序内部时如何延迟屏幕截图?

这是完整的代码(对不起代码,造成混乱):

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"')

这是程序内的屏幕截图,我得到的所有屏幕截图都是相同的(屏幕底部的时间相同)。 screenshot link

但是在台式机上,您可以看到屏幕快照是在不同时间拍摄的。 desktop screenshot desktop screenshot 2

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

由于程序处于全屏模式,因此代码在程序加载后立即截图。解决方案是在窗口模式下运行程序。