Skip to content

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")
Code language: Python (python)

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 (, 2, 4)))
Code language: Python (python)

See also  The Power of Two (2) Table
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x