mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-15 20:06:28 +03:00
adding three-color feature to ImageOps.colorize
This commit is contained in:
parent
937443f2a6
commit
63d8637bb8
|
@ -136,28 +136,50 @@ def autocontrast(image, cutoff=0, ignore=None):
|
||||||
return _lut(image, lut)
|
return _lut(image, lut)
|
||||||
|
|
||||||
|
|
||||||
def colorize(image, black, white):
|
def colorize(image, black, white, mid=None, midpoint=128):
|
||||||
"""
|
"""
|
||||||
Colorize grayscale image. The **black** and **white**
|
Colorize grayscale image.
|
||||||
arguments should be RGB tuples; this function calculates a color
|
This function calculates a color wedge mapping all
|
||||||
wedge mapping all black pixels in the source image to the first
|
black pixels in the source image to the first
|
||||||
color, and all white pixels to the second color.
|
color, and all white pixels to the second color. If
|
||||||
|
mid is specified, it uses three color mapping.
|
||||||
|
The **black** and **white**
|
||||||
|
arguments should be RGB tuples; optionally you can use
|
||||||
|
three color mapping by also specifying **mid**, and
|
||||||
|
optionally, **midpoint** (which is the integer value
|
||||||
|
in [0, 255] corresponding to the midpoint color,
|
||||||
|
default 128).
|
||||||
|
|
||||||
:param image: The image to colorize.
|
:param image: The image to colorize.
|
||||||
:param black: The color to use for black input pixels.
|
:param black: The color to use for black input pixels.
|
||||||
:param white: The color to use for white input pixels.
|
:param white: The color to use for white input pixels.
|
||||||
|
:param mid: The color to use for midtone input pixels.
|
||||||
|
:param midpoint: the int value [0, 255] for the mid color.
|
||||||
:return: An image.
|
:return: An image.
|
||||||
"""
|
"""
|
||||||
assert image.mode == "L"
|
assert image.mode == "L"
|
||||||
black = _color(black, "RGB")
|
black = _color(black, "RGB")
|
||||||
|
if mid is not None:
|
||||||
|
mid = _color(mid, "RGB")
|
||||||
white = _color(white, "RGB")
|
white = _color(white, "RGB")
|
||||||
red = []
|
red = []
|
||||||
green = []
|
green = []
|
||||||
blue = []
|
blue = []
|
||||||
|
if mid is None:
|
||||||
for i in range(256):
|
for i in range(256):
|
||||||
red.append(black[0] + i * (white[0] - black[0]) // 255)
|
red.append(black[0] + i * (white[0] - black[0]) // 255)
|
||||||
green.append(black[1] + i * (white[1] - black[1]) // 255)
|
green.append(black[1] + i * (white[1] - black[1]) // 255)
|
||||||
blue.append(black[2] + i * (white[2] - black[2]) // 255)
|
blue.append(black[2] + i * (white[2] - black[2]) // 255)
|
||||||
|
else:
|
||||||
|
for i in range(0, midpoint):
|
||||||
|
red.append(black[0] + i * (mid[0] - black[0]) // 255)
|
||||||
|
green.append(black[1] + i * (mid[1] - black[1]) // 255)
|
||||||
|
blue.append(black[2] + i * (mid[2] - black[2]) // 255)
|
||||||
|
for i in range(0, 256 - midpoint):
|
||||||
|
red.append(mid[0] + i * (white[0] - mid[0]) // 255)
|
||||||
|
green.append(mid[1] + i * (white[1] - mid[1]) // 255)
|
||||||
|
blue.append(mid[2] + i * (white[2] - mid[2]) // 255)
|
||||||
|
|
||||||
image = image.convert("RGB")
|
image = image.convert("RGB")
|
||||||
return _lut(image, red + green + blue)
|
return _lut(image, red + green + blue)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user