mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-02-04 13:40:54 +03:00
433 lines
10 KiB
Python
433 lines
10 KiB
Python
#
|
|
# The Python Imaging Library.
|
|
# $Id$
|
|
#
|
|
# standard channel operations
|
|
#
|
|
# History:
|
|
# 1996-03-24 fl Created
|
|
# 1996-08-13 fl Added logical operations (for "1" images)
|
|
# 2000-10-12 fl Added offset method (from Image.py)
|
|
#
|
|
# Copyright (c) 1997-2000 by Secret Labs AB
|
|
# Copyright (c) 1996-2000 by Fredrik Lundh
|
|
#
|
|
# See the README file for information on usage and redistribution.
|
|
#
|
|
|
|
from PIL import Image
|
|
|
|
##
|
|
# The <b>ImageChops</b> module contains a number of arithmetical image
|
|
# operations, called <i>channel operations</i> ("chops"). These can be
|
|
# used for various purposes, including special effects, image
|
|
# compositions, algorithmic painting, and more.
|
|
# <p>
|
|
# At this time, channel operations are only implemented for 8-bit
|
|
# images (e.g. "L" and "RGB").
|
|
# <p>
|
|
# Most channel operations take one or two image arguments and returns
|
|
# a new image. Unless otherwise noted, the result of a channel
|
|
# operation is always clipped to the range 0 to MAX (which is 255 for
|
|
# all modes supported by the operations in this module).
|
|
##
|
|
|
|
##
|
|
# Return an image with the same size as the given image, but filled
|
|
# with the given pixel value.
|
|
#
|
|
# @param image Reference image.
|
|
# @param value Pixel value.
|
|
# @return An image object.
|
|
|
|
def constant(image, value):
|
|
"""Fill a channel with a given grey level.
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
return Image.new("L", image.size, value)
|
|
|
|
##
|
|
# Copy image.
|
|
#
|
|
# @param image Source image.
|
|
# @return A copy of the source image.
|
|
|
|
def duplicate(image):
|
|
"""Copy a channel. Alias for :py:meth:`PIL.Image.Image.copy`.
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
return image.copy()
|
|
|
|
##
|
|
# Inverts an image
|
|
# (MAX - image).
|
|
#
|
|
# @param image Source image.
|
|
# @return An image object.
|
|
|
|
def invert(image):
|
|
"""
|
|
Invert an image (channel).
|
|
|
|
.. code-block:: python
|
|
|
|
out = MAX - image
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
image.load()
|
|
return image._new(image.im.chop_invert())
|
|
|
|
##
|
|
# Compare images, and return lighter pixel value
|
|
# (max(image1, image2)).
|
|
# <p>
|
|
# Compares the two images, pixel by pixel, and returns a new image
|
|
# containing the lighter values.
|
|
#
|
|
# @param image1 First image.
|
|
# @param image1 Second image.
|
|
# @return An image object.
|
|
|
|
def lighter(image1, image2):
|
|
"""
|
|
Compares the two images, pixel by pixel, and returns a new image containing
|
|
the lighter values.
|
|
|
|
.. code-block:: python
|
|
|
|
out = max(image1, image2)
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
image1.load()
|
|
image2.load()
|
|
return image1._new(image1.im.chop_lighter(image2.im))
|
|
|
|
##
|
|
# Compare images, and return darker pixel value
|
|
# (min(image1, image2)).
|
|
# <p>
|
|
# Compares the two images, pixel by pixel, and returns a new image
|
|
# containing the darker values.
|
|
#
|
|
# @param image1 First image.
|
|
# @param image1 Second image.
|
|
# @return An image object.
|
|
|
|
def darker(image1, image2):
|
|
"""
|
|
Compares the two images, pixel by pixel, and returns a new image
|
|
containing the darker values.
|
|
|
|
.. code-block:: python
|
|
|
|
out = min(image1, image2)
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
image1.load()
|
|
image2.load()
|
|
return image1._new(image1.im.chop_darker(image2.im))
|
|
|
|
##
|
|
# Calculate absolute difference
|
|
# (abs(image1 - image2)).
|
|
# <p>
|
|
# Returns the absolute value of the difference between the two images.
|
|
#
|
|
# @param image1 First image.
|
|
# @param image1 Second image.
|
|
# @return An image object.
|
|
|
|
def difference(image1, image2):
|
|
"""
|
|
Returns the absolute value of the pixel-by-pixel difference between the two
|
|
images.
|
|
|
|
.. code-block:: python
|
|
|
|
out = abs(image1 - image2)
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
image1.load()
|
|
image2.load()
|
|
return image1._new(image1.im.chop_difference(image2.im))
|
|
|
|
##
|
|
# Superimpose positive images
|
|
# (image1 * image2 / MAX).
|
|
# <p>
|
|
# Superimposes two images on top of each other. If you multiply an
|
|
# image with a solid black image, the result is black. If you multiply
|
|
# with a solid white image, the image is unaffected.
|
|
#
|
|
# @param image1 First image.
|
|
# @param image1 Second image.
|
|
# @return An image object.
|
|
|
|
def multiply(image1, image2):
|
|
"""
|
|
Superimposes two images on top of each other.
|
|
|
|
If you multiply an image with a solid black image, the result is black. If
|
|
you multiply with a solid white image, the image is unaffected.
|
|
|
|
.. code-block:: python
|
|
|
|
out = image1 * image2 / MAX
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
image1.load()
|
|
image2.load()
|
|
return image1._new(image1.im.chop_multiply(image2.im))
|
|
|
|
##
|
|
# Superimpose negative images
|
|
# (MAX - ((MAX - image1) * (MAX - image2) / MAX)).
|
|
# <p>
|
|
# Superimposes two inverted images on top of each other.
|
|
#
|
|
# @param image1 First image.
|
|
# @param image1 Second image.
|
|
# @return An image object.
|
|
|
|
def screen(image1, image2):
|
|
"""
|
|
Superimposes two inverted images on top of each other.
|
|
|
|
.. code-block:: python
|
|
|
|
out = MAX - ((MAX - image1) * (MAX - image2) / MAX)
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
image1.load()
|
|
image2.load()
|
|
return image1._new(image1.im.chop_screen(image2.im))
|
|
|
|
##
|
|
# Add images
|
|
# ((image1 + image2) / scale + offset).
|
|
# <p>
|
|
# Adds two images, dividing the result by scale and adding the
|
|
# offset. If omitted, scale defaults to 1.0, and offset to 0.0.
|
|
#
|
|
# @param image1 First image.
|
|
# @param image1 Second image.
|
|
# @return An image object.
|
|
|
|
def add(image1, image2, scale=1.0, offset=0):
|
|
"""
|
|
Adds two images, dividing the result by scale and adding the
|
|
offset. If omitted, scale defaults to 1.0, and offset to 0.0.
|
|
|
|
.. code-block:: python
|
|
|
|
out = ((image1 + image2) / scale + offset)
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
image1.load()
|
|
image2.load()
|
|
return image1._new(image1.im.chop_add(image2.im, scale, offset))
|
|
|
|
##
|
|
# Subtract images
|
|
# ((image1 - image2) / scale + offset).
|
|
# <p>
|
|
# Subtracts two images, dividing the result by scale and adding the
|
|
# offset. If omitted, scale defaults to 1.0, and offset to 0.0.
|
|
#
|
|
# @param image1 First image.
|
|
# @param image1 Second image.
|
|
# @return An image object.
|
|
|
|
def subtract(image1, image2, scale=1.0, offset=0):
|
|
"""
|
|
Subtracts two images, dividing the result by scale and adding the
|
|
offset. If omitted, scale defaults to 1.0, and offset to 0.0.
|
|
|
|
.. code-block:: python
|
|
|
|
out = ((image1 - image2) / scale + offset)
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
image1.load()
|
|
image2.load()
|
|
return image1._new(image1.im.chop_subtract(image2.im, scale, offset))
|
|
|
|
##
|
|
# Add images without clipping
|
|
# ((image1 + image2) % MAX).
|
|
# <p>
|
|
# Adds two images, without clipping the result.
|
|
#
|
|
# @param image1 First image.
|
|
# @param image1 Second image.
|
|
# @return An image object.
|
|
|
|
def add_modulo(image1, image2):
|
|
"""Add two images, without clipping the result.
|
|
|
|
.. code-block:: python
|
|
|
|
out = ((image1 + image2) % MAX)
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
image1.load()
|
|
image2.load()
|
|
return image1._new(image1.im.chop_add_modulo(image2.im))
|
|
|
|
##
|
|
# Subtract images without clipping
|
|
# ((image1 - image2) % MAX).
|
|
# <p>
|
|
# Subtracts two images, without clipping the result.
|
|
#
|
|
# @param image1 First image.
|
|
# @param image1 Second image.
|
|
# @return An image object.
|
|
|
|
def subtract_modulo(image1, image2):
|
|
"""Subtract two images, without clipping the result.
|
|
|
|
.. code-block:: python
|
|
|
|
out = ((image1 - image2) % MAX)
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
image1.load()
|
|
image2.load()
|
|
return image1._new(image1.im.chop_subtract_modulo(image2.im))
|
|
|
|
##
|
|
# Logical AND
|
|
# (image1 and image2).
|
|
|
|
def logical_and(image1, image2):
|
|
"""Logical AND between two images.
|
|
|
|
.. code-block:: python
|
|
|
|
out = ((image1 and image2) % MAX)
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
image1.load()
|
|
image2.load()
|
|
return image1._new(image1.im.chop_and(image2.im))
|
|
|
|
##
|
|
# Logical OR
|
|
# (image1 or image2).
|
|
|
|
def logical_or(image1, image2):
|
|
"""Logical OR between two images.
|
|
|
|
.. code-block:: python
|
|
|
|
out = ((image1 or image2) % MAX)
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
image1.load()
|
|
image2.load()
|
|
return image1._new(image1.im.chop_or(image2.im))
|
|
|
|
##
|
|
# Logical XOR
|
|
# (image1 xor image2).
|
|
|
|
def logical_xor(image1, image2):
|
|
"""Logical XOR between two images.
|
|
|
|
.. code-block:: python
|
|
|
|
out = ((bool(image1) != bool(image2)) % MAX)
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
image1.load()
|
|
image2.load()
|
|
return image1._new(image1.im.chop_xor(image2.im))
|
|
|
|
##
|
|
# Blend images using constant transparency weight.
|
|
# <p>
|
|
# Same as the <b>blend</b> function in the <b>Image</b> module.
|
|
|
|
def blend(image1, image2, alpha):
|
|
"""Blend images using constant transparency weight. Alias for
|
|
:py:meth:`PIL.Image.Image.blend`.
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
return Image.blend(image1, image2, alpha)
|
|
|
|
##
|
|
# Create composite using transparency mask.
|
|
# <p>
|
|
# Same as the <b>composite</b> function in the <b>Image</b> module.
|
|
|
|
def composite(image1, image2, mask):
|
|
"""Create composite using transparency mask. Alias for
|
|
:py:meth:`PIL.Image.Image.composite`.
|
|
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
return Image.composite(image1, image2, mask)
|
|
|
|
##
|
|
# Offset image data.
|
|
# <p>
|
|
# Returns a copy of the image where data has been offset by the given
|
|
# distances. Data wraps around the edges. If yoffset is omitted, it
|
|
# is assumed to be equal to xoffset.
|
|
#
|
|
# @param image Source image.
|
|
# @param xoffset The horizontal distance.
|
|
# @param yoffset The vertical distance. If omitted, both
|
|
# distances are set to the same value.
|
|
# @return An Image object.
|
|
|
|
def offset(image, xoffset, yoffset=None):
|
|
"""Returns a copy of the image where data has been offset by the given
|
|
distances. Data wraps around the edges. If **yoffset** is omitted, it
|
|
is assumed to be equal to **xoffset**.
|
|
|
|
:param xoffset: The horizontal distance.
|
|
:param yoffset: The vertical distance. If omitted, both
|
|
distances are set to the same value.
|
|
:rtype: :py:class:`~PIL.Image.Image`
|
|
"""
|
|
|
|
if yoffset is None:
|
|
yoffset = xoffset
|
|
image.load()
|
|
return image._new(image.im.offset(xoffset, yoffset))
|