Hex to RGB Conversion in Python

Hex to rgb – Conversion of RGB to hex and vice-versa in Python like RGB to Hex, Hex to RGB Example.

Convert HEX to RGB in Python

 

Converting Hex to RGB value in Python here we can learn to simple step by step Convert hex color to rgb Example. Best Practice to Python Convert Hex Color to RGB – Python Tutorial

value: [#]hex, such as #ff6600 or ff6600

return: [r, g, b]

Python Image Library PIL in Python

Example
Convert a Hexadecimal Value to an RGB Value With the Python Image Library PIL in Python
 

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

 

Result
 

Please Enter HEX value: #B12345
Genrate RGB value = (177, 35, 69)

 

Self-Defined Method in Python

Convert a Hexadecimal Value to an RGB Value With the Self-Defined Method in Python
Example
 

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

 

Result
 

Please Enter HEX value: #B12345
Genrate RGB value = (177, 35, 69)

 

Also Read : Black transparent color code

Conversion of RGB to hex and vice-versa in Python

Converting RGB to hex
 

def rgb_to_hex(r, g, b):
return ('{:X}{:X}{:X}').format(r, g, b)

print(rgb_to_hex(255, 165, 1))

 

Converting hex to RGB
 

def hex_to_rgb(hex):
rgb = []
for i in (0, 2, 4):
decimal = int(hex[i:i+2], 16)
rgb.append(decimal)

return tuple(rgb)

print(hex_to_rgb('ff0066'))

 

convert hex color to rgb python
 

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

 

hex to rgb python
 

from colormap import rgb2hex
from colormap import hex2rgb

print(rgb2hex(255, 255, 255))
print(hex2rgb('#FFFFFF'))

>>> #FFFFFF
>>> (255, 255, 255)

 

How to use?

 

hex_color = '#FFA501'
print(hex_to_rgb(hex_color))
hex_color = 'FFA501'
print(hex_to_rgb(hex_color))

 

I hope you get an idea about hex to rgb.

 


#py  #python 

Hex to RGB Conversion in Python
1.00 GEEK