Added type hints

This commit is contained in:
Andrew Murray 2023-12-14 23:10:30 +11:00
parent e5ee034675
commit ac38a91816

View File

@ -18,7 +18,7 @@
from . import Image from . import Image
def constant(image, value): def constant(image: Image.Image, value: int) -> Image.Image:
"""Fill a channel with a given gray level. """Fill a channel with a given gray level.
:rtype: :py:class:`~PIL.Image.Image` :rtype: :py:class:`~PIL.Image.Image`
@ -27,7 +27,7 @@ def constant(image, value):
return Image.new("L", image.size, value) return Image.new("L", image.size, value)
def duplicate(image): def duplicate(image: Image.Image) -> Image.Image:
"""Copy a channel. Alias for :py:meth:`PIL.Image.Image.copy`. """Copy a channel. Alias for :py:meth:`PIL.Image.Image.copy`.
:rtype: :py:class:`~PIL.Image.Image` :rtype: :py:class:`~PIL.Image.Image`
@ -36,7 +36,7 @@ def duplicate(image):
return image.copy() return image.copy()
def invert(image): def invert(image: Image.Image) -> Image.Image:
""" """
Invert an image (channel). :: Invert an image (channel). ::
@ -49,7 +49,7 @@ def invert(image):
return image._new(image.im.chop_invert()) return image._new(image.im.chop_invert())
def lighter(image1, image2): def lighter(image1: Image.Image, image2: Image.Image) -> Image.Image:
""" """
Compares the two images, pixel by pixel, and returns a new image containing Compares the two images, pixel by pixel, and returns a new image containing
the lighter values. :: the lighter values. ::
@ -64,7 +64,7 @@ def lighter(image1, image2):
return image1._new(image1.im.chop_lighter(image2.im)) return image1._new(image1.im.chop_lighter(image2.im))
def darker(image1, image2): def darker(image1: Image.Image, image2: Image.Image) -> Image.Image:
""" """
Compares the two images, pixel by pixel, and returns a new image containing Compares the two images, pixel by pixel, and returns a new image containing
the darker values. :: the darker values. ::
@ -79,7 +79,7 @@ def darker(image1, image2):
return image1._new(image1.im.chop_darker(image2.im)) return image1._new(image1.im.chop_darker(image2.im))
def difference(image1, image2): def difference(image1: Image.Image, image2: Image.Image) -> Image.Image:
""" """
Returns the absolute value of the pixel-by-pixel difference between the two Returns the absolute value of the pixel-by-pixel difference between the two
images. :: images. ::
@ -94,7 +94,7 @@ def difference(image1, image2):
return image1._new(image1.im.chop_difference(image2.im)) return image1._new(image1.im.chop_difference(image2.im))
def multiply(image1, image2): def multiply(image1: Image.Image, image2: Image.Image) -> Image.Image:
""" """
Superimposes two images on top of each other. Superimposes two images on top of each other.
@ -111,7 +111,7 @@ def multiply(image1, image2):
return image1._new(image1.im.chop_multiply(image2.im)) return image1._new(image1.im.chop_multiply(image2.im))
def screen(image1, image2): def screen(image1: Image.Image, image2: Image.Image) -> Image.Image:
""" """
Superimposes two inverted images on top of each other. :: Superimposes two inverted images on top of each other. ::
@ -125,7 +125,7 @@ def screen(image1, image2):
return image1._new(image1.im.chop_screen(image2.im)) return image1._new(image1.im.chop_screen(image2.im))
def soft_light(image1, image2): def soft_light(image1: Image.Image, image2: Image.Image) -> Image.Image:
""" """
Superimposes two images on top of each other using the Soft Light algorithm Superimposes two images on top of each other using the Soft Light algorithm
@ -137,7 +137,7 @@ def soft_light(image1, image2):
return image1._new(image1.im.chop_soft_light(image2.im)) return image1._new(image1.im.chop_soft_light(image2.im))
def hard_light(image1, image2): def hard_light(image1: Image.Image, image2: Image.Image) -> Image.Image:
""" """
Superimposes two images on top of each other using the Hard Light algorithm Superimposes two images on top of each other using the Hard Light algorithm
@ -149,7 +149,7 @@ def hard_light(image1, image2):
return image1._new(image1.im.chop_hard_light(image2.im)) return image1._new(image1.im.chop_hard_light(image2.im))
def overlay(image1, image2): def overlay(image1: Image.Image, image2: Image.Image) -> Image.Image:
""" """
Superimposes two images on top of each other using the Overlay algorithm Superimposes two images on top of each other using the Overlay algorithm
@ -161,7 +161,9 @@ def overlay(image1, image2):
return image1._new(image1.im.chop_overlay(image2.im)) return image1._new(image1.im.chop_overlay(image2.im))
def add(image1, image2, scale=1.0, offset=0): def add(
image1: Image.Image, image2: Image.Image, scale: float = 1.0, offset: float = 0
) -> Image.Image:
""" """
Adds two images, dividing the result by scale and adding the Adds two images, dividing the result by scale and adding the
offset. If omitted, scale defaults to 1.0, and offset to 0.0. :: offset. If omitted, scale defaults to 1.0, and offset to 0.0. ::
@ -176,7 +178,9 @@ def add(image1, image2, scale=1.0, offset=0):
return image1._new(image1.im.chop_add(image2.im, scale, offset)) return image1._new(image1.im.chop_add(image2.im, scale, offset))
def subtract(image1, image2, scale=1.0, offset=0): def subtract(
image1: Image.Image, image2: Image.Image, scale: float = 1.0, offset: float = 0
) -> Image.Image:
""" """
Subtracts two images, dividing the result by scale and adding the offset. Subtracts two images, dividing the result by scale and adding the offset.
If omitted, scale defaults to 1.0, and offset to 0.0. :: If omitted, scale defaults to 1.0, and offset to 0.0. ::
@ -191,7 +195,7 @@ def subtract(image1, image2, scale=1.0, offset=0):
return image1._new(image1.im.chop_subtract(image2.im, scale, offset)) return image1._new(image1.im.chop_subtract(image2.im, scale, offset))
def add_modulo(image1, image2): def add_modulo(image1: Image.Image, image2: Image.Image) -> Image.Image:
"""Add two images, without clipping the result. :: """Add two images, without clipping the result. ::
out = ((image1 + image2) % MAX) out = ((image1 + image2) % MAX)
@ -204,7 +208,7 @@ def add_modulo(image1, image2):
return image1._new(image1.im.chop_add_modulo(image2.im)) return image1._new(image1.im.chop_add_modulo(image2.im))
def subtract_modulo(image1, image2): def subtract_modulo(image1: Image.Image, image2: Image.Image) -> Image.Image:
"""Subtract two images, without clipping the result. :: """Subtract two images, without clipping the result. ::
out = ((image1 - image2) % MAX) out = ((image1 - image2) % MAX)
@ -217,7 +221,7 @@ def subtract_modulo(image1, image2):
return image1._new(image1.im.chop_subtract_modulo(image2.im)) return image1._new(image1.im.chop_subtract_modulo(image2.im))
def logical_and(image1, image2): def logical_and(image1: Image.Image, image2: Image.Image) -> Image.Image:
"""Logical AND between two images. """Logical AND between two images.
Both of the images must have mode "1". If you would like to perform a Both of the images must have mode "1". If you would like to perform a
@ -235,7 +239,7 @@ def logical_and(image1, image2):
return image1._new(image1.im.chop_and(image2.im)) return image1._new(image1.im.chop_and(image2.im))
def logical_or(image1, image2): def logical_or(image1: Image.Image, image2: Image.Image) -> Image.Image:
"""Logical OR between two images. """Logical OR between two images.
Both of the images must have mode "1". :: Both of the images must have mode "1". ::
@ -250,7 +254,7 @@ def logical_or(image1, image2):
return image1._new(image1.im.chop_or(image2.im)) return image1._new(image1.im.chop_or(image2.im))
def logical_xor(image1, image2): def logical_xor(image1: Image.Image, image2: Image.Image) -> Image.Image:
"""Logical XOR between two images. """Logical XOR between two images.
Both of the images must have mode "1". :: Both of the images must have mode "1". ::
@ -265,7 +269,7 @@ def logical_xor(image1, image2):
return image1._new(image1.im.chop_xor(image2.im)) return image1._new(image1.im.chop_xor(image2.im))
def blend(image1, image2, alpha): def blend(image1: Image.Image, image2: Image.Image, alpha: float) -> Image.Image:
"""Blend images using constant transparency weight. Alias for """Blend images using constant transparency weight. Alias for
:py:func:`PIL.Image.blend`. :py:func:`PIL.Image.blend`.
@ -275,7 +279,9 @@ def blend(image1, image2, alpha):
return Image.blend(image1, image2, alpha) return Image.blend(image1, image2, alpha)
def composite(image1, image2, mask): def composite(
image1: Image.Image, image2: Image.Image, mask: Image.Image
) -> Image.Image:
"""Create composite using transparency mask. Alias for """Create composite using transparency mask. Alias for
:py:func:`PIL.Image.composite`. :py:func:`PIL.Image.composite`.
@ -285,7 +291,7 @@ def composite(image1, image2, mask):
return Image.composite(image1, image2, mask) return Image.composite(image1, image2, mask)
def offset(image, xoffset, yoffset=None): def offset(image: Image.Image, xoffset: int, yoffset: int = None) -> Image.Image:
"""Returns a copy of the image where data has been offset by the given """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 distances. Data wraps around the edges. If ``yoffset`` is omitted, it
is assumed to be equal to ``xoffset``. is assumed to be equal to ``xoffset``.