ImageStat.py 3.8 KB

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