Python实现屏幕截图的代码及函数详解

废话不多说,先给大家看下python实现屏幕截图的代码,具体代码如下所述:

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.jb51.net)

Pythony实现屏幕截屏的函数如下所述:

# -*- coding: cp936 -*- 
import time,Image 
import os, win32gui, win32ui, win32con, win32api 
def window_capture(dpath): 
''''' 
截屏函数,调用方法window_capture('d:\\') ,参数为指定保存的目录 
返回图片文件名,文件名格式:日期.jpg 如: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   #图片大小 
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'
#调用截屏函数 
window_capture('d:\\')

相关推荐