ImageStat.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. from __future__ import annotations
  24. import math
  25. from functools import cached_property
  26. from . import Image
  27. class Stat:
  28. def __init__(
  29. self, image_or_list: Image.Image | list[int], mask: Image.Image | None = None
  30. ) -> None:
  31. """
  32. Calculate statistics for the given image. If a mask is included,
  33. only the regions covered by that mask are included in the
  34. statistics. You can also pass in a previously calculated histogram.
  35. :param image: A PIL image, or a precalculated histogram.
  36. .. note::
  37. For a PIL image, calculations rely on the
  38. :py:meth:`~PIL.Image.Image.histogram` method. The pixel counts are
  39. grouped into 256 bins, even if the image has more than 8 bits per
  40. channel. So ``I`` and ``F`` mode images have a maximum ``mean``,
  41. ``median`` and ``rms`` of 255, and cannot have an ``extrema`` maximum
  42. of more than 255.
  43. :param mask: An optional mask.
  44. """
  45. if isinstance(image_or_list, Image.Image):
  46. self.h = image_or_list.histogram(mask)
  47. elif isinstance(image_or_list, list):
  48. self.h = image_or_list
  49. else:
  50. msg = "first argument must be image or list" # type: ignore[unreachable]
  51. raise TypeError(msg)
  52. self.bands = list(range(len(self.h) // 256))
  53. @cached_property
  54. def extrema(self) -> list[tuple[int, int]]:
  55. """
  56. Min/max values for each band in the image.
  57. .. note::
  58. This relies on the :py:meth:`~PIL.Image.Image.histogram` method, and
  59. simply returns the low and high bins used. This is correct for
  60. images with 8 bits per channel, but fails for other modes such as
  61. ``I`` or ``F``. Instead, use :py:meth:`~PIL.Image.Image.getextrema` to
  62. return per-band extrema for the image. This is more correct and
  63. efficient because, for non-8-bit modes, the histogram method uses
  64. :py:meth:`~PIL.Image.Image.getextrema` to determine the bins used.
  65. """
  66. def minmax(histogram: list[int]) -> tuple[int, int]:
  67. res_min, res_max = 255, 0
  68. for i in range(256):
  69. if histogram[i]:
  70. res_min = i
  71. break
  72. for i in range(255, -1, -1):
  73. if histogram[i]:
  74. res_max = i
  75. break
  76. return res_min, res_max
  77. return [minmax(self.h[i:]) for i in range(0, len(self.h), 256)]
  78. @cached_property
  79. def count(self) -> list[int]:
  80. """Total number of pixels for each band in the image."""
  81. return [sum(self.h[i : i + 256]) for i in range(0, len(self.h), 256)]
  82. @cached_property
  83. def sum(self) -> list[float]:
  84. """Sum of all pixels for each band in the image."""
  85. v = []
  86. for i in range(0, len(self.h), 256):
  87. layer_sum = 0.0
  88. for j in range(256):
  89. layer_sum += j * self.h[i + j]
  90. v.append(layer_sum)
  91. return v
  92. @cached_property
  93. def sum2(self) -> list[float]:
  94. """Squared sum of all pixels for each band in the image."""
  95. v = []
  96. for i in range(0, len(self.h), 256):
  97. sum2 = 0.0
  98. for j in range(256):
  99. sum2 += (j**2) * float(self.h[i + j])
  100. v.append(sum2)
  101. return v
  102. @cached_property
  103. def mean(self) -> list[float]:
  104. """Average (arithmetic mean) pixel level for each band in the image."""
  105. return [self.sum[i] / self.count[i] for i in self.bands]
  106. @cached_property
  107. def median(self) -> list[int]:
  108. """Median pixel level for each band in the image."""
  109. v = []
  110. for i in self.bands:
  111. s = 0
  112. half = self.count[i] // 2
  113. b = i * 256
  114. for j in range(256):
  115. s = s + self.h[b + j]
  116. if s > half:
  117. break
  118. v.append(j)
  119. return v
  120. @cached_property
  121. def rms(self) -> list[float]:
  122. """RMS (root-mean-square) for each band in the image."""
  123. return [math.sqrt(self.sum2[i] / self.count[i]) for i in self.bands]
  124. @cached_property
  125. def var(self) -> list[float]:
  126. """Variance for each band in the image."""
  127. return [
  128. (self.sum2[i] - (self.sum[i] ** 2.0) / self.count[i]) / self.count[i]
  129. for i in self.bands
  130. ]
  131. @cached_property
  132. def stddev(self) -> list[float]:
  133. """Standard deviation for each band in the image."""
  134. return [math.sqrt(self.var[i]) for i in self.bands]
  135. Global = Stat # compatibility