Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pillow ImageColor Module

Pillow ImageColor Module

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

ImageColor provides a series of methods to convert color strings to 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 specified dynamically.

You can import the ImageColor module in the following way:



from PIL import ImageColor


Core Methods of the ImageColor Module

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 Dictionaries Return the color mapping table supported by Pillow

Detailed Method Descriptions and Examples

The getrgb() Method

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

Supported formats include:

Example Code:

Example

from

PIL

import

ImageColor

Hexadecimal colors

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)

The getcolor() Method

getcolor() The method and the getrgb() Similar, but allows specifying the target color mode.

Supported modes include:

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)

The colormap Attribute

colormap is a dictionary containing all 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 Monochrome Image

Example

from

PIL

import

Image

,

ImageColor

Get the color using ImageColor

color

=

ImageColor.

getrgb

(

"lightblue"

)

Create a 200x200 monochrome 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 using different colors

colors

=

[

"red"

,

"#00FF00"

,

"rgb(0, 0, 255)"

,

"hsl(30, 100%, 50%)"

]

for

i

,

color_str

in

enumerate

(

colors

)

:

    color

=

ImageColor.

getrgb

(

color_str

)

    draw.

rectangle

(

[

50

80

,

50

,

100

80

,

150

]

,

fill

=

color

)

img.

show

(

)


Notes

  1. Color String Format: Ensure the color string format is correct, otherwise it will raise ValueError
  2. Color Mode Support: Not all color modes support all color conversions
  3. Performance considerations: Frequently calling color conversion methods may affect performance, it is recommended to pre-convert and store commonly used colors
  4. Color Name Limitations: Pillow supports a limited number of color names, the complete list can be found via ImageColor.colormap Check

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