-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
94 lines (81 loc) · 2.41 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# name: Leon
#Data: 2019.4
import requests
import random
import hashlib
import urllib
from PIL import Image
import pytesseract
from urllib.parse import quote
#语言字典
language = {
'自动检测': 'auto',
'中文': 'zh',
'英语': 'en',
'粤语': 'yue',
'文言文': 'wyw',
'日语': 'jp',
'韩语': 'kor',
'法语': 'fra',
'西班牙语': 'spa',
'泰语': 'th',
'阿拉伯语': 'ara',
'俄语': 'ru',
'葡萄牙语': 'pt',
'德语': 'de',
'意大利语': 'it',
'希腊语': 'el',
'荷兰语': 'nl',
'波兰语': 'pl',
'保加利亚语': 'bul',
'爱沙尼亚语': 'est',
'丹麦语': 'dan',
'芬兰语': 'fin',
'捷克语': 'cs',
'罗马尼亚语': 'rom',
'斯洛文尼亚语': 'slo',
'瑞典语': 'swe ',
'匈牙利语': 'hu',
'繁体中文': 'cht',
'越南语': 'vie'
}
#百度翻译API接口
appid = '20190426000291981' # API
secretKey = 'sJldvBzBI78Pg3aK6_l4' # 密钥
#tesseract 提取图片文字
def get_text(path):
text = pytesseract.image_to_string(Image.open(path))
return text
#发送请求
def get_an(x):
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like G'
'ecko) Chrome/73.0.3664.3 Safari/537.36'}
t = requests.get(x, headers=header, timeout=2)
return t
if __name__ == '__main__':
i = 'yes'
while(i == 'yes'):
wh = input("翻译为->语言")
wh = language[wh]
an = input("翻译文字or图片(w or i):")
if an == 'i':
pa = input("请输入图片文件路径:")
q = get_text(path=pa).replace('\n', ' ')
else:
q = input("请输入内容:")
httpClient = None
url = '/api/trans/vip/translate'
fromLang = 'auto'
toLang = wh
salt = random.randint(32768, 65536)
sign = appid + q + str(salt) + secretKey
m1 = hashlib.md5()
m1.update(sign.encode("utf8"))
sign = m1.hexdigest()
myurl = 'http://api.fanyi.baidu.com' + url + '?appid=' + appid + '&q=' + urllib.parse.quote(q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(salt) + '&sign=' + sign
c = get_an(myurl).json()
#print(c)
print("--->>> ")
print(" "+c['trans_result'][0]['dst'])
print(" ---<<<")
i = input("是否继续翻译(yes/no):")