python 自動錄屏初步實現

來源:互聯網
上載者:User

標籤:ide   read   release   str   代碼   檔案   numpy   windows   png   

偶爾需要用python實現錄屏並匯出視頻,隨便寫了一個簡單的代碼。

大概思路是先每隔一段時間截屏,然後將截屏圖片變成視頻匯出。

 1 # -*- coding: cp936 -*- 2 from PIL import ImageGrab 3 import time,os,cv2 4 import numpy as np 5 def releasevideo(): 6     path=os.getcwd()+‘\\picture\\‘ 7     filelist=os.listdir(path) 8     fps=2 9     size=(1366,768)10     video=cv2.VideoWriter("1.avi", cv2.VideoWriter_fourcc(‘M‘, ‘J‘, ‘P‘, ‘G‘), fps, size)11     for item in filelist:12         if item.endswith(‘.png‘): 13     #找到路徑中所有尾碼名為.png的檔案,可以更換為.jpg或其它14             item = path + item15             img = cv2.imread(item)16             video.write(img)17 18     video.release()19     cv2.destroyAllWindows()20 if __name__==‘__main__‘:21     num=022     while 1:23         num+=124         time.sleep(0.5)   25         bbox=(0,0,1366,768)#四個參數代表了開始的x,y,結束的x,y,後兩個可以看電腦26         im=ImageGrab.grab(bbox)27         im.save(os.getcwd()+‘\\picture\\‘+str(num)+‘.png‘)28         if num>20:#這個可以控制時間結束,這邊我設定為5秒,改參數就行29             releasevideo()30             break31     

這裡只是測試可行性,沒有用最佳化,事實上沒有必要兩個分開操作,並行的效率會高很多。利用time.sleep()也是取巧居多。等過段時間利用wxpython寫一個python版本的錄屏工具。

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.