adding tests, updated docstring and comments

This commit is contained in:
tsennott 2018-07-06 18:42:16 -07:00
parent 63d8637bb8
commit adf570a77e
2 changed files with 22 additions and 2 deletions

View File

@ -94,6 +94,22 @@ class TestImageOps(PillowTestCase):
newimg = ImageOps.scale(i, 0.5)
self.assertEqual(newimg.size, (25, 25))
def test_colorize(self):
# Test the colorizing function
# Grab test image
i = hopper("L").resize((15, 16))
# Test original 2-color functionality
ImageOps.colorize(i, 'green', 'red')
# Test new three color functionality (cyanotype colors)
ImageOps.colorize(i,
(32, 37, 79),
(255, 255, 255),
(35, 52, 121),
40)
if __name__ == '__main__':
unittest.main()

View File

@ -147,8 +147,8 @@ def colorize(image, black, white, mid=None, midpoint=128):
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).
in [1, 254] corresponding to where the midpoint color
should be mapped (0 being black and 255 being white).
:param image: The image to colorize.
:param black: The color to use for black input pixels.
@ -158,10 +158,14 @@ def colorize(image, black, white, mid=None, midpoint=128):
:return: An image.
"""
assert image.mode == "L"
# Define colors from arguments
black = _color(black, "RGB")
if mid is not None:
mid = _color(mid, "RGB")
white = _color(white, "RGB")
# Create the mapping
red = []
green = []
blue = []