python,使用百度api實現複製截圖中的文字

來源:互聯網
上載者:User

標籤:etc   hot   tps   strftime   empty   ftime   turn   board   result   

百度雲文字識別技術文檔:

https://cloud.baidu.com/doc/OCR/OCR-Python-SDK.html#.E6.96.B0.E5.BB.BAAipOcr

 

from aip import AipOcr #百度aipfrom PIL import ImageGrab #處理剪下板圖片from PIL import Imageimport PILimport keyboard #監控鍵盤import sysimport time,datetimeimport randomimport win32clipboard as w  # 處理剪下板import win32condef screenShot():    ‘‘‘監控鍵盤事件,並儲存圖片‘‘‘    # 監控鍵盤,輸入QQ預設快速鍵時進入    if keyboard.wait(hotkey=‘ctrl+alt+a‘) == None:                        while True:            time.sleep(3)  #等待            im = ImageGrab.grabclipboard()  #擷取剪下板中的圖片            if isinstance(im,PIL.BmpImagePlugin.DibImageFile):  #若剪下板的內容可以解析成圖片                #檔案名稱                i = datetime.datetime.now().strftime(‘%Y%m%d%H%M%S‘)                r = str(random.randint(100,1000))                #儲存圖片                im.save(i+r+‘.png‘)                                         #百度雲帳號設定                APP_ID = ‘‘                API_KEY = ‘‘                SECRET_KEY = ‘‘                #百度雲api對象                client = AipOcr(APP_ID, API_KEY, SECRET_KEY)                #讀取圖片                image = get_file_content(i+r+‘.png‘)                #擷取圖片中的文字內容                data = client.basicGeneral(image)                words_result = data[‘words_result‘]                data = ""  # 需要儲存的內容                for words in words_result:                    data+=words[‘words‘]                                print(data)                setText(data)        #讀取圖片def get_file_content(filePath):    with open(filePath, ‘rb‘) as fp:        return fp.read()#寫入剪下板內容def setText(aString):    w.OpenClipboard()    w.EmptyClipboard()    w.SetClipboardText(aString)    w.CloseClipboard()if __name__ == "__main__":#for _ in range(sys.maxsize):  #修改成在screenShot中用while迴圈    screenShot()

 

python,使用百度api實現複製中的文字

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.