Python Pillow colors on images
Colors in Python Pillow Images
ImageColor contains colors in various formats, arranged in a table format. It also includes a converter from CSS3-style color specifiers to RGB primitives.
Color Names
The ImageColor module supports the following string formats:
- Hexadecimal color specifications, given as #rgb or #rrggbb. For example, #00ff00 represents pure green.
-
The hexadecimal color 00ff00 has a red value of 0 (0% red), a green value of 255 (100% green), and an RGB blue value of 0 (0% blue).
-
The color #00ff00 in Cylindrical-scale notation (also known as HSL) has a hue of 0.33 and a saturation of 0. 1.00, while 00ff00 has a brightness of 0.50.
-
The image color module provides approximately 140 standard color names, based on the colors supported by the X Window System and most web browsers. Color names are case-insensitive.
ImageColor.getrgb() Method
Converts a color string to an RGB tuple. If the string cannot be parsed, this function raises a ValueError exception.
Syntax
PIL.ImageColor.getrgb(color)
Where.
- Parameters: color – A color string
-
Return Value: (red, green, blue [, alpha]).
Example 1
from PIL import ImageColor
# using getrgb
img = ImageColor.getrgb("blue")
print(img)
img1 = ImageColor.getrgb("purple")
print(img1)
Output
(0, 0, 255)
(128, 0, 128)
Example 2
#Import required image modules
from PIL import Image,ImageColor
# Create new image & get color RGB tuple.
img = Image.new("RGB", (256, 256), ImageColor.getrgb("#add8e6"))
#Show image
img.show()
Image Color: getcolor() Method
This method is identical to getrgb(), but converts RGB values to grayscale if the mode is not a color supported by graphics commands for shape drawing and text annotations, or a paletted image. If the string cannot be parsed, this function raises a ValueError exception.
Syntax
PIL.ImageColor.getcolor(color, mode)
Where.
- Parameters – A color string
-
Return Value – (grayscale[, alpha]) or (red, green, blue[, alpha]).
Examples
#Import required image modules
from PIL import Image,ImageColor
# using getrgb
img = ImageColor.getrgb("skyblue")
print(img)
img1 = ImageColor.getrgb("purple")
print(img1)
Output
(135, 206, 235)
(128, 0, 128)