| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
import sys |
|---|
| 11 |
import pygame |
|---|
| 12 |
|
|---|
| 13 |
if len(sys.argv) < 2: |
|---|
| 14 |
print |
|---|
| 15 |
print "Usage: %s fileInput [fileOutput]" % sys.argv[0] |
|---|
| 16 |
print "fileOutput defaults to 'output.tga' and can be in bmp or tga format." |
|---|
| 17 |
sys.exit() |
|---|
| 18 |
|
|---|
| 19 |
print "-" * 78 |
|---|
| 20 |
print sys.argv[0] |
|---|
| 21 |
print "Script to process an image file and make it a sFont" |
|---|
| 22 |
print "-" * 78 |
|---|
| 23 |
print " Opening file: %s" % sys.argv[1] |
|---|
| 24 |
filename = sys.argv[1] |
|---|
| 25 |
surface = pygame.image.load(filename) |
|---|
| 26 |
charPos = [] |
|---|
| 27 |
|
|---|
| 28 |
if len(sys.argv) >= 3: |
|---|
| 29 |
fOut = sys.argv[2] |
|---|
| 30 |
else: |
|---|
| 31 |
fOut = "output.tga" |
|---|
| 32 |
PINK = (255,0,255) |
|---|
| 33 |
VOID = surface.get_at((0,1)) |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
surface.fill(VOID, (0, 0, surface.get_width(), 1)) |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
print " Analyzing..." |
|---|
| 40 |
print |
|---|
| 41 |
wPixels = enumerate(surface.get_at((i, 0)) for i in xrange(surface.get_width())) |
|---|
| 42 |
|
|---|
| 43 |
charStart = 0 |
|---|
| 44 |
inChar = False |
|---|
| 45 |
warning = False |
|---|
| 46 |
for i, wPixel in wPixels: |
|---|
| 47 |
hPixels = enumerate(surface.get_at((i, j)) for j in xrange(surface.get_height())) |
|---|
| 48 |
for j, hPixel in hPixels: |
|---|
| 49 |
if hPixel != VOID and hPixel != PINK: |
|---|
| 50 |
if not inChar: |
|---|
| 51 |
inChar = True |
|---|
| 52 |
charStart = i - 1 |
|---|
| 53 |
break |
|---|
| 54 |
elif hPixel == VOID and j == surface.get_height() - 1: |
|---|
| 55 |
if inChar: |
|---|
| 56 |
|
|---|
| 57 |
if len(charPos) > 0: |
|---|
| 58 |
lastChar = charPos[len(charPos) - 1] |
|---|
| 59 |
if charStart - lastChar[1] < 2: |
|---|
| 60 |
print " WARNING: character distance too little: %12s %12s" % (str(lastChar), str((charStart, i + 1))) |
|---|
| 61 |
warning = True |
|---|
| 62 |
charPos.append((charStart, i + 1)) |
|---|
| 63 |
inChar = False |
|---|
| 64 |
break |
|---|
| 65 |
|
|---|
| 66 |
print |
|---|
| 67 |
print " Total characters found: %i" % len(charPos) |
|---|
| 68 |
if len(charPos) != 190: |
|---|
| 69 |
print " WARNING: characters should be 190!" |
|---|
| 70 |
warning = True |
|---|
| 71 |
else: |
|---|
| 72 |
print |
|---|
| 73 |
print |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
charStart = 0 |
|---|
| 77 |
for i, char in enumerate(charPos): |
|---|
| 78 |
if char[0] - charStart >= 1: |
|---|
| 79 |
surface.fill(PINK, (charStart, 0, char[0] - charStart, 1)) |
|---|
| 80 |
charStart = char[1] |
|---|
| 81 |
|
|---|
| 82 |
if i == len(charPos) - 1: |
|---|
| 83 |
surface.fill(PINK, (char[1] + 1, 0, surface.get_width() - char[1] - 1, 1)) |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
pygame.image.save(surface, fOut) |
|---|
| 87 |
print " Written file %s" % fOut |
|---|
| 88 |
if warning: |
|---|
| 89 |
print " Please read the WARNINGS and check your file!" |
|---|
| 90 |
print "-" * 78 |
|---|
| 91 |
print "COMPLETED" |
|---|
| 92 |
print "-" * 78 |
|---|