Skip to content

Instantly share code, notes, and snippets.

@DanielKeep
Created April 24, 2012 02:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielKeep/2475758 to your computer and use it in GitHub Desktop.
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.
"""
DCPU Font converter.
Written by Daniel Keep <daniel.keep@gmail.com>.
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))
@jackgill
Copy link

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).

@DanielKeep
Copy link
Author

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