Created
June 22, 2012 01:40
-
-
Save mkeeter/2969712 to your computer and use it in GitHub Desktop.
TF2 code to png converter
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
with open('data.txt','r') as f: | |
lines = f.readlines() | |
splits = [] | |
for i in range(len(lines)): | |
# Clean up crufty data | |
lines[i] = lines[i][:-1].replace('\t',' ') | |
lines[i] = lines[i].replace('o', '0') | |
lines[i] = lines[i].replace('X','x') | |
lines[i] = lines[i].replace('0x',' ') | |
lines[i] = lines[i].replace('<<',' ') | |
lines[i] = lines[i].replace('"',' ') | |
splits += [filter(lambda l: bool(l), lines[i].split(' '))] | |
for id in ['1','2','3','4','5','6','7+8']: | |
# Select only the desired bananas | |
data = filter(lambda s: s[0] in id, splits) | |
# Figure out how big the image is | |
maxByte = max(map(lambda d: int(d[1]) + len(d) - 2, data)) | |
# Create a dictionary per character for voting purposes | |
image = [{} for byte in range(maxByte)] | |
for d in data: | |
offset = int(d[1]) | |
length = len(d) - 2 | |
d_ = d[2:] | |
for index in range(offset, offset+length): | |
try: | |
value = chr(int(d_[0], 16)) | |
image[index][value] = image[index].get(value, 0) + 1 | |
except: | |
pass | |
d_ = d_[1:] | |
missing = 0 | |
ambiguous = 0 | |
with open('%s.png' % id, 'wb') as f: | |
for byte in image: | |
if byte: | |
# Pick whichever option has the most 'votes' | |
best = max(byte.keys(), key=lambda k: byte[k]) | |
# If there was a tie, make a note. | |
if sum(byte[k] == byte[best] for k in byte.keys()) > 1: | |
ambiguous += 1 | |
# Write to the image file | |
f.write(max(byte.keys(), key=lambda k: byte[k])) | |
else: | |
# Write a dummy byte | |
f.write(chr(128)) | |
missing += 1 | |
print 'Image %s:\t%i missing, %i ambiguous' % (id, missing, ambiguous) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment