. 01 .Mathpix Snipping Tool
经常跟数学公式打交道的人应该很熟悉一款叫 Mathpix Snipping Tool 的神器,它使用 AI 技术将公式图片转化为 Latex 公式脚本。可惜的是,现在每个月只能免费使用 50 次了。
不过好在官网还是提供了每月免费使用 1000 次的办法,那就是调用 OCR API 的方式。
. 02 .Mathpix OCR API
从官网[1]看,目前来说,调用 OCR API 的话,一个月可以免费使用 1000 次!之后一直到 10 万次都是每次 0.004 美元(约合 0.028 元)。对于一般用户,1000 次应该差不多够啦。注册请戳这里https://dashboard.mathpix.com。
. 03 .批量转化脚本
该软件通过截屏及时转化为公式本身是一个很好的交互方式,但是我更想先截屏一批公式图片,然后一次性将他们转化为 Latex 公式代码并存入文件。查了下,网上貌似没有现成的代码。想想也没几行代码,那就自己来实现吧。
这里我们把官网提供的脚本样例[2]稍作修改,使得它能够将一个文件夹里的公式图片批量转化为 Latex 公式脚本。各位只要把其中的 app_id 和 app_key 替换成你自己的相应值,将截屏图片放入文件夹equ_png,然后python mathpix_python.py,得到文件如latex_script_21_56_11_09_12_2019.tex。
本脚本在 Linux 下测试通过,在其他平台应该也没啥问题,有兴趣的就试试吧,如果发现问题请反馈,谢谢。
import os
import json
import glob
import time
import base64
import requests
# 获取指定目录下的所有图片
equ_png_dir = 'equ_png'
pnglist = glob.glob(equ_png_dir+'/*.png')
pnglist
['equ_png/equ1.png', 'equ_png/equ2.png']
#
# Common module for calling Mathpix OCR service from Python.
#
# N.B.: Set your credentials in environment variables APP_ID and APP_KEY,
# either once via setenv or on the command line as in
# APP_ID=my-id APP_KEY=my-key python3 simple.py
#
env = os.environ
default_headers = {
'app_id': env.get('APP_ID', 'Your_APP_ID'),
'app_key': env.get('APP_KEY', 'Your_APP_KEY'),
'Content-type': 'application/json'
}
service = 'https://api.mathpix.com/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=30):
r = requests.post(service,
data=json.dumps(args), headers=headers, timeout=timeout)
return json.loads(r.text)
raw_dict = {
'\a':r'\a', '\b':r'\b', '\c':r'\c', '\f':r'\f',
'\n':r'\n', '\r':r'\r', '\t':r'\t', '\v':r'\v',
'\'':r'\'', '\"':r'\"', '\0':r'\0', '\1':r'\1',
'\2':r'\2', '\3':r'\3', '\4':r'\4', '\5':r'\5',
'\6':r'\6', '\7':r'\7', '\8':r'\8', '\9':r'\9'}
"""Return a raw string representation of script"""
def raw(script):
new_script = ''
for char in script:
try:
new_script += raw_dict[char]
except KeyError:
new_script += char
return new_script
def mathpix_dir(pngs):
t = time.strftime('%H_%M_%S_')+time.strftime('%d_%m_%Y')
filename = 'latex_script_'+t+'.tex'
sfile = open(filename,'w')
sfile.write('%%'+t+'\n\n')
timeout = 2
for i,pic in enumerate(pngs):
r = latex({
'src': image_uri(pic),
'formats': ['latex_styled']
})
equ = r['latex_styled']
print(equ)
latexscript = '%%{}\n$$\n{}\n$$\n\n\n'.format(pic,raw(equ))
sfile.write(latexscript)
time.sleep(timeout)
sfile.close()
if __name__ == '__main__':
mathpix_dir(pnglist)
. 04 .样例测试
好了,我们拿下面三个图片来测试一下效果。
△ 公式1图片
△ 公式2图片
△ 公式3图片
打开上面程序创建出来的文件并将里面的 Latex 脚本代码放到 markdown 里显示如下。
# equ_png/equ1.png
# equ_png/equ2.png
# equ_png/equ3.png
. 05 .代码
获取本文代码请点击这里[3]。
相关资料
[1]
官网定价: https://mathpix.com/ocr#pricing
[2]
脚本样例: https://github.com/Mathpix/api-examples
[3]
github地址: https://github.com/mathinml/batimg2latex