How to Convert HEX to RBG in Python


If you need to convert HEX (Hexadecimal) to RGB (Red-Green-Blue) in your Python code, then you can do the following:

Option 1 – Using the PIL library

from PIL import ImageColor
hex = input('Enter HEX value: ')
ImageColor.getcolor(hex, "RGB")

Option 2 – Using a custom solution

hex = input('Enter HEX value: ').lstrip('#')
print('RGB value =', tuple(int(hex[i:i+2], 16) for i in (0, 2, 4)))