Python screen capture of the code and function details

  • 2020-05-12 02:50:56
  • OfStack

Without further details, I'd like to show you the code of screen capture of python. The specific code is as follows:


from selenium import webdriver
import time
def capture(url, save_fn="capture.png"):
browser = webdriver.Firefox() # Get local session of firefox
browser.set_window_size(1200, 900)
browser.get(url) # Load page
browser.execute_script("""
(function () {
var y = 0;
var step = 100;
window.scroll(0, 0);
function f() {
if (y < document.body.scrollHeight) {
y += step;
window.scroll(0, y);
setTimeout(f, 50);
} else {
window.scroll(0, 0);
document.title += "scroll-done";
}
}setTimeout(f, 1000);
})();
""")
for i in xrange(30):
if "scroll-done" in browser.title:
break
time.sleep(1)
browser.save_screenshot(save_fn)
browser.close()
if __name__ == "__main__":
capture(//www.ofstack.com)

Pythony implements the screen capture function as follows:


# -*- coding: cp936 -*- 
import time,Image 
import os, win32gui, win32ui, win32con, win32api 
def window_capture(dpath): 
''''' 
 Screenshot function , A method is called window_capture('d:\\') , Parameter to specify the directory to save  
 Return image file name , File name format : The date of .jpg  Such as :2009328224853.jpg 
'''
hwnd = 0
hwndDC = win32gui.GetWindowDC(hwnd) 
mfcDC=win32ui.CreateDCFromHandle(hwndDC) 
saveDC=mfcDC.CreateCompatibleDC() 
saveBitMap = win32ui.CreateBitmap() 
MoniterDev=win32api.EnumDisplayMonitors(None,None) 
w = MoniterDev[0][2][2] 
h = MoniterDev[0][2][3] 
#print w,h   # Image size  
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) 
saveDC.SelectObject(saveBitMap) 
saveDC.BitBlt((0,0),(w, h) , mfcDC, (0,0), win32con.SRCCOPY) 
cc=time.gmtime() 
bmpname=str(cc[0])+str(cc[1])+str(cc[2])+str(cc[3]+8)+str(cc[4])+str(cc[5])+'.bmp'
saveBitMap.SaveBitmapFile(saveDC, bmpname) 
Image.open(bmpname).save(bmpname[:-4]+".jpg") 
os.remove(bmpname) 
jpgname=bmpname[:-4]+'.jpg'
djpgname=dpath+jpgname 
copy_command = "move %s %s" % (jpgname, djpgname) 
os.popen(copy_command) 
return bmpname[:-4]+'.jpg'
# Call the screenshot function  
window_capture('d:\\')

Related articles: