1. 程式人生 > >python,使用百度api實現復制截圖中的文字

python,使用百度api實現復制截圖中的文字

content nsh AD con 讀取 __name__ return IT now()

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

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

from aip import AipOcr #百度aip
from PIL import ImageGrab #處理剪切板圖片
from PIL import Image
import PIL
import keyboard #監控鍵盤
import sys
import time,datetime
import random

import win32clipboard as w  # 處理剪切板
import win32con

def 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實現復制截圖中的文字