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
low-level implementation of morphological lookup table evaluation is handled
by the C extension module :mod:`_imagingmorph`. Users should interact
with :class:`LutBuilder` and :class:`MorphOp`, not with the C extension
directly.
Copy link
Member

Choose a reason for hiding this comment

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

I'm interested, why did you feel the need to mention _imagingmorph? _imagingft and _imagingmath aren't mentioned in our docs, for precisely the reason you mention here - we don't want users to deal with them directly. How do you think users would discover _imagingmorph in the first place?

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]

preset names (``"dilation4"``, ``"dilation8"``, ``"erosion4"``,
``"erosion8"``, ``"edge"``), or you may define custom patterns.

Example: creating a LUT for a 2×2 corner detector::
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Example: creating a LUT for a 2×2 corner detector::
For example, creating a LUT for a 2×2 corner detector::

Comment on lines +141 to +149

Relationship to `_imagingmorph`
-------------------------------

The :mod:`PIL.ImageMorph` module provides a Python interface, while the
low-level implementation of morphological lookup table evaluation is handled
by the C extension module :mod:`_imagingmorph`. Users should interact
with :class:`LutBuilder` and :class:`MorphOp`, not with the C extension
directly.
Copy link
Member

Choose a reason for hiding this comment

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

Yep, let's remove mention of internal details.

Suggested change
Relationship to `_imagingmorph`
-------------------------------
The :mod:`PIL.ImageMorph` module provides a Python interface, while the
low-level implementation of morphological lookup table evaluation is handled
by the C extension module :mod:`_imagingmorph`. Users should interact
with :class:`LutBuilder` and :class:`MorphOp`, not with the C extension
directly.

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Comment on lines +101 to +106
im = Image.open("input.png").convert("L")

# Built-in 8-connected dilation
op = ImageMorph.MorphOp(op_name="dilation8")

count, out = op.apply(im)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
im = Image.open("input.png").convert("L")
# Built-in 8-connected dilation
op = ImageMorph.MorphOp(op_name="dilation8")
count, out = op.apply(im)
with Image.open("input.png") as im:
im = im.convert("L")
# Built-in 8-connected dilation
op = ImageMorph.MorphOp(op_name="dilation8")
count, out = op.apply(im)

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