Skip to content
Skabir2023Public

About

Text overlay on image api

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Image Generator API

A Docker-based API service for generating styled text overlays on images. This service provides two templates for creating professional-looking images with customizable text, colors, and styling options.

Features

  • Template 1: Bullet-point style with numbered circles and rectangular backgrounds
  • Template 2: Clean title-focused design with customizable fonts
  • Text Stroke Support: Add outlines/strokes to text with customizable width and color
  • API-driven: Complete REST API with JSON input/output
  • Docker support: Easy deployment with Docker and docker-compose
  • Customizable settings: Override default styling through API parameters
  • Image URL input: Accepts background images via URL
  • Base64 output: Returns generated images as base64-encoded strings

Quick Start

Using Docker Compose (Recommended)

  1. Clone or download this repository
  2. Navigate to the project directory
  3. Build and run the service:
docker-compose up --build

The API will be available at http://localhost:8090

Using Docker

# Build the image
docker build -t image-generator-api .

# Run the container
docker run -p 8090:8090 image-generator-api

Local Development

# Install dependencies
pip install -r requirements.txt

# Setup fonts (optional)
python setup_fonts.py

# Run the application
uvicorn app:app --host 0.0.0.0 --port 8090 --reload

API Documentation

Endpoints

GET /

Returns basic API information.

GET /health

Health check endpoint.

POST /generate-image

Generate an image with text overlay.

Request Format

{
  "image_url": "https://example.com/background-image.jpg",
  "title": "Your Title Text",
  "bullet_points": ["Point 1", "Point 2", "Point 3"],
  "template": "template_1",
  "width": 1000,
  "height": 1500,
  "template_1_settings": {
    "title_font_size": 75,
    "title_text_color_default": "white",
    "title_stroke_width": 2,
    "title_stroke_color": "black",
    "tips_count": 4,
    "footer_text": "your-website.com"
  }
}

Request Parameters

Parameter Type Required Description
image_url string Yes URL of the background image
title string Yes Title text to overlay
bullet_points array No List of bullet points (Template 1 only)
template string Yes Template to use: "template_1" or "template_2"
width integer No Image width (default: 1000)
height integer No Image height (default: 1500)
template_1_settings object No Template 1 specific settings
template_2_settings object No Template 2 specific settings

Response Format

{
  "success": true,
  "image_base64": "iVBORw0KGgoAAAANSUhEUgAA...",
  "error": null
}

Text Stroke Features

Both templates now support text stroke (outline) functionality for enhanced text visibility and styling:

Stroke Settings Available:

  • Title Text Stroke: title_stroke_width and title_stroke_color
  • Tips Text Stroke: tips_stroke_width and tips_stroke_color (Template 1)
  • Circle Number Stroke: circle_text_stroke_width and circle_text_stroke_color (Template 1)
  • Another Text Stroke: another_text_stroke_width and another_text_stroke_color (Template 2)
  • Footer Text Stroke: footer_text_stroke_width and footer_text_stroke_color

Stroke Usage Examples:

{
  "template_1_settings": {
    "title_text_color_default": "white",
    "title_stroke_width": 2,
    "title_stroke_color": "black",
    "tips_text_color": "#2c3e50",
    "tips_stroke_width": 1,
    "tips_stroke_color": "white"
  }
}

Stroke Width: Set to 0 to disable stroke, or 1-5 for different stroke thickness Stroke Color: Any valid color (hex, RGB, or color names)

Template Settings

Template 1 Settings

Template 1 is designed for bullet-point style content with numbered circles.

{
  "template_1_settings": {
    "bg_color": "#8194b0",
    "random_bg_color": true,
    "random_colors": ["#040038", "#8142f6", "#4395b1"],
    "overlay_bg": false,
    "resize_bg": false,
    "title_font_size": 75,
    "title_max_width": 900,
    "title_text_color_default": "white",
    "title_text_color_dark": "#323232",
    "title_stroke_width": 0,
    "title_stroke_color": "black",
    "title_line_spacing": 15,
    "title_margin_from_top": 50,
    "gradient": false,
    "gradient_direction": 360,
    "tips_count": 4,
    "tips_font_size": 36,
    "tips_top_margin": 120,
    "tips_max_text_width": 700,
    "tips_left_margin": 150,
    "rectangle_top_padding": 20,
    "rectangle_bottom_padding": 20,
    "rectangle_horizontal_padding": 30,
    "rectangle_fill_color": "white",
    "rectangle_opacity": 255,
    "rectangle_corner_radius": 20,
    "rectangle_outline": "grey",
    "rectangle_outline_width": 0,
    "tips_text_color": "#323232",
    "tips_stroke_width": 0,
    "tips_stroke_color": "white",
    "tips_line_spacing": 10,
    "margin_between_tips": 110,
    "tips_block_x_offset": 20,
    "circle_radius": 40,
    "circle_x_offset": -55,
    "circle_y_offset": 0,
    "circle_fill_color": "#b7abe9",
    "circle_opacity": 255,
    "circle_outline": "grey",
    "circle_outline_width": 0,
    "tips_number_font_size": 40,
    "circle_text_color": "white",
    "circle_text_stroke_width": 0,
    "circle_text_stroke_color": "black",
    "circle_text_x_offset": 0,
    "circle_text_y_offset": 0,
    "footer": true,
    "footer_text": "website.com",
    "footer_font_size": 40,
    "footer_height": 100,
    "footer_fill_color": "#475b75",
    "footer_opacity": 255,
    "footer_text_color": "white",
    "footer_text_stroke_width": 0,
    "footer_text_stroke_color": "black",
    "footer_text_y_offset": 0
  }
}

Template 2 Settings

Template 2 is designed for clean, title-focused designs.

{
  "template_2_settings": {
    "bg_color": "#7c95b3",
    "random_bg_color": true,
    "random_colors": ["#7ff3b6", "#e684a5", "#b5abe4", "#26364f"],
    "overlay_bg": true,
    "resize_bg": false,
    "gradient": false,
    "gradient_direction": 360,
    "title_font_size": 90,
    "title_text_color": "#323232",
    "title_stroke_width": 0,
    "title_stroke_color": "white",
    "title_line_spacing": 20,
    "title_max_width": 550,
    "title_y_offset": -100,
    "another_font": true,
    "strings_with_another_font": 2,
    "another_text_top_padding": 40,
    "another_text_font_size": 100,
    "another_text_color": "#876ac7",
    "another_text_stroke_width": 0,
    "another_text_stroke_color": "white",
    "another_text_line_spacing": 0,
    "rectangle_top_padding": 50,
    "rectangle_bottom_padding": 60,
    "rectangle_horizontal_padding": 50,
    "rectangle_fill_color": "white",
    "rectangle_opacity": 255,
    "rectangle_outline": "black",
    "rectangle_outline_width": 5,
    "rectangle_corner_radius": 0,
    "footer": true,
    "footer_text": "website.com",
    "footer_font_size": 40,
    "footer_height": 100,
    "footer_fill_color": "#b7abe9",
    "footer_opacity": 255,
    "footer_text_color": "white",
    "footer_text_stroke_width": 0,
    "footer_text_stroke_color": "black",
    "footer_text_y_offset": 0
  }
}

Example Usage

cURL Example

curl -X POST "http://localhost:8090/generate-image" \
  -H "Content-Type: application/json" \
  -d '{
    "image_url": "https://example.com/background.jpg",
    "title": "5 Tips for Better Health",
    "bullet_points": [
      "Drink more water daily",
      "Exercise for 30 minutes",
      "Get 8 hours of sleep",
      "Eat more vegetables"
    ],
    "template": "template_1",
    "template_1_settings": {
      "footer_text": "healthtips.com",
      "title_stroke_width": 2,
      "title_stroke_color": "black",
      "tips_stroke_width": 1,
      "tips_stroke_color": "white"
    }
  }'

Python Example

import requests
import base64
from PIL import Image
import io

# API request
response = requests.post('http://localhost:8090/generate-image', json={
    "image_url": "https://example.com/background.jpg",
    "title": "Amazing Recipe Ideas",
    "template": "template_2",
    "template_2_settings": {
        "footer_text": "recipes.com",
        "title_text_color": "#2c3e50"
    }
})

# Process response
if response.json()['success']:
    # Decode base64 image
    image_data = base64.b64decode(response.json()['image_base64'])
    image = Image.open(io.BytesIO(image_data))
    
    # Save or display the image
    image.save('generated_image.png')
    image.show()

n8n Integration

For n8n workflows, use the HTTP Request node:

  1. Method: POST
  2. URL: http://your-server:8090/generate-image
  3. Body: JSON with your parameters
  4. Headers: Content-Type: application/json

The response will contain the generated image as base64, which you can then process or save as needed.

Font Setup

The application includes a font setup script that attempts to use system fonts. If no system fonts are found, it will fall back to PIL's default fonts.

To manually setup fonts:

python setup_fonts.py

You can also manually place TTF font files in:

  • data/image_assets/template_1/fonts/
  • data/image_assets/template_2/fonts/

Required font files:

  • Template 1: title_font.ttf, tips_font.ttf, footer_font.ttf
  • Template 2: title_font.ttf, title_2_font.ttf, footer_font.ttf

Environment Variables

Variable Default Description
PORT 8090 Port to run the API on
HOST 0.0.0.0 Host to bind the API to

Troubleshooting

Common Issues

  1. Font not found errors: Run python setup_fonts.py or manually add font files
  2. Image download fails: Ensure the image URL is accessible and returns a valid image
  3. Memory issues: Reduce image dimensions or use smaller background images

Logs

Check Docker logs for debugging:

docker-compose logs -f image-generator-api

Development

Project Structure

├── app.py                          # FastAPI application
├── modules/
│   ├── api_image_generator.py      # API-specific image generators
│   └── settings.py                 # Template settings
├── data/
│   └── image_assets/               # Font and asset storage
├── Dockerfile                      # Docker configuration
├── docker-compose.yml             # Docker Compose configuration
├── requirements.txt               # Python dependencies
└── setup_fonts.py                # Font setup utility

Adding New Templates

  1. Create new settings class in modules/settings.py
  2. Create new generator class in modules/api_image_generator.py
  3. Add template option to the API request model
  4. Update the generate_image endpoint logic

License

This project is provided as-is for educational and commercial use.

About

Text overlay on image api

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages