Skip to content

Instantly share code, notes, and snippets.

@danmou
Created March 7, 2018 17:43
Show Gist options
  • Save danmou/20170f2e7468f9f1d3dce16b5262c03e to your computer and use it in GitHub Desktop.
Save danmou/20170f2e7468f9f1d3dce16b5262c03e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import base64
import requests
import json
import subprocess
import tempfile
image = tempfile.NamedTemporaryFile().name
subprocess.run(['gnome-screenshot', '-a', '-f', image, '--display=:0'], check=True)
image_uri = "data:image/png;base64," + base64.b64encode(open(image, "rb").read()).decode('ascii')
r = requests.post("https://api.mathpix.com/v3/latex",
data=json.dumps({'src': image_uri}),
headers={"app_id": "", "app_key": "",
"Content-type": "application/json"})
result = json.loads(r.text)
if result["error"]:
result = result["error"]
else:
result = result["latex"]
subprocess.run(['xclip', '-selection', 'clipboard'], input=result.encode('ascii'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment