Tech With Galvan
Back to Articles
AutomationMay 6, 2029 • 5 min read

How to Automatically Create Social Images From Templates

Automate social media image creation from templates — Python, Canva API, and Pillow. Generate branded graphics in bulk.

Galvan
Galvan

Founder & Creator

Introduction

Creating social media graphics manually takes 15-30 minutes per post. For a business posting 3-5 times per week across 3 platforms, that’s 5-10 hours of design work. Automated image generation reduces this to seconds: define a template, feed it data, and generate hundreds of branded graphics.

This post shows you how to automate social image creation with Python.

Approach 1: Pillow (free, local)

Python’s Pillow library can generate images from templates:

from PIL import Image, ImageDraw, ImageFont
import os

def create_social_image(title, subtitle, output_path):
    # Create canvas (1080x1080 for Instagram)
    img = Image.new("RGB", (1080, 1080), color="#1e293b")
    draw = ImageDraw.Draw(img)
    
    # Load fonts
    title_font = ImageFont.truetype("fonts/Poppins-Bold.ttf", 64)
    subtitle_font = ImageFont.truetype("fonts/Poppins-Regular.ttf", 32)
    
    # Title (centered)
    title_bbox = draw.textbbox((0, 0), title, font=title_font)
    title_width = title_bbox[2] - title_bbox[0]
    title_x = (1080 - title_width) // 2
    draw.text((title_x, 400), title, font=title_font, fill="white")
    
    # Subtitle
    sub_bbox = draw.textbbox((0, 0), subtitle, font=subtitle_font)
    sub_width = sub_bbox[2] - sub_bbox[0]
    sub_x = (1080 - sub_width) // 2
    draw.text((sub_x, 500), subtitle, font=subtitle_font, fill="#94a3b8")
    
    # Logo/watermark
    logo = Image.open("logo.png").resize((100, 100))
    img.paste(logo, (50, 50), logo if logo.mode == "RGBA" else None)
    
    img.save(output_path)
    print(f"Created: {output_path}")

# Generate 10 images
titles = [
    ("5 Python Tips", "For beginners"),
    ("AI Tools Roundup", "March 2026"),
    ("Freelancing Guide", "Start today"),
]

for i, (title, subtitle) in enumerate(titles):
    create_social_image(title, subtitle, f"post_{i+1}.png")

Approach 2: Canva API (designer-quality)

Canva’s API lets you generate images from templates:

import requests, os

CANVA_API_KEY = os.environ["CANVA_API_KEY"]

def create_from_canva_template(template_id, text_replacements):
    # Get template
    template_url = f"https://api.canva.com/rest/v1/designs/{template_id}"
    headers = {"Authorization": f"Bearer {CANVA_API_KEY}"}
    
    # Create design from template
    r = requests.post(
        "https://api.canva.com/rest/v1/designs",
        headers=headers,
        json={"design_type": {"type": "preset", "name": "instagram_post"}, "title": "Auto-generated"},
    )
    design_id = r.json()["design"]["id"]
    
    # Export as PNG
    export_url = f"https://api.canva.com/rest/v1/designs/{design_id}/exports"
    r = requests.post(export_url, headers=headers, json={"format": {"type": "png"}})
    export_id = r.json()["job"]["id"]
    
    # Wait for export to complete
    # ... polling logic ...
    
    return f"https://export.canva.com/.../{export_id}.png"

Pros: Designer-quality templates, easy for non-developers to create Cons: Requires Canva Pro ($13/mo), API is complex

Approach 3: HTML + CSS → Image (Puppeteer/Playwright)

Design your template in HTML/CSS, then render to image:

from playwright.sync_api import sync_playwright

def html_to_image(html_content, output_path, width=1080, height=1080):
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page(viewport={"width": width, "height": height})
        page.set_content(html_content)
        page.screenshot(path=output_path, full_page=True)
        browser.close()

# Template
template = """
<!DOCTYPE html>
<html>
<head>
<style>
    body { margin: 0; width: 1080px; height: 1080px; background: linear-gradient(135deg, #1e293b, #334155); 
           display: flex; flex-direction: column; justify-content: center; align-items: center; 
           font-family: 'Inter', sans-serif; color: white; }
    h1 { font-size: 64px; font-weight: 800; margin: 0; text-align: center; }
    p { font-size: 32px; color: #94a3b8; margin-top: 20px; }
    .logo { position: absolute; top: 40px; left: 40px; width: 80px; }
</style>
</head>
<body>
    <img src="logo.png" class="logo">
    <h1>{title}</h1>
    <p>{subtitle}</p>
</body>
</html>
"""

titles = [("5 Python Tips", "For beginners"), ("AI Tools Roundup", "March 2026")]
for i, (title, subtitle) in enumerate(titles):
    html = template.format(title=title, subtitle=subtitle)
    html_to_image(html, f"post_{i+1}.png")

Pros: Full CSS control, designers can create templates Cons: Requires Playwright, slower than Pillow

Approach 4: Bulk generation from data

Generate images from a CSV/database:

import csv, os
from PIL import Image, ImageDraw, ImageFont

def generate_from_csv(csv_path, template_func, output_dir):
    os.makedirs(output_dir, exist_ok=True)
    with open(csv_path) as f:
        reader = csv.DictReader(f)
        for i, row in enumerate(reader):
            img = template_func(row)
            img.save(f"{output_dir}/post_{i+1}.png")
            print(f"Generated post_{i+1}.png: {row['title']}")

# CSV: title,subtitle,category
# 5 Python Tips,Beginners,Python
# AI Tools Roundup,March 2026,AI

def my_template(row):
    img = Image.new("RGB", (1080, 1080), "#1e293b")
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype("Poppins-Bold.ttf", 64)
    draw.text((100, 400), row["title"], font=font, fill="white")
    draw.text((100, 500), row["subtitle"], font=ImageFont.truetype("Poppins-Regular.ttf", 32), fill="#94a3b8")
    return img

generate_from_csv("posts.csv", my_template, "output/")

Scheduling and automation

Run daily via cron

# Generate social images daily at 6 AM
0 6 * * * cd /path/to/project && python generate_images.py

GitHub Actions

name: Generate Social Images
on:
  schedule:
    - cron: '0 6 * * *'
  workflow_dispatch:
jobs:
  generate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: pip install pillow
      - run: python generate_images.py
      - uses: actions/upload-artifact@v4
        with:
          name: social-images
          path: output/

Common mistakes

  • Inconsistent branding — use the same fonts, colors, and layout every time
  • No template system — if you’re designing each image manually, you’re doing it wrong
  • Wrong dimensions — Instagram: 1080x1080 (square) or 1080x1350 (portrait). Twitter: 1200x675. LinkedIn: 1200x627.
  • Text too small — mobile users can’t read tiny text. Minimum 32px for body, 64px for titles.
  • No logo/watermark — always include your brand. People share images without context.
  • Not testing — generate 3-5 images and check them on your phone before automating.

Key Concepts

  • Template = reusable design with placeholders for text/images
  • Pillow = Python image processing library
  • Playwright = browser automation (can render HTML to image)
  • Bulk generation = creating many images from data (CSV, DB)
  • Aspect ratio = image dimensions (1:1 square, 4:5 portrait, 16:9 landscape)

What to Try Next

FAQ

Pillow vs Canva API vs Playwright — which to use?

Pillow = free, fast, code-based (good for developers). Canva API = designer-quality templates (good for non-developers). Playwright = full CSS control (good for complex designs). Start with Pillow for simplicity.

Can I use custom fonts?

Yes — download .ttf/.otf files and use ImageFont.truetype("font.ttf", size). Google Fonts has free commercial-use fonts. Store them in your project directory.

How do I handle different platforms (Instagram, Twitter, LinkedIn)?

Create separate templates for each platform with the correct dimensions. Or use one template and crop/resize for different platforms. Instagram: 1080x1080. Twitter: 1200x675. LinkedIn: 1200x627.

Can I automate video creation too?

Yes — use moviepy (Python) or ffmpeg to generate videos from images + audio. But video automation is more complex. Start with images; add video later. See Best Tools for Freelancers.