Converting Hex to RGB value in Python Converting Hex to RGB value in Python python python

Converting Hex to RGB value in Python


I believe that this does what you are looking for:

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

(The above was written for Python 3)

Sample run:

Enter hex: #B4FBB8RGB = (180, 251, 184)

Writing to a file

To write to a file with handle fhandle while preserving the formatting:

fhandle.write('RGB = {}'.format( tuple(int(h[i:i+2], 16) for i in (0, 2, 4)) ))


You can use ImageColor from Pillow.

>>> from PIL import ImageColor>>> ImageColor.getcolor("#23a9dd", "RGB")(35, 169, 221)


A lazy option:webcolors package has a hex_to_rgb function.