You can download this code by clicking the button below.
This code is now available for download.
This function generates a random RGB color and converts it to a hexadecimal format. It first generates three random numbers as RGB color values using the random library, then creates a 1x1 pixel RGB image and fills it with that color. Finally, it converts the RGB color to a hexadecimal format using the ImageColor module.
Technology Stack : Pillow, Python Standard Library
Code Type : Function
Code Difficulty : Intermediate
def random_color():
from random import randint
from PIL import Image, ImageColor
# Create a 1x1 image with random color
color = (randint(0, 255), randint(0, 255), randint(0, 255))
img = Image.new('RGB', (1, 1), color)
# Get the color in hex format
hex_color = ImageColor.getrgb(color)
return hex_color