File tree Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change @@ -57,3 +57,13 @@ def test_constant() -> None:
57
57
assert st .rms [0 ] == 128
58
58
assert st .var [0 ] == 0
59
59
assert st .stddev [0 ] == 0
60
+
61
+
62
+ def test_zero_count () -> None :
63
+ im = Image .new ("L" , (0 , 0 ))
64
+
65
+ st = ImageStat .Stat (im )
66
+
67
+ assert st .mean == [0 ]
68
+ assert st .rms == [0 ]
69
+ assert st .var == [0 ]
Original file line number Diff line number Diff line change @@ -120,7 +120,7 @@ def sum2(self) -> list[float]:
120
120
@cached_property
121
121
def mean (self ) -> list [float ]:
122
122
"""Average (arithmetic mean) pixel level for each band in the image."""
123
- return [self .sum [i ] / self .count [i ] for i in self .bands ]
123
+ return [self .sum [i ] / self .count [i ] if self . count [ i ] else 0 for i in self .bands ]
124
124
125
125
@cached_property
126
126
def median (self ) -> list [int ]:
@@ -141,13 +141,20 @@ def median(self) -> list[int]:
141
141
@cached_property
142
142
def rms (self ) -> list [float ]:
143
143
"""RMS (root-mean-square) for each band in the image."""
144
- return [math .sqrt (self .sum2 [i ] / self .count [i ]) for i in self .bands ]
144
+ return [
145
+ math .sqrt (self .sum2 [i ] / self .count [i ]) if self .count [i ] else 0
146
+ for i in self .bands
147
+ ]
145
148
146
149
@cached_property
147
150
def var (self ) -> list [float ]:
148
151
"""Variance for each band in the image."""
149
152
return [
150
- (self .sum2 [i ] - (self .sum [i ] ** 2.0 ) / self .count [i ]) / self .count [i ]
153
+ (
154
+ (self .sum2 [i ] - (self .sum [i ] ** 2.0 ) / self .count [i ]) / self .count [i ]
155
+ if self .count [i ]
156
+ else 0
157
+ )
151
158
for i in self .bands
152
159
]
153
160
You can’t perform that action at this time.
0 commit comments