#!/usr/bin/env python # #abs2png.py: Generate a png image from a abs range image. #The grey level is defined using the min and the max values of Z # # # Copyright (C) 2010 Clement Creusot # # 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 3 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, see . import sys import os import string from PIL import Image import binascii def print_help(): print "Usage: "+sys.argv[0]+" filein.abs[.gz] [fileout.png]" sys.exit() def print_error(str): print "ERROR: "+str sys.exit() # read parameters if (len(sys.argv) == 2): pngfilename = sys.argv[1]+".png"; elif (len(sys.argv) == 3): pngfilename = sys.argv[2]; else: print_help() absfilename = sys.argv[1]; if not os.path.exists(absfilename): print_error("File doesnt exist") if absfilename.rfind(".gz") != -1: import tempfile from subprocess import * tmpdir = tempfile.mkdtemp() call(["cp",absfilename, tmpdir]) gzfilename = os.path.join(tmpdir,os.path.basename(absfilename)) call(["gunzip", gzfilename]); absfilename = gzfilename.replace("abs.gz","abs") absfile = open(absfilename, "r") try: tab = string.split(absfile.readline()) rows = int(tab[0]) tab = string.split(absfile.readline()) columns= int(tab[0]) tab = string.split(absfile.readline()) # comment pixel except ValueError: print_error("Bad header") # Read the flags and the Z coordinate flags = map(int,string.split(absfile.readline())) absfile.readline() #X absfile.readline() #Y Z = map(float,string.split(absfile.readline())) absfile.close() Min = 99999.0 Max = -99999.0 sum = 0.0 count = 0.0 for i,z in enumerate(Z): Z[i] = flags[i]*z if flags[i] == 1: Min = min(Min,z) Max = max(Max,z) sum = sum + z count = count + 1 if count != 0.0 : mean = sum /count else: mean = 0.0 delta = Max - Min print Min, Max, delta, mean img = '' for i,z in enumerate(Z): v = 255-int((z-Min)/delta*255)*flags[i] if v<16: s = '0'+hex(v)[2:] else: s = hex(v)[2:] img = img + str(binascii.unhexlify(s)) pilImage = Image.frombuffer('L',(columns,rows),img,'raw','L',0,1) pilImage.save(pngfilename)