Reputation: 2545
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.
Upvotes: 0
Views: 258
Reputation: 32929
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:
There is a fourth format, mentioned because it is used internally, is close to what lies inside a PNG file itself, and has some support from the public API. This format is called packed. When packed, each row is a sequence of bytes (integers from 0 to 255), just as it is before PNG scanline filtering is applied. When the bit depth is 8 this is essentially the same as boxed row flat pixel; [...] This format is used by the
Writer.write_packed()
method.
Basically, the bits in the integer value ranging from 0-255 specify if a given pixel in that row is on/off. Using the char
function out of the very same code, try visualizing it by executing something like this in your favorite Python shell:
for row in char("A"):
print bin(row[0])[2:].zfill(8)
You should end up with output like the following:
00000000
00111000
01000100
01111100
01000100
01000100
01000100
00000000
...which is more visible in a monospaced terminal font, but still should be pretty obvious.
Upvotes: 2