home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pypil112.zip / PIL-1.1.2.zip / Lib / site-packages / PIL / ImageStat.py < prev    next >
Text File  |  2001-05-03  |  4KB  |  164 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # global statistics
  6. #
  7. # History:
  8. #       96-04-05 fl     Created
  9. #       97-05-21 fl     Added mask; added rms, var, stddev attributes
  10. #       97-08-05 fl     Added median
  11. #       98-07-05 hk     Fixed integer overflow error
  12. #
  13. # Notes:
  14. #       This class shows how to implement delayed evaluation of
  15. #       attributes.  To get a certain value, simply access the
  16. #       corresponding attribute.  The __getattr__ dispatcher takes
  17. #       care of the rest.
  18. #
  19. # Copyright (c) Secret Labs AB 1997.
  20. # Copyright (c) Fredrik Lundh 1996-97.
  21. #
  22. # See the README file for information on usage and redistribution.
  23. #
  24.  
  25. import Image
  26. import operator, math
  27.  
  28. class Stat:
  29.     "Get image or feature statistics"
  30.  
  31.     def __init__(self, image_or_list, mask = None):
  32.         try:
  33.             if mask:
  34.                 self.h = image_or_list.histogram(mask)
  35.             else:
  36.                 self.h = image_or_list.histogram()
  37.         except AttributeError:
  38.             self.h = image_or_list # assume it to be a histogram list
  39.         if type(self.h) != type([]):
  40.             raise TypeError, "first argument must be image or list"
  41.         self.bands = range(len(self.h) / 256)
  42.  
  43.     def __getattr__(self, id):
  44.         "Calculate missing attribute"
  45.         if id[:4] == "_get":
  46.             raise AttributeError, id
  47.         # calculate missing attribute
  48.         v = getattr(self, "_get" + id)()
  49.         setattr(self, id, v)
  50.         return v
  51.  
  52.     def _getextrema(self):
  53.         "Get min/max values for each band in the image"
  54.  
  55.         def minmax(histogram):
  56.             n = 255
  57.             x = 0
  58.             for i in range(256):
  59.                 if histogram[i]:
  60.                     n = min(n, i)
  61.                     x = max(x, i)
  62.             return n, x # returns (255, 0) if there's no data in the histogram
  63.  
  64.         v = []
  65.         for i in range(0, len(self.h), 256):
  66.             v.append(minmax(self.h[i:]))
  67.         return v
  68.  
  69.     def _getcount(self):
  70.         "Get total number of pixels in each layer"
  71.  
  72.         v = []
  73.         for i in range(0, len(self.h), 256):
  74.             v.append(reduce(operator.add, self.h[i:i+256]))
  75.         return v
  76.  
  77.     def _getsum(self):
  78.         "Get sum of all pixels in each layer"
  79.  
  80.         v = []
  81.         for i in range(0, len(self.h), 256):
  82.             sum = 0.0
  83.             for j in range(256):
  84.                 sum = sum + j * self.h[i+j]
  85.             v.append(sum)
  86.         return v
  87.  
  88.     def _getsum2(self):
  89.         "Get squared sum of all pixels in each layer"
  90.  
  91.         v = []
  92.         for i in range(0, len(self.h), 256):
  93.             sum2 = 0.0
  94.             for j in range(256):
  95.                 sum2 = sum2 + (j ** 2) * float(self.h[i+j])
  96.             v.append(sum2)
  97.         return v
  98.  
  99.     def _getmean(self):
  100.         "Get average pixel level for each layer"
  101.  
  102.         v = []
  103.         for i in self.bands:
  104.             v.append(self.sum[i] / self.count[i])
  105.         return v
  106.  
  107.     def _getmedian(self):
  108.         "Get median pixel level for each layer"
  109.  
  110.         v = []
  111.         for i in self.bands:
  112.             s = 0
  113.             l = self.count[i]/2
  114.             b = i * 256
  115.             for j in range(256):
  116.                 s = s + self.h[b+j]
  117.                 if s > l:
  118.                     break
  119.             v.append(j)
  120.         return v
  121.  
  122.     def _getrms(self):
  123.         "Get RMS for each layer"
  124.  
  125.         v = []
  126.         for i in self.bands:
  127.             v.append(math.sqrt(self.sum2[i] / self.count[i]))
  128.         return v
  129.  
  130.  
  131.     def _getvar(self):
  132.         "Get variance for each layer"
  133.  
  134.         v = []
  135.         for i in self.bands:
  136.             n = self.count[i]
  137.             v.append((self.sum2[i]-(self.sum[i]**2.0)/n)/n)
  138.         return v
  139.  
  140.     def _getstddev(self):
  141.         "Get standard deviation for each layer"
  142.  
  143.         v = []
  144.         for i in self.bands:
  145.             v.append(math.sqrt(self.var[i]))
  146.         return v
  147.  
  148. Global = Stat # compatibility
  149.  
  150. if __name__ == "__main__":
  151.  
  152.     im = Image.open("Images/lena.ppm")
  153.     
  154.     st = Stat(im)
  155.  
  156.     print "extrema", st.extrema
  157.     print "sum    ", st.sum
  158.     print "mean   ", st.mean
  159.     print "median ", st.median
  160.     print "rms    ", st.rms
  161.     print "sum2   ", st.sum2
  162.     print "var    ", st.var
  163.     print "stddev ", st.stddev
  164.