File: ImageMorph.rst

package info (click to toggle)
pillow 12.1.0-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 72,560 kB
  • sloc: python: 49,748; ansic: 38,748; makefile: 302; sh: 168; javascript: 85
file content (53 lines) | stat: -rw-r--r-- 1,609 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
.. py:module:: PIL.ImageMorph
.. py:currentmodule:: PIL.ImageMorph

:py:mod:`~PIL.ImageMorph` module
================================

The :py:mod:`~PIL.ImageMorph` module allows `morphology`_ operators ("MorphOp") to be
applied to 1 or L mode images::

  from PIL import Image, ImageMorph
  img = Image.open("Tests/images/hopper.bw")
  mop = ImageMorph.MorphOp(op_name="erosion4")
  count, imgOut = mop.apply(img)
  imgOut.show()

.. _morphology: https://en.wikipedia.org/wiki/Mathematical_morphology

In addition to applying operators, you can also analyse images.

You can inspect an image in isolation to determine which pixels are non-empty::

  print(mop.get_on_pixels(img))  # [(0, 0), (1, 0), (2, 0), ...]

Or you can retrieve a list of pixels that match the operator. This is the number of
pixels that will be non-empty after the operator is applied::

  coords = mop.match(img)
  print(coords)  # [(17, 1), (18, 1), (34, 1), ...]
  print(len(coords))  # 550

  imgOut = mop.apply(img)[1]
  print(len(mop.get_on_pixels(imgOut)))  # 550

If you would like more customized operators, you can pass patterns to the MorphOp
class::

  mop = ImageMorph.MorphOp(patterns=["1:(... ... ...)->0", "4:(00. 01. ...)->1"])

Or you can pass lookup table ("LUT") data directly. This LUT data can be constructed
with the :py:class:`~PIL.ImageMorph.LutBuilder`::

  builder = ImageMorph.LutBuilder()
  mop = ImageMorph.MorphOp(lut=builder.build_lut())

.. autoclass:: LutBuilder
    :members:
    :undoc-members:
    :show-inheritance:

.. autoclass:: MorphOp
    :members:
    :undoc-members:
    :show-inheritance: