from PIL import Image, ImageDraw, ImageFont import random def generate_freeform_image(user_input, width=800, height=600, output_path="generated_image.png"): """ Generates a freeform abstract image based on user input. Parameters: - user_input (str): A theme, mood, or keyword to inspire the design. - width (int): Width of the image. - height (int): Height of the image. - output_path (str): File path to save the image. """ # Create a blank canvas image = Image.new("RGB", (width, height), color="white") draw = ImageDraw.Draw(image) # Define color palette based on mood mood_colors = { "happy": ["#FFD700", "#FF69B4", "#ADFF2F"], "calm": ["#ADD8E6", "#87CEFA", "#B0E0E6"], "dark": ["#2F4F4F", "#000000", "#4B0082"], "energetic": ["#FF4500", "#FF6347", "#FFFF00"], "mystical": ["#8A2BE2", "#4B0082", "#00CED1"] } # Pick colors based on input selected_colors = mood_colors.get(user_input.lower(), ["#CCCCCC", "#999999", "#666666"]) # Draw random shapes for _ in range(50): shape_type = random.choice(["ellipse", "rectangle", "line"]) x1, y1 = random.randint(0, width), random.randint(0, height) x2, y2 = random.randint(0, width), random.randint(0, height) color = random.choice(selected_colors) if shape_type == "ellipse": draw.ellipse([x1, y1, x2, y2], fill=color, outline=None) elif shape_type == "rectangle": draw.rectangle([x1, y1, x2, y2], fill=color, outline=None) elif shape_type == "line": draw.line([x1, y1, x2, y2], fill=color, width=random.randint(1, 5)) # Optionally add text try: font = ImageFont.truetype("arial.ttf", 24) draw.text((10, 10), f"Inspired by: {user_input}", fill="black", font=font) except: draw.text((10, 10), f"Inspired by: {user_input}", fill="black") # Save the image image.save(output_path) print(f"Image saved to {output_path}")