Skip to content

Instantly share code, notes, and snippets.

@Knio
Created November 8, 2009 06:34
Show Gist options
  • Save Knio/229142 to your computer and use it in GitHub Desktop.
Save Knio/229142 to your computer and use it in GitHub Desktop.
Windows7 Math Input Panel grabber

This script copies the output of the Windows 7 Math Input Panel to the clipboard, in the form <img title="TITLE" src="DATA"> where

  • TITLE = the MathML document corresponding to the formula
  • DATA = a base64 encoded string of a PNG image of the formula
import sys
import base64
import ImageGrab
import win32clipboard as w
import win32con
import win32gui
import StringIO
h = win32gui.FindWindow("MathInput_Window", '')
x1,y1,x2,y2 = win32gui.GetWindowRect(h)
Y2 = y1 + 615
X1 = x1 + 260
# get screen bitmap
im = ImageGrab.grab()
X,Y = im.size
imdata = im.getdata()
# scan upwards to find boundary
for y in range(Y2, 0, -1):
if imdata[y*X+X1] != (255,255,255):
break
Y1 = y+1
done = False
for x in range(X1+600, X1, -1):
for y in range(Y1, Y2):
if imdata[x+y*X] != (255,255,255):
done = True
break
if done: break
X2 = x + 7
im = im.crop((X1, Y1, X2, Y2))
im = im.convert('RGBA')
# TODO alpha channel
data = StringIO.StringIO()
im.save(data, 'png')
w.OpenClipboard()
try:
alt = w.GetClipboardData(w.RegisterClipboardFormat("MathML"))
except:
alt = ""
alt = alt.decode('utf-16')
alt = alt.encode('ascii', 'xmlcharrefreplace')
print alt
alt = alt.replace('"','&quot;')
st = '<img title="%s" src="data:image/png;base64,%s">' % (alt, base64.b64encode(data.getvalue()))
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_TEXT, st)
w.CloseClipboard()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment