Open In App

Python PIL | ImageOps.colorize() Method

Improve
Improve
Like Article
Like
Save
Share
Report

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images.

PIL.ImageOps.colorize() Colorize grayscale image. The black and white arguments should be RGB tuples; this function calculates a color wedge mapping all black pixels in the source image to the first color, and all white pixels to the second color.

Syntax: PIL.ImageOps.colorize(image, black, white)

Parameters:

image – The image to colorize.

black – The color to use for black input pixels.

white – The color to use for white input pixels.

Returns: An Image .

Image Used:

Code :




   
  
# importing image object from PIL
from PIL import Image, ImageOps
  
# creating an image object
img = Image.open(r"C:\Users\System-Pc\Desktop\pinktree.jpg").convert("L")
  
# image colorize function
img1 = ImageOps.colorize(img, black ="blue", white ="white")
img1.show()


Output:

Another Example: Here we use different parameters.

Image Used:

Code :




   
  
# importing image object from PIL
from PIL import Image, ImageOps
  
# creating an image object
img = Image.open(r"C:\Users\System-Pc\Desktop\bird.jpg").convert("L")
  
# image colorize function
img1 = ImageOps.colorize(img, black ="red", white ="yellow")
img1.show()


Output:



Last Updated : 26 Aug, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads