DEV Community

Pandeyashish17
Pandeyashish17

Posted on

4

Generate image from text with random gradient background everytime

import random
from PIL import Image, ImageDraw, ImageFont

# Create a new image with a gradient background
width, height = 200, 50
start_color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
end_color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
img = Image.new('RGB', (width, height), color = start_color)
draw = ImageDraw.Draw(img)

# Add a gradient to the background
for x in range(width):
    for y in range(height):
        color = tuple(int(start_color[i] + (end_color[i] - start_color[i]) * x / width) for i in range(3))
        draw.point((x, y), fill=color)

# Use the Audiowide font
font = ImageFont.truetype("Audiowide-Regular.ttf", size=24)

# Draw the text on the image
draw.text((10, 10), "Hello, World!", fill=(0, 0, 0), font=font)

# Save the image
img.save('text_image.png')

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

ACI image

ACI.dev: Fully Open-source AI Agent Tool-Use Infra (Composio Alternative)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Check out our GitHub!

Join the Runner H "AI Agent Prompting" Challenge: $10,000 in Prizes for 20 Winners!

Runner H is the AI agent you can delegate all your boring and repetitive tasks to - an autonomous agent that can use any tools you give it and complete full tasks from a single prompt.

Check out the challenge

DEV is bringing live events to the community. Dismiss if you're not interested. ❤️