Python Pillow - Colors on an Image (2024)

The ImageColor module contains colors in different format arranged in tables and it also contains converters from CSS3-style color specifiers to RGB tuples.

Color Names

The ImageColor module supports the following strings formats −

ImageColor.getrgb() Method

Convert a color string to an RGB tuple. If the string cannot be parsed, a ValueError exception is raised by this function.

Syntax

PIL.ImageColor.getrgb(color)

Where,

  • Arguments: color – A color string

  • Return Value: (red, green, blue[, alpha])

Example 1

from PIL import ImageColor# using getrgbimg = ImageColor.getrgb("blue")print(img)img1 = ImageColor.getrgb("purple")print(img1)

Output

(0, 0, 255)(128, 0, 128)

Example 2

#Import required image modulesfrom PIL import Image,ImageColor# Create new image & get color RGB tuple.img = Image.new("RGB", (256, 256), ImageColor.getrgb("#add8e6"))#Show imageimg.show()

Output

Python Pillow - Colors on an Image (1)

ImageColor. getcolor() Method

This method is same as getrgb(), however, converts the RGB value to a greyscale value, if the mode isn’t The graphics commands support shape drawing and text annotation color or a palette image. If the string cannot be parsed, this function raises a ValueError exception.

Syntax

PIL.ImageColor.getcolor(color, mode)

Where,

  • Arguments - A color string

  • Return Value - (graylevel[, alpha]) or (red, green, blue[, alpha])

Example

#Import required image modulesfrom PIL import Image,ImageColor# using getrgbimg = ImageColor.getrgb("skyblue")print(img)img1 = ImageColor.getrgb("purple")print(img1)

Output

(135, 206, 235)(128, 0, 128)

Advertisem*nts

Python Pillow - Colors on an Image (2024)
Top Articles
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 6329

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.