Pillow Imagecolor Module
ImageColor is a submodule in the Python Pillow image processing library, specifically designed to handle color-related operations.
ImageColor provides a series of methods to convert color strings into RGB or RGBA format tuples, supporting multiple color representation formats.
Although the ImageColor module is compact, it plays an important role in image processing, especially in scenarios where colors need to be dynamically specified.
The ImageColor module can be imported 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 | Function Description |
| --- | --- | --- | --- |
| `getrgb(color)` | `color`: color string | RGB tuple (r, g, b) | Converts color string to RGB tuple |
| `getcolor(color, mode)` | `color`: color string `mode`: target color mode | color value tuple | Converts color string to specified color mode value |
| `colormap` | none | dictionary | Returns the color map supported by Pillow |
* * *
## Method Detailed Description and Examples
### getrgb() Method
The `getrgb()` method is the most commonly used method, which converts color strings in various formats to standard RGB tuples.
**Supported formats include**:
* Hexadecimal color codes: `"#RRGGBB"` or `"#RGB"`
* RGB function representation: `"rgb(255, 0, 0)"`
* HSL function representation: `"hsl(0, 100%, 50%)"`
* Color names: `"red"`, `"blue"`, etc.
**Example code**:
## Instance
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
The `getcolor()` method is similar to `getrgb()`, but allows specifying the target color mode.
**Supported modes include**:
* `"RGB"`
* `"RGBA"`
* `"L"` (grayscale)
* `"CMYK"`
* `"HSV"`
**Example code**:
## Instance
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 (value of red in grayscale)
### colormap Attribute
`colormap` is a dictionary that contains all named colors supported by Pillow and their corresponding hexadecimal values.
**Example code**:
## Instance
from PIL import ImageColor
# get color map
color_map = ImageColor.colormap
# view number of colors
print(f"Pillow supports {len(color_map)} named colors")
# view specific color
print("hexadecimal value of red:", color_map)# output: #ff0000
* * *
## Practical Application Examples
### Creating Monochrome Images
## Instance
from PIL import Image, ImageColor
# use ImageColor to get color
color = ImageColor.getrgb("lightblue")
# create 200x200 monochrome image
img = Image.new("RGB",(200,200), color)
img.show()
### Dynamically Setting Drawing Colors
## Instance
from PIL import Image, ImageDraw, ImageColor
# create blank image
img = Image.new("RGB",(400,200),"white")
draw = ImageDraw.Draw(img)
# draw graphics with 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 + i*80,50,100 + i*80,150], fill=color)
img.show()
* * *
## Notes
1. **Color string format**: Ensure the color string format is correct, otherwise a `ValueError` will be raised
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 pre-convert and store commonly used colors
4. **Color name limitations**: Pillow supports a limited number of color names, the complete list can be viewed through `ImageColor.colormap`
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.
YouTip