Skip to content

Instantly share code, notes, and snippets.

@larsfu
Created July 29, 2010 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larsfu/499247 to your computer and use it in GitHub Desktop.
Save larsfu/499247 to your computer and use it in GitHub Desktop.
# visualize.py
#
# Copyright 2010 Lars Funke <larsfunke1996@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import sys, string, os
def visualize(map):
if lastset != 1:
shrinkfactor = input('PNG shrink factor (1/x) [6] > ')
if shrinkfactor == '':
shrinkfactor = 6
else:
shrinkfactor = int(shrinkfactor)
quadsize = input('Quadsize [32] > ')
if quadsize == '':
quadsize = 32
else:
quadsize = int(quadsize)
else:
settingsfile = open('output/'+map+'.settings.meta')
settings = settingsfile.read()
settings = settings.split(',')
shrinkfactor = int(settings[0])
quadsize = int(settings[1])
try:
logfile = open('logs/'+map+'.log')
except IOError:
print('The logfile could not be found. Exiting.')
quit()
print('Parsing logfile...')
log = logfile.read()
log = log.split(',')
showkey = False
color_start = [0, 255, 0]
color_end = [255, 0, 0]
counts = {}
values = []
for coord in log:
coord = coord.split(' ')
if coord[0] == '':
break
try:
x = round(int(coord[0])/quadsize)
y = round(int(coord[1])/quadsize)
except ValueError:
print('Error processing coordinate: ' + str(coord))
if x not in counts:
counts[x] = {}
if y not in counts[x]:
counts[x][y] = 1
else:
counts[x][y] += 1
values.append(counts[x][y])
#Find the value that 90% of the coordinates are in
values.sort()
high = values[round(0.90*len(values)-1)]
#Find the maximum values
xmax = 0
ymax = 0
keys = list(counts.keys())
xmax = max(counts)*quadsize
ylist = list([0])
for x in counts:
for y in counts[x]:
ylist.append(y)
ymax = max(ylist)*quadsize
print(' done, '+str(len(log)-1)+' coordinates found.')
def colorFromGradient(percentage):
diff_r = color_end[0]- color_start[0]
diff_g = color_end[1]- color_start[1]
diff_b = color_end[2]- color_start[2]
factor = round(percentage) / 100;
r = round(color_start[0] + diff_r * factor)
g = round(color_start[1] + diff_g * factor)
b = round(color_start[2] + diff_b * factor)
return 'rgb('+str(r)+','+str(g)+','+str(b)+')'
print('Writing SVG...')
opacity = "60"
#SVG Header
svg = '<?xml version="1.0" encoding="UTF-8"?>\n'
svg += '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'
svg += '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events" version="1.1" baseProfile="full" '
svg += 'width="'+str(xmax)+'px" height="'+str(ymax)+'px">\n'
for x in counts:
for y in counts[x]:
color = colorFromGradient(counts[x][y]/high*100)
svg += '<rect x="'+str(x*quadsize-quadsize)+'" y="'+str(y*quadsize-quadsize)+'" width="'+str(quadsize)+'" height="'+str(quadsize)+'" fill="'+color+'" style="opacity:'+opacity+'%" />\n'
svg += '</svg>'
svgfile = open('output/'+map+'.svg', 'w')
svgfile.writelines(svg)
svgfile.close()
print(' done.')
print('Rendering PNG...')
cmd = 'convert -resize '+str(round(xmax/shrinkfactor))+'x'+str(round(ymax/shrinkfactor))+' -depth 16 -background transparent output/'+map+'.svg output/'+map+'.png'
os.system(cmd)
print(' done.')
print('Writing metadata...')
meta = '{"high": '+str(high)+', "deaths": '+str(len(log)-1)+'}'
metafile = open('output/'+map+'.map.meta', 'w')
metafile.write(meta);
metafile.close();
try:
mapsfile = open('output/allmaps.meta', 'r')
maps = mapsfile.read()
maps = maps.replace('[', '')
maps = maps.replace(']', '')
maps = maps.replace('"', '')
maps = maps.split(', ')
mapsfile.close()
except IOError:
maps = []
print('All map file was not found, creating')
if not map in maps:
mapsfile = open('output/allmaps.meta', 'w')
mapsnew = '['
for _map in maps:
if _map != "":
mapsnew += '"'+_map+'", '
mapsnew += '"'+map+'"'
mapsnew += ']'
mapsfile.write(mapsnew)
mapsfile.close()
colorfile = open('output/colors.meta', 'w')
colors = '["rgb('+str(color_start[0])+','+str(color_start[1])+','+str(color_start[2])+')", "rgb('+str(color_end[0])+','+str(color_end[1])+','+str(color_end[2])+')"]'
colorfile.write(colors)
colorfile.close()
settingsfile = open('output/'+map+'.settings.meta', 'w')
settings = str(shrinkfactor)+','+str(quadsize)
settingsfile.write(settings)
settingsfile.close()
print (' done.')
print('This script will visualize the logged deaths.')
map = input('Map to parse > ')
if map == "ALL":
logs = os.listdir("logs");
lastset = int(input("Use settings from last run? (1/0) > "))
for log in logs:
log = log.split(".log")
print('Parsing map: '+log[0])
visualize(log[0])
else:
visualize(map);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment