Pillow ImageColor Module

ImageColor is a submodule of the Python Pillow image processing library, specifically used for handling color-related operations.

ImageColor provides a series of methods to convert color strings into tuples in RGB or RGBA format, supporting multiple color representation formats.

Although the ImageColor module is small, it plays an important role in image processing, especially in scenarios where colors need to be dynamically specified.

You can import the ImageColor module in the following way:

from PIL import ImageColor

ImageColor Module Core Methods

The following table lists the main methods of the ImageColor module and their functional descriptions:

Method Name Parameters Return Value Description
getrgb(color) color: Color string RGB tuple (r, g, b) Convert a color string to an RGB tuple
getcolor(color, mode) color: Color string
mode: Target color mode
Color value tuple Convert a color string to a value in the specified color mode
colormap None Dictionary Return the color mapping table supported by Pillow

Detailed Method Descriptions and Examples

getrgb() Method

getrgb()This method is the most commonly used method, converting color strings in various formats to standard RGB tuples.

Supported formats include::

  • Hexadecimal color code:"#RRGGBB"or"#RGB"
  • RGB function representation:"rgb(255, 0, 0)"
  • HSL function representation:"hsl(0, 100%, 50%)"
  • Color name:"red", "blue"wait

Example code:

Example

from PIL import ImageColor

# Hexadecimal color
print(ImageColor.getrgb("#ff0000"))  # Output: (255, 0, 0)
print(ImageColor.getrgb("#f00"))    # Output: (255, 0, 0)

# RGB function
print(ImageColor.getrgb("rgb(255, 0, 0)"))  # Output: (255, 0, 0)

# Color name
print(ImageColor.getrgb("red"))    # Output: (255, 0, 0)
print(ImageColor.getrgb("blue"))   # Output: (0, 0, 255)

getcolor() Method

getcolor()The method is similar togetrgb()the getrgb() method, but you can specify the target color mode.

Supported modes include::

  • "RGB"
  • "RGBA"
  • "L"(grayscale)
  • "CMYK"
  • "HSV"

Example code:

Example

from PIL import ImageColor

# Convert to RGBA
print(ImageColor.getcolor("red", "RGBA"))  # Output: (255, 0, 0, 255)

# Convert to grayscale
print(ImageColor.getcolor("red", "L"))    # Output: 76 (the value of red in grayscale)

colormap Property

colormapIt is a dictionary that contains all the named colors supported by Pillow and their corresponding hexadecimal values.

Example code:

Example

from PIL import ImageColor

# Get the color mapping table
color_map = ImageColor.colormap

# View the number of colors
print(f"Pillow supports {len(color_map)} named colors")

# View a specific color
print("Hexadecimal value of red:", color_map["red"])  # Output: #ff0000

Practical Application Examples

Create a Solid Color Image

Example

from PIL import Image, ImageColor

# Use ImageColor to get the color
color = ImageColor.getrgb("lightblue")

# Create a 200x200 solid color image
img = Image.new("RGB", (200, 200), color)
img.show()

Dynamically Set Drawing Colors

Example

from PIL import Image, ImageDraw, ImageColor

# Create a blank image
img = Image.new("RGB", (400, 200), "white")
draw = ImageDraw.Draw(img)

# Draw shapes with different colors
colors = ["red", "#0080FF", "rgb(0, 0, 255)", "hsl(30, 100%, 50%)"]

for i, color_str in enumerate(colors):
    color = ImageColor.getrgb(color_str)
    draw.rectangle([50 + i*80, 50, 100 + i*80, 150], fill=color)

img.show()

Notes

  1. Color string format: Ensure the color string format is correct, otherwise it will raise an exception.ValueError
  2. Color mode support: Not all color modes support all color conversions
  3. Performance considerations: Frequent calls to color conversion methods may affect performance; it is recommended to convert and store commonly used colors in advance.
  4. Color name limitations: The color names supported by Pillow are limited; the full list can be viewed viaImageColor.colormapthe colormap property.

By mastering the ImageColor module, you can more flexibly use various color representation methods in Pillow image processing, adding more possibilities to your image processing programs.

Other Extensions