Skip to content

Commit 23ea364

Browse files
committed
Allow 1 mode images in MorphOp get_on_pixels()
1 parent ca21683 commit 23ea364

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

Tests/test_imagemorph.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,10 @@ def test_corner() -> None:
188188
assert len(coords) == 4
189189
assert tuple(coords) == ((2, 2), (4, 2), (2, 4), (4, 4))
190190

191-
coords = mop.get_on_pixels(Aout)
192-
assert len(coords) == 4
193-
assert tuple(coords) == ((2, 2), (4, 2), (2, 4), (4, 4))
191+
for image in (Aout, Aout.convert("1")):
192+
coords = mop.get_on_pixels(image)
193+
assert len(coords) == 4
194+
assert tuple(coords) == ((2, 2), (4, 2), (2, 4), (4, 4))
194195

195196

196197
def test_mirroring() -> None:

src/PIL/ImageMorph.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,15 @@ def match(self, image: Image.Image) -> list[tuple[int, int]]:
232232
return _imagingmorph.match(bytes(self.lut), image.getim())
233233

234234
def get_on_pixels(self, image: Image.Image) -> list[tuple[int, int]]:
235-
"""Get a list of all turned on pixels in a binary image
235+
"""Get a list of all turned on pixels in a 1 or L mode image.
236236
237237
Returns a list of tuples of (x,y) coordinates
238238
of all matching pixels. See :ref:`coordinate-system`."""
239239

240-
if image.mode != "L":
241-
msg = "Image mode must be L"
240+
if image.mode == "1":
241+
image = image.convert("L")
242+
elif image.mode != "L":
243+
msg = "Image mode must be 1 or L"
242244
raise ValueError(msg)
243245
return _imagingmorph.get_on_pixels(image.getim())
244246

0 commit comments

Comments
 (0)