All pastes #1386989 Raw Edit

prac2-T

public python v1 · immutable
#1386989 ·published 2009-04-09 10:56 UTC
rendered paste body
#!/usr/bin/python# -*- coding: utf-8 -*-import sysimport getoptimport arraydef main():   if len(sys.argv) < 3:      print "Usage: %s <image_IN.tlx> <image_OUT.tlx>" % sys.argv[0]      return 1   var = TLXImage(sys.argv[1], sys.argv[2])   var.calcHistograms()   var.cutQueues()class TLXImage:   def __init__(self, imageName, imageOUT):       self.imageOUT = imageOUT       self.imageBytes = file(imageName).read()       self.version = self.imageBytes[0:4]       self.numBands = self.toNumber(self.imageBytes[4:6])       self.numCols = self.toNumber(self.imageBytes[6:8])       self.numLines = self.toNumber(self.imageBytes[8:10])       self.histograms = [[0 for col in range(256)] for row in range(self.numBands)]   def toNumber(self, arr):       return ord(arr[1]) << 8 | ord(arr[0])   def calcHistograms(self):       fc = self.numCols * self.numLines       for i in range(self.numBands):           for j in range(10 + i*fc, 10 + (i + 1)*fc):               self.histograms[i][ ord(self.imageBytes[j]) ] += 1           file = open("histo00" + str(i) + ".bin", "wb")           #file.write(self.histograms[i])	   for k in self.histograms[i]:	     file.write(str(k))           file.close()   def cutQueues(self):       fc = self.numCols * self.numLines       zerofive = 0.05 * fc       self.imageBytes = list(self.imageBytes)       for i in range(self.numBands):           # calc min and max           min = 0           j = 0	           while True:               min += self.histograms[i][j]               j += 1               if min >= zerofive:                   break           max = 0           j = 255           while True:               max += self.histograms[i][j]               j -= 1               if max >= zerofive:                   break           # cut the queue           factor = 255 / (max - min)           for j in range(10 + i*fc, 10 + (i + 1)*fc):             self.imageBytes[j] = str((ord(self.imageBytes[j]) - min) * factor)       self.imageBytes = ''.join(self.imageBytes)       file = open(self.imageOUT, "wb")       file.write(self.imageBytes)       file.close()if __name__ == "__main__":   main()