Skip to content

Commit 40fcf77

Browse files
committed
Allow 1 mode images in apply() and match()
1 parent eab0543 commit 40fcf77

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

Tests/test_imagemorph.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,9 @@ def test_incorrect_mode() -> None:
236236
im = hopper("RGB")
237237
mop = ImageMorph.MorphOp(op_name="erosion8")
238238

239-
with pytest.raises(ValueError, match="Image mode must be L"):
239+
with pytest.raises(ValueError, match="Image mode must be 1 or L"):
240240
mop.apply(im)
241-
with pytest.raises(ValueError, match="Image mode must be L"):
241+
with pytest.raises(ValueError, match="Image mode must be 1 or L"):
242242
mop.match(im)
243243
with pytest.raises(ValueError, match="Image mode must be 1 or L"):
244244
mop.get_on_pixels(im)

src/PIL/ImageMorph.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,10 @@ def apply(self, image: Image.Image) -> tuple[int, Image.Image]:
209209
msg = "No operator loaded"
210210
raise Exception(msg)
211211

212-
if image.mode != "L":
213-
msg = "Image mode must be L"
212+
if image.mode == "1":
213+
image = image.convert("L")
214+
elif image.mode != "L":
215+
msg = "Image mode must be 1 or L"
214216
raise ValueError(msg)
215217
outimage = Image.new(image.mode, image.size, None)
216218
count = _imagingmorph.apply(bytes(self.lut), image.getim(), outimage.getim())
@@ -226,8 +228,10 @@ def match(self, image: Image.Image) -> list[tuple[int, int]]:
226228
msg = "No operator loaded"
227229
raise Exception(msg)
228230

229-
if image.mode != "L":
230-
msg = "Image mode must be L"
231+
if image.mode == "1":
232+
image = image.convert("L")
233+
elif image.mode != "L":
234+
msg = "Image mode must be 1 or L"
231235
raise ValueError(msg)
232236
return _imagingmorph.match(bytes(self.lut), image.getim())
233237

0 commit comments

Comments
 (0)