Basic Image Operations

Python Pillow Color Conversions

Image Manipulation

Image Filtering

Image Enhancement and Correction

Image Analysis

Advanced Topics

  • Image Module
  • Python Pillow Useful Resources

    Python Pillow - Colors on an Image



    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 - Creating a image

    Let's see an example of creating an image using the Python Pillow with RGBA color representation.

    main.py

    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()
    

    Output

    On executing the above program you will get output RGBA like below −

    colors_on_an_image_ex1

    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.

    Example - 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.

    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.

    main.py

    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()
    

    Output

    when you run the above program you will get following output −

    colors_on_an_image_ex2

    Example - 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.

    The following example demonstrates how to access and print the named colors available in Pillow's ImageColor module.

    main.py

    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}')
    

    Output

    when you run the above program you will get similar output like below −

    {'aliceblue': '#f0f8ff', 'antiquewhite': '#faebd7', 'aqua': '#00ffff', ...}
    Total number of named colors: 148
    

    Example - 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.

    Here is an example of creating a new grayscale image with grayscale color representation.

    main.py

    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()
    

    Output

    Following is the output of the above program −

    colors_on_an_image_ex3
    Advertisements