import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
import random
def create_random_image():
width, height = 400, 400
image = Image.new("RGB", (width, height))
pixels = image.load()
for x in range(width):
for y in range(height):
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
pixels[x, y] = (r, g, b)
return ImageTk.PhotoImage(image)
def update_image():
red = red_slider.get()
green = green_slider.get()
blue = blue_slider.get()
new_image = Image.new("RGB", (400, 400), (red, green, blue))
canvas.image = ImageTk.PhotoImage(new_image)
canvas.create_image(0, 0, anchor=tk.NW, image=canvas.image)
root = tk.Tk()
root.title("Colorful Image Generator")
canvas = tk.Canvas(root, width=400, height=400)
canvas.pack()
red_slider = ttk.Scale(root, from_=0, to=255, orient=tk.HORIZONTAL, label="Red")
green_slider = ttk.Scale(root, from_=0, to=255, orient=tk.HORIZONTAL, label="Green")
blue_slider = ttk.Scale(root, from_=0, to=255, orient=tk.HORIZONTAL, label="Blue")
update_button = ttk.Button(root, text="Update Image", command=update_image)
canvas.image = create_random_image()
canvas.create_image(0, 0, anchor=tk.NW, image=canvas.image)
red_slider.pack()
green_slider.pack()
blue_slider.pack()
update_button.pack()
red_slider.set(128)
green_slider.set(128)
blue_slider.set(128)