You can download this code by clicking the button below.
This code is now available for download.
This function generates a random color and returns the RGB value of the color. It first creates a white 100x100 pixel image, then randomly selects RGB values, and uses these values to fill a circle with the color, finally converting the color to a hexadecimal string.
Technology Stack : Pillow (Image, ImageDraw, ImageColor)
Code Type : Python Function
Code Difficulty : Intermediate
def random_color():
from random import randint
from PIL import Image, ImageColor
# Create a new white image
img = Image.new('RGB', (100, 100), 'white')
# Generate a random color
random_color = (randint(0, 255), randint(0, 255), randint(0, 255))
# Draw a circle with the random color
draw = ImageDraw.Draw(img)
draw.ellipse((50, 50, 150, 150), fill=random_color)
# Convert the image to a hex string
hex_color = ImageColor.getrgb(random_color)
return hex_color