Pillow/docs/reference/ImageMath.rst

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

133 lines
4.1 KiB
ReStructuredText
Raw Normal View History

2013-10-13 04:40:14 +04:00
.. py:module:: PIL.ImageMath
.. py:currentmodule:: PIL.ImageMath
:py:mod:`~PIL.ImageMath` Module
===============================
2013-10-13 04:40:14 +04:00
The :py:mod:`~PIL.ImageMath` module can be used to evaluate “image expressions”. The
2019-05-08 05:54:12 +03:00
module provides a single :py:meth:`~PIL.ImageMath.eval` function, which takes
an expression string and one or more images.
2013-10-13 04:40:14 +04:00
Example: Using the :py:mod:`~PIL.ImageMath` module
--------------------------------------------------
::
2013-10-13 04:40:14 +04:00
from PIL import Image, ImageMath
2013-10-13 04:40:14 +04:00
2021-02-25 15:41:31 +03:00
with Image.open("image1.jpg") as im1:
with Image.open("image2.jpg") as im2:
2013-10-13 04:40:14 +04:00
2021-02-25 15:41:31 +03:00
out = ImageMath.eval("convert(min(a, b), 'L')", a=im1, b=im2)
out.save("result.png")
2013-10-13 04:40:14 +04:00
.. py:function:: eval(expression, environment)
Evaluate expression in the given environment.
In the current version, :py:mod:`~PIL.ImageMath` only supports
single-layer images. To process multi-band images, use the
2015-10-11 13:24:35 +03:00
:py:meth:`~PIL.Image.Image.split` method or :py:func:`~PIL.Image.merge`
2013-10-13 04:40:14 +04:00
function.
:param expression: A string which uses the standard Python expression
syntax. In addition to the standard operators, you can
also use the functions described below.
:param environment: A dictionary that maps image names to Image instances.
You can use one or more keyword arguments instead of a
dictionary, as shown in the above example. Note that
the names must be valid Python identifiers.
:return: An image, an integer value, a floating point value,
or a pixel tuple, depending on the expression.
Expression syntax
-----------------
Expressions are standard Python expressions, but theyre evaluated in a
non-standard environment. You can use PIL methods as usual, plus the following
set of operators and functions:
Standard Operators
^^^^^^^^^^^^^^^^^^
You can use standard arithmetical operators for addition (+), subtraction (-),
multiplication (*), and division (/).
The module also supports unary minus (-), modulo (%), and power (**) operators.
Note that all operations are done with 32-bit integers or 32-bit floating
point values, as necessary. For example, if you add two 8-bit images, the
result will be a 32-bit integer image. If you add a floating point constant to
an 8-bit image, the result will be a 32-bit floating point image.
You can force conversion using the ``convert()``, ``float()``, and ``int()``
functions described below.
2013-10-13 04:40:14 +04:00
Bitwise Operators
^^^^^^^^^^^^^^^^^
The module also provides operations that operate on individual bits. This
includes and (&), or (|), and exclusive or (^). You can also invert (~) all
pixel bits.
Note that the operands are converted to 32-bit signed integers before the
bitwise operation is applied. This means that youll get negative values if
2023-10-19 11:12:01 +03:00
you invert an ordinary grayscale image. You can use the and (&) operator to
2013-10-13 04:40:14 +04:00
mask off unwanted bits.
Bitwise operators dont work on floating point images.
Logical Operators
^^^^^^^^^^^^^^^^^
Logical operators like ``and``, ``or``, and ``not`` work
2013-10-13 04:40:14 +04:00
on entire images, rather than individual pixels.
An empty image (all pixels zero) is treated as false. All other images are
treated as true.
Note that ``and`` and ``or`` return the last evaluated operand,
2013-10-13 04:40:14 +04:00
while not always returns a boolean value.
Built-in Functions
^^^^^^^^^^^^^^^^^^
These functions are applied to each individual pixel.
.. py:currentmodule:: None
.. py:function:: abs(image)
:noindex:
2013-10-13 04:40:14 +04:00
Absolute value.
.. py:function:: convert(image, mode)
:noindex:
2013-10-13 04:40:14 +04:00
Convert image to the given mode. The mode must be given as a string
constant.
.. py:function:: float(image)
:noindex:
2013-10-13 04:40:14 +04:00
Convert image to 32-bit floating point. This is equivalent to
convert(image, “F”).
.. py:function:: int(image)
:noindex:
2013-10-13 04:40:14 +04:00
Convert image to 32-bit integer. This is equivalent to convert(image, “I”).
Note that 1-bit and 8-bit images are automatically converted to 32-bit
integers if necessary to get a correct result.
.. py:function:: max(image1, image2)
:noindex:
2013-10-13 04:40:14 +04:00
Maximum value.
.. py:function:: min(image1, image2)
:noindex:
2013-10-13 04:40:14 +04:00
Minimum value.