mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-27 00:19:58 +03:00
Fixed ZeroDivisionError
This commit is contained in:
parent
640f55a655
commit
103a5a0b59
|
@ -57,3 +57,13 @@ def test_constant() -> None:
|
||||||
assert st.rms[0] == 128
|
assert st.rms[0] == 128
|
||||||
assert st.var[0] == 0
|
assert st.var[0] == 0
|
||||||
assert st.stddev[0] == 0
|
assert st.stddev[0] == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_zero_count() -> None:
|
||||||
|
im = Image.new("L", (0, 0))
|
||||||
|
|
||||||
|
st = ImageStat.Stat(im)
|
||||||
|
|
||||||
|
assert st.mean == [0]
|
||||||
|
assert st.rms == [0]
|
||||||
|
assert st.var == [0]
|
||||||
|
|
|
@ -120,7 +120,7 @@ class Stat:
|
||||||
@cached_property
|
@cached_property
|
||||||
def mean(self) -> list[float]:
|
def mean(self) -> list[float]:
|
||||||
"""Average (arithmetic mean) pixel level for each band in the image."""
|
"""Average (arithmetic mean) pixel level for each band in the image."""
|
||||||
return [self.sum[i] / self.count[i] for i in self.bands]
|
return [self.sum[i] / self.count[i] if self.count[i] else 0 for i in self.bands]
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def median(self) -> list[int]:
|
def median(self) -> list[int]:
|
||||||
|
@ -141,13 +141,20 @@ class Stat:
|
||||||
@cached_property
|
@cached_property
|
||||||
def rms(self) -> list[float]:
|
def rms(self) -> list[float]:
|
||||||
"""RMS (root-mean-square) for each band in the image."""
|
"""RMS (root-mean-square) for each band in the image."""
|
||||||
return [math.sqrt(self.sum2[i] / self.count[i]) for i in self.bands]
|
return [
|
||||||
|
math.sqrt(self.sum2[i] / self.count[i]) if self.count[i] else 0
|
||||||
|
for i in self.bands
|
||||||
|
]
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def var(self) -> list[float]:
|
def var(self) -> list[float]:
|
||||||
"""Variance for each band in the image."""
|
"""Variance for each band in the image."""
|
||||||
return [
|
return [
|
||||||
|
(
|
||||||
(self.sum2[i] - (self.sum[i] ** 2.0) / self.count[i]) / self.count[i]
|
(self.sum2[i] - (self.sum[i] ** 2.0) / self.count[i]) / self.count[i]
|
||||||
|
if self.count[i]
|
||||||
|
else 0
|
||||||
|
)
|
||||||
for i in self.bands
|
for i in self.bands
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user