Created
April 24, 2012 02:52
-
-
Save DanielKeep/2475758 to your computer and use it in GitHub Desktop.
Python script to convert the DCPU font from PNG to something directly useable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
DCPU Font converter. | |
Written by Daniel Keep <[email protected]>. | |
Released under the MIT license. | |
""" | |
from PIL import Image | |
font_b = Image.open("font.png").convert("1") | |
glyphs = [] | |
for i in range(128): | |
x = i % 32 | |
y = i // 32 | |
cols = [0, 0, 0, 0] | |
px = x * 4 | |
py = y * 8 | |
for col in range(4): | |
colv = 0 | |
for row in range(8): | |
pv = font_b.getpixel((px+col,py+row)) & 1 | |
colv = colv | (pv << row) | |
cols[col] = colv | |
print "%r:\t%r" % (chr(i), | |
["%08s" % bin(w)[2:] for w in cols]) | |
glyph = (cols[1] | (cols[0] << 8), cols[3] | (cols[2] << 8)) | |
glyphs.append(glyph) | |
# | |
# Output | |
# | |
with open("font.dasm", "wt") as f: | |
for line in __doc__.splitlines(): | |
f.write("; %s\n" % line) | |
for i,glyph in enumerate(glyphs): | |
f.write("dat 0x%04x, 0x%04x ; '%r'\n" % (glyph[0], glyph[1], chr(i))) | |
with open("font.bin", "wb") as f: | |
for i,glyph in enumerate(glyphs): | |
for word in glyph: | |
f.write(chr(word & 0xFF)) | |
f.write(chr(word >> 8)) |
Weirdly, import Image
works for my in my Python 2.7 install, but doesn't work from within Visual Studio (which I was just using for autocomplete). Oh well. Glad you found it useful :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for posting this Daniel, it's really useful. In case anyone else runs into this: I needed to change line 10 to "import Image" (running Python 2.7 on Win7, with PIL 1.1.7).