Skip to content

Conversation

@johnnygitgud
Copy link

This pull request adds documentation for the ImageMorph module
For issue #713 ("Todo: Docs for ImageMorph").

The updated documentation has:

  • Overview of morphological operations in Pillow
  • Explanation of structuring element patterns and ASCII masks
  • Usage examples for LutBuilder and MorphOp
  • Examples demonstrating pattern matching and apply()
  • Information about LUT loading, saving, and reuse
  • Guidance on required image modes

@radarhere radarhere changed the title Added documentation for Image Morph module [ci skip] Added documentation for ImageMorph module Nov 16, 2025
Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for filling this gap in the docs!


Please can you check the Sphinx warnings?

https://app.readthedocs.org/projects/pillow/builds/30336202/?utm_source=pillow&utm_content=notification#294395027--277

ImageMorph.rst:13: WARNING: py:class reference target not found: LutBuilder [ref.class]
ImageMorph.rst:13: WARNING: py:class reference target not found: MorphOp [ref.class]
ImageMorph.rst:54: WARNING: py:class reference target not found: LutBuilder [ref.class]
ImageMorph.rst:59: WARNING: py:class reference target not found: LutBuilder [ref.class]
ImageMorph.rst:62: WARNING: py:class reference target not found: LutBuilder [ref.class]
ImageMorph.rst:83: WARNING: py:meth reference target not found: LutBuilder.get_lut [ref.meth]
ImageMorph.rst:83: WARNING: py:meth reference target not found: LutBuilder.load_lut [ref.meth]
ImageMorph.rst:83: WARNING: py:meth reference target not found: LutBuilder.save_lut [ref.meth]
ImageMorph.rst:87: WARNING: py:class reference target not found: MorphOp [ref.class]
ImageMorph.rst:90: WARNING: py:class reference target not found: MorphOp [ref.class]
ImageMorph.rst:90: WARNING: py:meth reference target not found: MorphOp.apply [ref.meth]
ImageMorph.rst:109: WARNING: py:meth reference target not found: MorphOp.match [ref.meth]
ImageMorph.rst:109: WARNING: py:meth reference target not found: MorphOp.get_on_pixels [ref.meth]
ImageMorph.rst:125: WARNING: py:class reference target not found: LutBuilder [ref.class]
ImageMorph.rst:145: WARNING: py:mod reference target not found: _imagingmorph [ref.mod]
ImageMorph.rst:145: WARNING: py:class reference target not found: LutBuilder [ref.class]
ImageMorph.rst:145: WARNING: py:class reference target not found: MorphOp [ref.class]

Co-authored-by: Hugo van Kemenade <[email protected]>
radarhere and others added 3 commits November 29, 2025 22:20
Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Hugo van Kemenade <[email protected]>

* ``1`` — pixel must be “on”
* ``0`` — pixel must be “off”
* ``-`` — “don’t care” value (ignored during matching)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johnnygitgud considering that our existing documentation says

. or X - Ignore

what made you think that - was the ignore value?


lb = ImageMorph.LutBuilder(op_name="corner")
lb.add_patterns(patterns)
lut = lb.build_lut()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example doesn't work. It fails with

Traceback (most recent call last):
  File "demo.py", line 10, in <module>
    lut = lb.build_lut()
  File "PIL/ImageMorph.py", line 156, in build_lut
    raise Exception(msg)
Exception: Syntax error in pattern "10"

It is, for one thing, missing the 4/N/1/M operation. It feels like you haven't read

Operations:
- 4 - 4 way rotation
- N - Negate
- 1 - Dummy op for no other operation (an op must always be given)
- M - Mirroring
Example::
lb = LutBuilder(patterns = ["4:(... .1. 111)->1"])

If you haven't read the source code, that's kind of a red flag with a documentation suggestion.

Could you explain how you came up with this code?

@radarhere
Copy link
Member

Looking at https://pillow--9302.org.readthedocs.build/en/9302/reference/ImageMorph.html, I feel like the initial description duplicates part of the LutBuilder class description. As I've pointed out in comments here, there have also been some surprising mistakes in this PR. I actually suspect this was generated using AI?

I've created #9349 as an alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants