调⽤mathpixapi识别图⽚中的公式
借助mathpix软件可以很⽅便的识别图⽚中的公式,复制到word中,可编辑的模式。但是免费版的mathpix只提供每⽉50次的使⽤,很难满⾜要求,但可喜的是他们提供了每⽉1000次的免费调⽤接⼝。
登录该⽹址即可申请app_id和app_key
这个脚本使⽤起来很简单,通过任意⼀款截图软件把要是别的图⽚复制到粘贴板,脚本的功能是把粘贴板的图⽚保持到d盘,并送到mathpix的识别接⼝,返回json格式的对象,对其解析得到识别的latex格式的公式,识别结果会显⽰在console窗⼝,也会保存在⽂件中,把识别结果复制到word可以显⽰正常的可编辑的公式,
# example from github/Mathpix/api-examples/blob/master/python/mathpix.py
import os,base64,requests,json
from PIL import ImageGrab
env = os.environ
#HOME = ('HOME') + "/Desktop/"
HOME = "D:\\"
default_headers = {
'app_id': ('APP_ID', '***********'),
'app_key': ('APP_KEY', '********************'),
'Content-type': 'application/json'
}
service = 'api.mathpix/v3/latex'
# Return the base64 encoding of an image with the given filename.
def image_uri(filename):
image_data = open(filename, "rb").read()
return "data:image/jpg;base64," + base64.b64encode(image_data).decode()
# Call the Mathpix service with the given arguments, headers, and timeout.
def latex(args, headers=default_headers, timeout=300):
r = requests.post(service, data=json.dumps(args), headers=headers, timeout=timeout)
return json.)
def mathpix():
# 从剪贴板获取公式
im = abclipboard()
im.save(HOME+'screen.png','PNG')
r = latex({
'src': image_uri(HOME+"screen.png"),
"ocr": ["math", "text"],
'formats': ['latex_styled']
})
print(r['latex_styled'])
f=open("",'a')
f.write(r['latex_styled']+'\n')
f.close()
if __name__ == '__main__':免费图片编辑软件app
# 调⽤ macOS 的 screencapture 命令⾏⼯具
#os.system("screencapture -i -c")
mathpix()
⽐如
的识别结果是
y = | a + b | / c
在word中插⼊latex格式的公式,可以转化为正常的显⽰⽅式