Convert AsciiMath/MathML to SVG/PNG - python

I'm looking for a way to convert my AsciiMath (or MathML) sources to SVG and/or PNG. I've found a nodeJS library for SVG conversion but calling that from Python is not very convenient and the output is not entirely satisfying.
Taking the fact I'd like to render mathematical formulas to svg/png it seems logical to look for a solution in math libraries (NumPy, SciPy, Pandas, Matplotlib, Sympy, etc...) but to no avail. All my google results combining all possible permutations of asciimath+mathml+svg+png lead to nothing which is strange.
Please recommend me either search patterns to find a solution or share your experiences/ideas to get this seemingly simple job done in Python.
All help would be highly appreciated!

I just created ziamath for exactly this purpose. It comes bundled with the STIX math font, so no setup is required beyond the pip install, but it should also work with other math-enabled fonts. Pure-Python, so it does not need a Latex installation or anything else to work. This first version doesn't quite cover the full MathML specification, but the most useful parts are in there.
To bundle it into an app with something like PyInstaller, you'll need to make sure the STIX font gets included, but that should just be one line in the PyInstaller config.

I have used this for years without issues. It is written in python.
https://sourceforge.net/projects/svgmath/files/svgmath/0.3.3/

Related

QScintilla in PySide

I like PySide, and have used it for a while now, but in the program I am working on at the moment, I need an advanced code editor.
I have found QScintilla, but that is for PyQt. Is that compatible with PySide and if so, how would I go about using it?
I have also looked at PySide-QScintilla, but all that is is a couple of .h files and an __init.py__ and the __init__.py doesn't even have anything relating to the .h files! Has anyone used that? How do you include it in a program?
I also saw qutepart but it seems to be mush too basic for what I need.
I would accept solutions other than QScintilla, but they have to have:
Error Highlighting
Syntax Highlighting
Code Suggestion (a drop down menu while typing)
Line Numbers
Custom Colors
Must be at least GPL (LGPL would be nice)
Any other features are great.
Doing a quick Google search brought up the following:
http://gitorious.org/pyside-qscintilla
https://github.com/LuaDist/scintilla/tree/master/qt/ScintillaEditPy
I suspect the first one is a direct port, whereas the second is some kind of Scintilla wrapper instead of a QScintilla port.

Trying to write Python script to remove duplicate fonts Mac OSX

I am trying to write a utility to check through the fonts on OSX and remove duplicates - so far I've had success iterating through the folder structure and writing out an xml file containing fonts and paths. The drawback is that this doesn't find duplicates as the same font can be installed twice in different locations but with a different file name; font family information is internal to the font file.
I have found this: List of installed fonts OS X / C
import Cocoa
manager = Cocoa.NSFontManager.sharedFontManager()
font_families = list(manager.availableFontFamilies())
Which uses PyObjC to list installed fonts. This looks like a possible solution but I'm not a heavy weight coder and know nothing about PyObjC! I did have a play around in a terminal with dir(manager)and dir(NSFontManager)to see if I could find a method that returned the file paths of installed fonts but to no avail.
Any help gratefully received.
Best wishes
//Simon
Install the fonttools package and take a look at this blog post. There's a script there that gives the font name of its first argument and I'm sure you could adapt it to your use case.
Thanks once again for the replies - I just wanted to update this in the hope it helps. I'm looking at fontTools now, but admit I am having trouble finding documentation. According to the developer's page there are libraries to handle other fonts apart from ttf, but I'm still looking with little success just yet!
In the mean time, I did turn up this page on Using Apple Font tools, which seems get half way to what I need - ie listing installed fonts and locations. It looks like the ftxinstalledfonts command will accept a pipe so in theory I could pipe the output to my existing python script that builds an xml database. I'm assuming I can do this by calling ftxinstalledfonts via the sys module but am not clear as to how to get the output... I'll go and plunder the python docs but if there's no luck and someone has 10 secs to give a pointer I'd be grateful.
As a quick aside, the Font Geek site above has some very useful stuff about TTX as well.
Best wishes
//Simon

Replacing Functionality of PIL (ImageDraw) in Google App Engine (GAE)

So, Google App Engine doesn't look like it's going to include the Python Imaging Library anytime soon. There is an images api, but it's paltry, and inadequate for what I need.
I'm wondering what Python only (no C-extensions) there are that can replace the Image.paste and the ImageDraw modules. I don't want to write them myself, but that is an option. I'm also open to other solutions, such as "do the processing somewhere else, then call via api", if they're not too ugly. (For the record, the solution I just suggested seems pretty ugly to me.)
How have others gotten around this?
(I'm not wedded to GAE, just exploring, and this looks like a deal breaker for my app.)
Notes:
For me, crop, resize is not enough. In particular I need
paste (replace part of an image with another.... can be faked with "compose")
draw (for drawing gridlines, etc. Can be faked as well)
text (write text on an image, much harder to fake, unless someone wants to correct me)
My skimpygimpy.sourceforge.net will do drawing and text, but it won't edit existing images (but it could be modified for that, of course, if you want to dive in). It is pure python. see it working on google apps, for example at
http://piopio.appspot.com/W1200_1400.stdMiddleware#Header51,
That's an experimental site that I'll be messing with. The link may not work forever.
Your assumption is wrong. If you use the Python 2.7 runtime, you can use PIL (version 1.1.7) as documented here: https://developers.google.com/appengine/docs/python/tools/libraries27.
This article also explains how to enable PIL for your app.
BTW, the last comment in the bug you referenced also mentions it.
I don't know if it has all features you want, but I have been messing with PNGCanvas, and it does some things I have done before with PIL
Now according to this ticket "On the Python 2.7 runtime, you can import PIL and use it directly. It's the real PIL, not a wrapper around the images API."

Is there a python equivalent of the prefuse visualization toolkit?

The prefuse visualization toolkit is pretty nice, but for Java. I was wondering if there was something similar for python. My primary interest is being able to navigate dynamic graphs.
I know this is not exactly python, but you could use prefuse in python through jython
Something along the lines of:
Add prefuse to your path:
export JYTHONPATH=$JYTHONPATH:prefuse.jar
and
>>> import prefuse
from your jython machinery
this guy has an example of using prefuse from jython here
You might want to check out SUMMON, a visualization system that uses python but handles fairly large data sets. There's an impressive video of visualizing and navigating a massive tree. (Can't post the link because I'm a first time poster. It's on the SUMMON front page.)
If you're using a Mac, check out NodeBox. One extension it offers is a graph library that looks pretty good. Poke around in the NodeBox gallery some to find something similar to your problem and it should have some helpful links.
This is well after OP, but just in case:
pydot. Allows generation & rendering of graphs. If you need graph algorithms (transitive closure etc.) also look at pygraphlib which extends and integrates pydot.
Note that neither allows interactive editing of the rendered diagram. They both use graphviz to generate output.
You could try using prefuse with JPype, if you can't find a suitable replacement.
Note that prefuse now has the flare package which uses flash.
Connect that to a Python backend via web2py and you've got a great web app (just an idea).
MayaVi

Editing Photoshop PSD text layers programmatically

I have a multi-layered PSD, with one specific layer being non-rasterized text. I'm trying to figure out a way I can, from a bash/perl/python/whatever-else program:
load the PSD
edit the text in said layer
flatten all layers in the image
save as a web-friendly format like PNG or JPG
I immediately thought of ImageMagick, but I don't think I can edit the text layer through IM. If I can accomplish the first two steps some other programmatic way, I can always use ImageMagick to perform the last two steps.
After a couple of hours of googling and searching CPAN and PyPI, I still have found nothing promising. Does anyone have advice or ideas on the subject?
If you don't like to use the officially supported AppleScript, JavaScript, or VBScript, then there is also the possibility to do it in Python. This is explained in the article Photoshop scripting with Python, which relies on Photoshop's COM interface.
I have not tried it, so in case it does not work for you:
If your text is preserved after conversion to SVG then you can simply replace it by whatever tool you like. Afterwards, convert it to PNG (eg. by inkscape --export-png=...).
The only way I can think of to automate the changing of text inside of a PSD would be to use a regex based substitution.
Create a very simple picture in Photoshop, perhaps a white background and a text layer, with the text being a known length.
Search the file for your text, and with a hex editor, search nearby for the length of the text (which may or may not be part of the file format).
Try changing the text, first to a string of the same length, then to something shorter/longer.
Open in Photoshop after each change to see if the file is corrupt.
This method, if viable, will only work if the layer in question contains a known string, which can be substituted for your other value. Note that I have no idea whether this will work, as I don't have Photoshop on this computer to try this method out. Perhaps you can make it work?
As for converting to png, I am at a loss. If the replacing script is in Python, you may be able to do it with the Python Imaging Library (PIL, which seems to support it), but otherwise you may just have to open Photoshop to do the conversion. Which means that it probably wouldn't be worth it to change the text pragmatically in the first place.
Have you considered opening and editing the image in The GIMP? It has very good PSD support, and can be scripted in several languages.
Which one you use depends in part on your platform, the Perl interface didn't work on Windows the last I knew. I believe Scheme is supported in all ports.
You can use Photoshop itself to do this with OLE. You will need to install Photoshop, of course. Win32::OLE in Perl or similar module in Python. See http://www.adobe.com/devnet/photoshop/pdfs/PhotoshopScriptingGuide.pdf
If you're going to automate Photoshop, you pretty much have to use Photoshop's own scripting systems. I don't think there's a way around that.
Looking at the problem a different way, can you export from Photoshop to some other format which supports layers, like PNG, which is editable by ImageMagick?
You can also try this using Node.js. I made a PSD command-line tool
One-line command install (needs NodeJS/NPM installed)
npm install -g psd-cli
You can then use it by typing in your terminal
psd myfile.psd -t
You can check out the code to use it from another node script or use it through your shell is from another Bash/Perl/whatever script.

Categories

Resources