Python實現螢幕截圖的代碼及函數詳解_python

來源:互聯網
上載者:User

廢話不多說,先給大家看下python實現螢幕截圖的代碼,具體代碼如下所述:

from selenium import webdriverimport timedef capture(url, save_fn="capture.png"):browser = webdriver.Firefox() # Get local session of firefoxbrowser.set_window_size(1200, 900)browser.get(url) # Load pagebrowser.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:breaktime.sleep(1)browser.save_screenshot(save_fn)browser.close()if __name__ == "__main__":capture(http://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 = 0hwndDC = 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:\\')

以上所述是小編給大家分享的Python實現螢幕截圖的代碼及函數詳解,希望對大家有所協助,如果大家有任何疑問歡迎給我留言,小編會及時回複大家的,在此有非常感謝大家對雲棲社區網站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.