#! /usr/bin/env python # -*- coding: latin-1 -*- """ This module creates an image of a numpy 2D array History: -------- v0.0.1 : 6/10/2006 - Adaptations to the new version of the numpy package : numpy.arange -> numpy.arange v0.0.0 : 07/2006 - creation """ import os import time import glob import sys import os.path import re import fpformat from optparse import OptionParser import numpy import Image, ImagePalette, ImageChops from ColorTable import * PALETTE_LENGTH=256 # number of different values allowed when using a palette # exceptions InvalidData="InvalidData" UnknownColormap="UnknownColormap" class ColorTableWinget: """ This class transforms a data array into a PIL image """ def __init__(self,data,colormap="rainbow",val_min=None,val_max=None,invalid=None,slope=1., offset=0., calibration_equation=0): """ constructor @param data the input data array (numpy one) @param colormap name of the colormap to use. See ColorTable.py for definitions @param val_min the minimal valid value @param val_max the maximal valid value @param invalid invalid value @param slope slope of the graduations. @param offset offet of the graduations. @param calibration_equation equation of the graduations (linear) """ if data is None: raise InvalidData self.data=data # the input array self.colortable=ColorTable(colormap) # the available colortables self.colormap=colormap # name of the colormap # all datas are stored in floats if val_min!=None: self.val_min=float(val_min) else: self.val_min=self.data.min() if val_max!=None: self.val_max=float(val_max) else: self.val_max=self.data.max() if invalid!=None: self.invalid=float(invalid) # the calibration coefficients self.slope=slope # calibration slope self.offset=offset # calibration offset self.calibration_equation=calibration_equation # The calibration equation to use : 0->(slope*x+offset) ; 1->(slope*x-offset) # 255 is PALETTE_LENGTH-1, -1 because of the interval inc=float(self.val_max-self.val_min)/(255.) #---build the mask of the invalid values invalid_mask=numpy.not_equal(data,self.invalid) invalid_mask=invalid_mask.astype(numpy.uint8) # apply the calibration if self.calibration_equation==0: self.data=self.offset+self.slope*self.data else : raise ValueError, "Invalid calibration equation" #---build the palette data array palette_data=numpy.zeros(self.data.shape,numpy.uint8) #---turn the data into 8bits indexes for applying the color palette if self.val_min is None: self.val_min=min(self.data.flat) if self.val_max is None: self.val_max=max(self.data.flat) # apply a (min,max) threshold if needed (ie if val_min or val_max is given) self.data=numpy.clip(self.data,self.val_min,self.val_max) if inc is None: # inc is not given -> compute it # 254 is PALETTE_LENGTH-2, -1 because index 0 is the invalid value, and -1 because of the interval inc=float(val_max-val_min)/(255.) palette_data=((self.data-val_min)/inc).astype(numpy.uint8) #---Apply the mask of invalid datas palette_data=(palette_data*invalid_mask) #---Build the 8bits palette image--- self.pil_img=Image.fromstring('P',palette_data.shape, palette_data.tostring()) #---Apply the colormap to it--- # instantiate the colormap self.pil_img.putpalette(self.colortable.get_palette(self.colormap))