Python實現圖片轉文字並翻譯至剪切板

1、環境搭建:html

  一、PySimpleGUI: pip3 install pysimpleguipython

  二、pytesseract須要有tesseract環境才行:api

    1. 先搭建tesseract:app

      brew install tesseract  /*安裝tesseract環境*/dom

      brew install tesseract-lang   /*安裝語言包,大概有600+M,心痛。。*/ide

    2. 安裝pytesseract函數

      pip3 install pytesseractui

2、基本流程:url

  一、設計一個窗口,支持上傳圖片文件和相關參數設定。大概長這樣:spa

  

  
import PySimpleGUI as sg event,values = sg.Window('選擇轉文字照片'). Layout([ [sg.Text('上傳照片')], [sg.Input(), sg.FileBrowse('選擇文件')], [sg.Radio('開啓翻譯','flag'),sg.Radio('中/英', "choose")], [sg.OK('確認'), sg.Cancel('取消')] ]).Read()
GUI代碼

  分支控制:  

  
if values[1]: if values[2]: res = translate(text, 'en', 'zh') else: res = translate(text, 'zh', 'en') text = ''
   for ans in res['trans_result']: text+=ans['dst']+'\n'
根據表單進行中英互譯

  二、圖片轉文字:

  這裏主要用到的是pytesseract庫,引用庫之後一行代碼就搞定了。

  text=pytesseract.image_to_string(Image.open(values[0]),lang='chi_sim')

   參數:values[0]是上傳圖片的地址,lang表明文字識別語言

  三、調用百度翻譯api:

  首先去百度翻譯開發者平臺註冊申請api:百度翻譯開放平臺

  而後在控制檯的開發者信息裏,會有你的appid和密鑰:

  

  根據官方提供的demo,我擼了一個調用函數:

  
def translate(q,fromLang,toLang): # q表明須要翻譯的語句、fromlang是待翻譯語言、tolang是翻譯成的語言
   appid = '你的appid' secretKey = '你的密鑰' httpClient = None myurl = '/api/trans/vip/translate' salt = random.randint(32768, 65536) sign = appid+q+str(salt)+secretKey m1 = hashlib.md5() m1.update(sign.encode(encoding='utf-8')) sign = m1.hexdigest() myurl = myurl+'?appid='+appid+'&q='+parse.quote(q)+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign try: httpClient = http.client.HTTPConnection('api.fanyi.baidu.com') httpClient.request('GET', myurl) response = httpClient.getresponse() ans = response.read().decode('utf-8') ans = eval(ans) except Exception as e: print(e) finally: if httpClient: httpClient.close() if ans: return ans
百度翻譯api調用

  四、調用剪切板:

  利用subprocess庫,按照基本使用方法直接用就好。

  
text=bytes(text,'utf8') p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE) p.stdin.write(text) p.stdin.close() p.communicate()
複製至剪切板

3、完整代碼:

 1 from PIL import Image  2 import pytesseract  3 import subprocess  4 import PySimpleGUI  5 import PySimpleGUI as sg  6 import http.client  7 import hashlib  8 from urllib import parse  9 import random 10 
11 def translate(q,fromLang,toLang): 12    appid = '你的appid'
13    secretKey = '你的密鑰'
14    httpClient = None 15    myurl = '/api/trans/vip/translate'
16    salt = random.randint(32768, 65536) 17    sign = appid+q+str(salt)+secretKey 18    m1 = hashlib.md5() 19    m1.update(sign.encode(encoding='utf-8')) 20    sign = m1.hexdigest() 21    myurl = myurl+'?appid='+appid+'&q='+parse.quote(q)+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign 22    try: 23       httpClient = http.client.HTTPConnection('api.fanyi.baidu.com') 24       httpClient.request('GET', myurl) 25       response = httpClient.getresponse() 26       ans = response.read().decode('utf-8') 27       ans = eval(ans) 28    except Exception as e: 29       print(e) 30    finally: 31       if httpClient: 32  httpClient.close() 33       if ans: 34          return ans 35 
36 event,values = sg.Window('選擇轉文字照片'). Layout([ 37                                                    [sg.Text('上傳照片')], 38                                                    [sg.Input(), sg.FileBrowse('選擇文件')], 39                                                    [sg.Radio('開啓翻譯','flag'),sg.Radio('中/英', "choose")], 40                                                    [sg.OK('確認'), sg.Cancel('取消')] 41  ]).Read() 42 if event=='取消': 43     exit('no image file selected!') 44 
45 text=pytesseract.image_to_string(Image.open(values[0]),lang='chi_sim'); 46 text=str(text).replace('\n','') 47 if values[1]: 48    if values[2]: 49       res = translate(text, 'en', 'zh') 50    else: 51       res = translate(text, 'zh', 'en') 52    text = ''
53    for ans in res['trans_result']: 54       text+=ans['dst']+'\n'
55 text=bytes(text,'utf8') 56 p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE) 57 p.stdin.write(text) 58 p.stdin.close() 59 p.communicate()
Source Code

原文出處:https://www.cnblogs.com/Rhythm-/p/10500092.html

相關文章
相關標籤/搜索