Python PIL ImageColor.getrgb() method

Python PIL ImageColor.getrgb() Method

PIL is the Python Imaging Library, which provides image editing capabilities for the Python interpreter. The ImageColor module contains color tables and a converter from CSS3-style color specifiers to RGB primitives. This module is used by PIL.Image.Image.new() and the ImageDraw module, among others.

ImageColor.getrgb() Converts a color string to an RGB tuple. If the string cannot be parsed, this function raises a ValueError exception.

Syntax: PIL.ImageColor.getrgb(color)

Parameters:
color – A color string

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

# importing Image module from PIL package 
from PIL import Image, ImageColor
  
# using getrgb
im = ImageColor.getrgb("orange")
print(im)
  
im1 = ImageColor.getrgb("red")
print(im1)

Output:

(255, 165, 0)
(255, 0, 0)

Another example: – Here we use different colors.

# importing the Image module from the PIL package 
from PIL import Image, ImageColor
  
# using getrgb
im = ImageColor.getrgb("blue")
print(im)
  
im1 = ImageColor.getrgb("yellow")
print(im1)

Output:

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

Leave a Reply

Your email address will not be published. Required fields are marked *