”;
In Pillow (Python Imaging Library) colors on an image are defined as a combination of red (R), green (G) and blue (B) components which are commonly known as the RGB color model. This model represents colors as a mixture of these three primary colors with each component ranging from 0, i.e. minimum intensity to 255, i.e. maximum intensity. A fourth component, alpha (A) is often included for transparency with 0 representing full transparency and 255 representing full opacity.
Defining colors accurately is crucial when working with images as it determines the appearance and composition of the image. Pillow provides various ways to represent and define colors making it flexible for various image processing and manipulation tasks.
Here is a detailed explanation of these color components −
Red (R)
-
The red component represents the amount of red in a color.
-
When R is 0 which represents there is no red and resulting in a shade of cyan or green.
-
When R is 255 which specifies there is maximum red intensity and produces a pure red color.
-
Intermediate values produce varying shades of red from pink to orange.
Green (G)
-
The green component represents the amount of green in a color.
-
When G is 0 which represents there is no green and results in shades of magenta or blue.
-
When G is 255 which represents there is maximum green intensity and results in a pure green color.
-
Intermediate values produce different shades of green from lime to teal.
Blue (B)
-
The blue component represents the amount of blue in a color.
-
When B is 0 which represents there is no blue and results in shades of yellow or red.
-
When B is 255 which represents there is maximum blue intensity by producing a pure blue color.
-
Intermediate values create various shades of blue from navy to sky blue.
Alpha (A)
-
The alpha component is optional but essential for controlling transparency.
-
When A is 0 which represents the pixel is fully transparent allowing what”s behind it to show through.
-
When A is 255 which represents the pixel is fully opaque and it completely covers what”s underneath.
-
Intermediate alpha values create varying levels of transparency allowing a pixel to be partially see-through.
To represent a color in Pillow we typically use a tuple or list with four values in the order (R, G, B, A) to specify the color of a pixel. For example (255, 0, 0, 255) represents a fully opaque red pixel while (0, 255, 0, 128) represents a semi-transparent green pixel.
Example
Let”s see an example of creating an image using the Python Pillow with RGBA color representation.
from PIL import Image # Create an RGBA image with Semi-transparent green image = Image.new(''RGBA'', (700, 300), (0, 255, 0, 128)) # Display the resultant Semi-transparent green image image.show()
On executing the above program you will get output RGBA like below −
Understanding the RGB color model and the alpha component is fundamental when working with image processing and manipulation in Pillow as it allows us to create, modify and combine colors and images in various ways.
Hexadecimal Color Representation
Hexadecimal color codes are widely used on the web and in graphics design. In Pillow we can define a color using a hexadecimal string which represents the RGB values. The format is #RRGGBB where RR, GG and BB are two-digit hexadecimal values for red, green and blue respectively.
Example
Here is an example of creating an image using the Python Pillow Hexadecimal color representation. In this example we are creating red colored image with 80% opacity.
from PIL import Image # Create a red colored image with 80% opacity image = Image.new(''RGBA'', (700, 300), "#ff0000cc") # Display the resultant image image.show()
when you run the above program you will get following output −
Named Colors
Pillow ImageColor module provides a set of named colors (commonly used HTML color names), allowing us to use these color names instead of numerical values. For example, the name red indicates pure red color, and it is important to note that color names are case insensitive, meaning “red” and “Red” are treated the same.
Example
The following example demonstrates how to access and print the named colors available in Pillow”s ImageColor module.
from PIL import ImageColor # Access all the named colors color_map = ImageColor.colormap # Count the number of named colors num_colors = len(color_map) # Print the available named colors and count print(color_map) print(f''Total number of named colors: {num_colors}'')
when you run the above program you will get similar output like below −
{''aliceblue'': ''#f0f8ff'', ''antiquewhite'': ''#faebd7'', ''aqua'': ''#00ffff'', ''aquamarine'': ''#7fffd4'', ''azure'': ''#f0ffff'', ''beige'': ''#f5f5dc'', ''bisque'': ''#ffe4c4'', ''black'': ''#000000'', ''blanchedalmond'': ''#ffebcd'', ''blue'': ''#0000ff'', ''blueviolet'': ''#8a2be2'', ''brown'': ''#a52a2a'', ''burlywood'': ''#deb887'', ''cadetblue'': ''#5f9ea0'', ''chartreuse'': ''#7fff00'', ''chocolate'': ''#d2691e'', ''coral'': ''#ff7f50'', ''cornflowerblue'': ''#6495ed'', ''cornsilk'': ''#fff8dc'', ''crimson'': ''#dc143c'', ''cyan'': ''#00ffff'', ''darkblue'': ''#00008b'', ''darkcyan'': ''#008b8b'', ''darkgoldenrod'': ''#b8860b'', ''darkgray'': ''#a9a9a9'', ''darkgrey'': ''#a9a9a9'', ''darkgreen'': ''#006400'', ''darkkhaki'': ''#bdb76b'', ''darkmagenta'': ''#8b008b'', ''darkolivegreen'': ''#556b2f'', ''darkorange'': ''#ff8c00'', ''darkorchid'': ''#9932cc'', ''darkred'': ''#8b0000'', ''darksalmon'': ''#e9967a'', ''darkseagreen'': ''#8fbc8f'', ''darkslateblue'': ''#483d8b'', ''darkslategray'': ''#2f4f4f'', ''darkslategrey'': ''#2f4f4f'', ''darkturquoise'': ''#00ced1'', ''darkviolet'': ''#9400d3'', ''deeppink'': ''#ff1493'', ''deepskyblue'': ''#00bfff'', ''dimgray'': ''#696969'', ''dimgrey'': ''#696969'', ''dodgerblue'': ''#1e90ff'', ''firebrick'': ''#b22222'', ''floralwhite'': ''#fffaf0'', ''forestgreen'': ''#228b22'', ''fuchsia'': ''#ff00ff'', ''gainsboro'': ''#dcdcdc'', ''ghostwhite'': ''#f8f8ff'', ''gold'': ''#ffd700'', ''goldenrod'': ''#daa520'', ''gray'': ''#808080'', ''grey'': ''#808080'', ''green'': ''#008000'', ''greenyellow'': ''#adff2f'', ''honeydew'': ''#f0fff0'', ''hotpink'': ''#ff69b4'', ''indianred'': ''#cd5c5c'', ''indigo'': ''#4b0082'', ''ivory'': ''#fffff0'', ''khaki'': ''#f0e68c'', ''lavender'': ''#e6e6fa'', ''lavenderblush'': ''#fff0f5'', ''lawngreen'': ''#7cfc00'', ''lemonchiffon'': ''#fffacd'', ''lightblue'': ''#add8e6'', ''lightcoral'': ''#f08080'', ''lightcyan'': ''#e0ffff'', ''lightgoldenrodyellow'': ''#fafad2'', ''lightgreen'': ''#90ee90'', ''lightgray'': ''#d3d3d3'', ''lightgrey'': ''#d3d3d3'', ''lightpink'': ''#ffb6c1'', ''lightsalmon'': ''#ffa07a'', ''lightseagreen'': ''#20b2aa'', ''lightskyblue'': ''#87cefa'', ''lightslategray'': ''#778899'', ''lightslategrey'': ''#778899'', ''lightsteelblue'': ''#b0c4de'', ''lightyellow'': ''#ffffe0'', ''lime'': ''#00ff00'', ''limegreen'': ''#32cd32'', ''linen'': ''#faf0e6'', ''magenta'': ''#ff00ff'', ''maroon'': ''#800000'', ''mediumaquamarine'': ''#66cdaa'', ''mediumblue'': ''#0000cd'', ''mediumorchid'': ''#ba55d3'', ''mediumpurple'': ''#9370db'', ''mediumseagreen'': ''#3cb371'', ''mediumslateblue'': ''#7b68ee'', ''mediumspringgreen'': ''#00fa9a'', ''mediumturquoise'': ''#48d1cc'', ''mediumvioletred'': ''#c71585'', ''midnightblue'': ''#191970'', ''mintcream'': ''#f5fffa'', ''mistyrose'': ''#ffe4e1'', ''moccasin'': ''#ffe4b5'', ''navajowhite'': ''#ffdead'', ''navy'': ''#000080'', ''oldlace'': ''#fdf5e6'', ''olive'': ''#808000'', ''olivedrab'': ''#6b8e23'', ''orange'': ''#ffa500'', ''orangered'': ''#ff4500'', ''orchid'': ''#da70d6'', ''palegoldenrod'': ''#eee8aa'', ''palegreen'': ''#98fb98'', ''paleturquoise'': ''#afeeee'', ''palevioletred'': ''#db7093'', ''papayawhip'': ''#ffefd5'', ''peachpuff'': ''#ffdab9'', ''peru'': ''#cd853f'', ''pink'': ''#ffc0cb'', ''plum'': ''#dda0dd'', ''powderblue'': ''#b0e0e6'', ''purple'': ''#800080'', ''rebeccapurple'': ''#663399'', ''red'': ''#ff0000'', ''rosybrown'': ''#bc8f8f'', ''royalblue'': ''#4169e1'', ''saddlebrown'': ''#8b4513'', ''salmon'': ''#fa8072'', ''sandybrown'': ''#f4a460'', ''seagreen'': ''#2e8b57'', ''seashell'': ''#fff5ee'', ''sienna'': ''#a0522d'', ''silver'': ''#c0c0c0'', ''skyblue'': ''#87ceeb'', ''slateblue'': ''#6a5acd'', ''slategray'': ''#708090'', ''slategrey'': ''#708090'', ''snow'': ''#fffafa'', ''springgreen'': ''#00ff7f'', ''steelblue'': ''#4682b4'', ''tan'': ''#d2b48c'', ''teal'': ''#008080'', ''thistle'': ''#d8bfd8'', ''tomato'': ''#ff6347'', ''turquoise'': ''#40e0d0'', ''violet'': ''#ee82ee'', ''wheat'': ''#f5deb3'', ''white'': ''#ffffff'', ''whitesmoke'': ''#f5f5f5'', ''yellow'': ''#ffff00'', ''yellowgreen'': ''#9acd32''} Total number of named colors: 148
Grayscale Colors
Grayscale colors are represented by a single intensity value ranging from 0 (black) to 255 (white). We can define grayscale colors using a single integer value.
Example
Here is an example of creating a new grayscale image with grayscale color representation.
from PIL import Image, ImageDraw, ImageFont # Create a greyscale image with color intensity value image = Image.new(''L'', (700, 300), 100) #Create a drawing object draw = ImageDraw.Draw(image) #Define text attributes text = "Welcome to Tutorialspoint..." font = ImageFont.truetype("arial.ttf", size=35) text_position = (150, 130) # Specify the text color using the single integer value text_color = 255 #Add text to the image draw.text(text_position, text, fill=text_color, font=font) image.show()
Following is the output of the above program −
”;