From 9b7200d2b439a4a41411e074e0d6cc453a8e3e57 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 23 Dec 2025 12:50:26 +1100 Subject: [PATCH] Allow 1 mode images in MorphOp get_on_pixels() --- Tests/test_imagemorph.py | 9 +++++---- src/PIL/ImageMorph.py | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Tests/test_imagemorph.py b/Tests/test_imagemorph.py index ca192a809..59d1b0645 100644 --- a/Tests/test_imagemorph.py +++ b/Tests/test_imagemorph.py @@ -188,9 +188,10 @@ def test_corner() -> None: assert len(coords) == 4 assert tuple(coords) == ((2, 2), (4, 2), (2, 4), (4, 4)) - coords = mop.get_on_pixels(Aout) - assert len(coords) == 4 - assert tuple(coords) == ((2, 2), (4, 2), (2, 4), (4, 4)) + for image in (Aout, Aout.convert("1")): + coords = mop.get_on_pixels(image) + assert len(coords) == 4 + assert tuple(coords) == ((2, 2), (4, 2), (2, 4), (4, 4)) def test_mirroring() -> None: @@ -239,7 +240,7 @@ def test_incorrect_mode() -> None: mop.apply(im) with pytest.raises(ValueError, match="Image mode must be L"): mop.match(im) - with pytest.raises(ValueError, match="Image mode must be L"): + with pytest.raises(ValueError, match="Image mode must be 1 or L"): mop.get_on_pixels(im) diff --git a/src/PIL/ImageMorph.py b/src/PIL/ImageMorph.py index bd70aff7b..d6d0f8296 100644 --- a/src/PIL/ImageMorph.py +++ b/src/PIL/ImageMorph.py @@ -232,13 +232,13 @@ class MorphOp: return _imagingmorph.match(bytes(self.lut), image.getim()) def get_on_pixels(self, image: Image.Image) -> list[tuple[int, int]]: - """Get a list of all turned on pixels in a binary image + """Get a list of all turned on pixels in a 1 or L mode image. Returns a list of tuples of (x,y) coordinates of all matching pixels. See :ref:`coordinate-system`.""" - if image.mode != "L": - msg = "Image mode must be L" + if image.mode not in ("1", "L"): + msg = "Image mode must be 1 or L" raise ValueError(msg) return _imagingmorph.get_on_pixels(image.getim())