File: convert_font.py

package info (click to toggle)
cbios 0.21-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 736 kB
  • ctags: 1,196
  • sloc: asm: 6,739; ruby: 378; xml: 209; python: 124; makefile: 112; sed: 1
file content (39 lines) | stat: -rw-r--r-- 895 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# $Id: convert_font.py,v 1.1 2004/12/30 16:40:51 mthuurne Exp $
# Converts a 256x64 image to assembly (db) font data.

import sys
# Python Imaging Library
# http://www.pythonware.com/products/pil/
import Image

if len(sys.argv) != 2:
	print >>sys.stderr, 'Usage: python convert_font.py <image_file>'
	sys.exit(2)
else:
	fileName = sys.argv[1]

image = Image.open(fileName)

# Check size.
assert image.size == (256, 64)

# Convert to monochrome.
image = image.convert('L').point(lambda i: i >= 128 and 255)

# Output assembly.
print 'B_Font:'
for char in range(256):
	bx = (char % 32) * 8
	by = (char / 32) * 8
	pattern = []
	for y in range(by, by + 8):
		byte = 0
		for x in range(bx, bx + 8):
			byte <<= 1
			if image.getpixel( (x, y) ):
				byte |= 1
		pattern.append(byte)
	print ' ' * 16 + 'db' + ' ' * 6 + ','.join([
		'$%02X' % byte for byte in pattern
		]) + ' ' * 9 + '; ' + str(char)