You can download this code by clicking the button below.
This code is now available for download.
This function loads an image from a specified path, applies a blur filter to the image, converts it to grayscale, then gets the average color of the grayscale image. Finally, it creates a new image with the average color as its background and saves the processed image to the specified path.
Technology Stack : Pillow
Code Type : The type of code
Code Difficulty : Intermediate
def random_image_color(arg1, arg2):
from PIL import Image, ImageFilter, ImageOps
# Load an image from a file
image = Image.open(arg1)
# Apply a filter to the image
filtered_image = image.filter(ImageFilter.BLUR)
# Convert the image to grayscale
grayscale_image = ImageOps.grayscale(filtered_image)
# Get the average color of the image
average_color = grayscale_image.convert("L").getpixel((0, 0))
# Save the image with the average color as its background
new_image = Image.new("RGB", (grayscale_image.width, grayscale_image.height), color=(average_color, average_color, average_color))
new_image.paste(grayscale_image, (0, 0))
# Save the resulting image
new_image.save(arg2)