diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 11cf3619d..63ec0d2e4 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -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() diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index 33196d5ed..29dec124d 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -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 = []