I’m trying to modify this code (http://code.google.com/p/pypng/source/browse/trunk/code/texttopng) so that it can render extended ASCII symbols. Currently I’m trying to figure out how storing the fonts in hexadecimal format works, but after extensive googling around I haven’t been able to understand it, nor extend it.
And another problem I’m trying to solve is to render even multi-line documents with this script. I’m thinking it’s something to do with this line of code
return x,y,itertools.imap(lambda row: itertools.chain(*row),
zip(*map(char, m)))
Thank you.
Basically, don’t do that. It is somewhat obscure, esoteric, hard to read and very limited, as you have noticed yourself. The Python Imaging Library offers text rendering to PNG and JPEG using Freetype2, which is a much saner and feature-complete approach.
Now we have that out of the way, the representation is quite simple and by the way also explained in the documentation of the png module in the standard library:
Basically, the bits in the integer value ranging from 0-255 specify if a given pixel in that row is on/off. Using the
charfunction out of the very same code, try visualizing it by executing something like this in your favorite Python shell:You should end up with output like the following:
…which is more visible in a monospaced terminal font, but still should be pretty obvious.