In this article, we will learn Colors on an Image using the Pillow module in Python. Let’s discuss some concepts:
- A crucial class within the Python Imaging Library is the Image class. It’s defined within the Image module and provides a PIL image on which manipulation operations are often administered. An instance of this class is often created in several ways: by loading images from a file, creating images from scratch, or as a result of processing other images. We’ll see all these in use.
- Any image consists of pixels, each pixel represents a dot in an image. A pixel contains three values and every value ranges between 0 and 255, representing the quantity of red, green, and blue components. The combination of those forms an actual color of the pixel.
- The ImageColor module contains color tables and converters from CSS3-style color specifiers to RGB tuples. This module is used by PIL.Image.Image.new() and the ImageDraw module, among others.
ImageColor module that contains various formats of representing colors. These formats are as follows:
- String: Colors can also be represented as strings such as red, green, blue, and yellow. They are case insensitive.
- Hexadecimal Color: It is represented as: #rgb or #rrggbb. For example- #ffff00 represents yellow where red is 255, green is 255 and blue is 0. RGB would be a tuple-(255,255,0)
- Cylindrical: It is represented as HSL where H-hue, S-saturation, and L-lightness of color. For example- #ffff00 represents yellow where hue is 0.63, saturation is 1.00, and also the lightness value is 0.50.
Creating Images with colors
Here, we will create Images with colors using Image.new() method.
PIL.Image.new() method creates a new image with the given mode and size. Size is given as a (width, height)-tuple, in pixels. The color is given as a single value for single-band images, and a tuple for multi-band images (with one value for each band). We can also use color names. If the color argument is omitted, the image is filled with zero (this usually corresponds to black). If the color is None, the image is not initialized. This can be useful if you’re going to paste or draw things in the image.
Syntax:
PIL.Image.new(mode, size, color)Parameters:
- mode: The mode to use for the new image. (It could be RGB, RGBA)
- size: A 2-tuple containing (width, height) in pixels.
- color: What color to use for the image. Default is black. If given, this should be a single integer or floating point value for single-band modes, and a tuple for multi-band modes.
Return Value: An Image object.
Python3
from PIL import Image # color --> "red" or (255,0,0) or #ff0000 img = Image.new( 'RGB' ,( 200 , 200 ),( 255 , 0 , 0 )) img.show() |
Output:
Converting color string to RGB Color values
Using the ImageColor module, we can also convert colors to RGB format(RGB tuple) as RGB is very convenient to perform different operations. To do this we will use ImageColor.getgrb() method. The ImageColor.getrgb() Convert a color string to an RGB tuple. If the string cannot be parsed, this function raises a ValueError exception.
Syntax:
PIL.ImageColor.getrgb(color)Parameters:
- color: A color string
Returns: (red, green, blue[, alpha])
Python3
# importing module from PIL import ImageColor # using getrgb for yellow img1 = ImageColor.getrgb( "yellow" ) print (img1) # using getrgb for red img2 = ImageColor.getrgb( "red" ) print (img2) |
Output:
(255, 255, 0) (255, 0, 0)
Converting color string to Grayscale values
The ImageColor.getcolor() Same as getrgb(), but converts the RGB value to a grayscale value if the mode is not color or a palette image. If the string cannot be parsed, this function raises a ValueError exception.
Syntax:
PIL.ImageColor.getcolor(color, mode)Parameters:
- color – A color string
Returns: (graylevel [, alpha]) or (red, green, blue[, alpha])
Python3
# importing module from PIL import ImageColor # using getrgb for yellow img1 = ImageColor.getcolor( "yellow" , 'L' ) print (img1) # using getrgb for red img2 = ImageColor.getcolor( "red" , 'L' ) print (img2) |
Output:
226 76
Change the color by changing the pixel values
We can also change the color of an image to some other color.
Input Image:
Example:
Python3
from PIL import Image img = Image. open ( "flower.jpg" ) img = img.convert( "RGB" ) d = img.getdata() new_image = [] for item in d: # change all white (also shades of whites) # pixels to yellow if item[ 0 ] in list ( range ( 200 , 256 )): new_image.append(( 255 , 224 , 100 )) else : new_image.append(item) # update image data img.putdata(new_image) # save new image img.save( "flower_image_altered.jpg" ) |
Output: