Content & Marketing

AI Image & Video Generation: Step-by-Step Python Automation

AI Image & Video Generation: Step-by-Step Python Automation

1. Architecting a Python-Powered Visual Pipeline

Core SDK Ecosystem

Integrating generative models into your AI Content Creation & Marketing Automation stack requires a structured Python environment. Start by isolating dependencies with venv or poetry. The modern ecosystem relies on three primary SDKs: openai for DALL-E 3, replicate for hosted open-weight models, and stability-sdk for enterprise-grade image pipelines.

Install them via pip:

pip install openai replicate stability-sdk python-dotenv

Hardware & API Considerations

Local GPU inference demands significant VRAM. Cloud APIs remain the pragmatic choice for most teams. Always implement exponential backoff for rate limits. Store credentials securely using a .env file. Never hardcode API keys in version control.

import os
from dotenv import load_dotenv
load_dotenv()

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
REPLICATE_API_TOKEN = os.getenv("REPLICATE_API_TOKEN")

Debugging Tip: If you encounter 401 Unauthorized, verify your .env file is in the working directory. Confirm your account has active billing and valid scopes.

2. Pre-Generation: Prompt Engineering & Text Automation

Dynamic Prompt Templates

Raw prompts rarely yield consistent brand assets. Use jinja2 to parameterize templates and inject campaign variables. This ensures uniform lighting, style tags, and negative prompts across batches.

from jinja2 import Template

prompt_template = Template("""
{{ subject }} in a {{ style }} style, {{ lighting }} lighting, 
high resolution, --v 6.0
""")

def build_prompt(subject: str, style: str = "cinematic", lighting: str = "studio") -> str:
 return prompt_template.render(subject=subject, style=style, lighting=lighting).strip()

LLM-Assisted Metadata Generation

Before rendering assets, automate prompt refinement by chaining LLM calls with AI Copywriting Workflows to ensure brand-aligned outputs. Use langchain to validate prompts against a style guide.

from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate

llm = ChatOpenAI(model="gpt-4o-mini")
validator = PromptTemplate.from_template("Refine this prompt for a minimalist tech brand: {prompt}")
chain = validator | llm

Debugging Tip: Add temperature=0.2 to LLM calls. This reduces creative variance and maintains predictable prompt structures.

3. Image Generation: API Integration & Batch Processing

DALL-E 3 & Stable Diffusion via Python

The openai SDK handles image generation synchronously by default. Wrap calls in error handling to capture RateLimitError and APIConnectionError. Always specify quality="hd" and response_format="b64_json" for direct pipeline integration.

from openai import OpenAI
import base64

client = OpenAI(api_key=OPENAI_API_KEY)

def generate_image(prompt: str, output_path: str):
 response = client.images.generate(
 model="dall-e-3", prompt=prompt, n=1, size="1024x1024", response_format="b64_json"
 )
 image_data = base64.b64decode(response.data[0].b64_json)
 with open(output_path, "wb") as f:
 f.write(image_data)

Asynchronous Batch Requests

Scale generation using asyncio and aiohttp to process dozens of prompts concurrently. This reduces idle wait time from minutes to seconds.

import asyncio
from openai import AsyncOpenAI

async_client = AsyncOpenAI(api_key=OPENAI_API_KEY)

async def batch_generate(prompts: list[str]):
 tasks = [async_client.images.generate(model="dall-e-3", prompt=p, n=1, size="1024x1024") for p in prompts]
 return await asyncio.gather(*tasks, return_exceptions=True)

Image Post-Processing with Pillow/OpenCV

Raw outputs often require resizing, watermarking, or format conversion. Pillow provides a lightweight solution for these transformations. For platform-specific assets like channel art, developers can adapt the Create YouTube thumbnails with DALL-E 3 and Python script to handle custom aspect ratios and overlay text.

from PIL import Image

def resize_and_pad(image_path: str, target_size: tuple[int, int], output_path: str):
 img = Image.open(image_path).convert("RGB")
 img.thumbnail(target_size, Image.Resampling.LANCZOS)
 img.save(output_path, "JPEG", quality=95)

Debugging Tip: If Pillow throws DecompressionBombError, increase Image.MAX_IMAGE_PIXELS. Validate source dimensions before processing.

4. Video Synthesis & Frame-Level Manipulation

RunwayML & Pika API Integration

Video generation APIs typically operate on an asynchronous job model. Submit a prompt, poll for completion, and download the resulting MP4. Use requests with a retry strategy to handle transient network failures.

import time
import requests

def submit_video_job(api_url: str, headers: dict, prompt: str) -> str:
 resp = requests.post(f"{api_url}/generate", json={"prompt": prompt}, headers=headers)
 return resp.json()["job_id"]

def poll_until_ready(api_url: str, headers: dict, job_id: str, interval: int = 5) -> str:
 while True:
 status = requests.get(f"{api_url}/jobs/{job_id}", headers=headers).json()
 if status["status"] == "completed":
 return status["video_url"]
 time.sleep(interval)

FFmpeg Automation via MoviePy

Once raw clips are generated, streamline repetitive cuts and transitions using Python scripts for bulk video editing to maintain consistent pacing across campaigns. moviepy wraps FFmpeg for Pythonic timeline manipulation.

from moviepy.editor import VideoFileClip, concatenate_videoclips

def stitch_clips(clip_paths: list[str], output_path: str):
 clips = [VideoFileClip(p) for p in clip_paths]
 final = concatenate_videoclips(clips, method="compose")
 final.write_videofile(output_path, codec="libx264", audio_codec="aac")

Frame Interpolation & Upscaling

AI video often outputs at 24fps or 720p. Use ffmpeg-python to interpolate frames to 60fps or upscale to 1080p without manual rendering.

import ffmpeg

def upscale_video(input_path: str, output_path: str):
 stream = ffmpeg.input(input_path)
 stream = ffmpeg.filter(stream, 'scale', '1920:1080')
 ffmpeg.output(stream, output_path).run(overwrite_output=True)

Debugging Tip: If FFmpeg fails with Invalid data found, verify the codec matches the container. Add -c:v libx264 -preset fast to force standard encoding.

5. Audio Enhancement & Accessibility Automation

Speech-to-Text with Whisper

Accessibility drives engagement. The openai-whisper library transcribes audio locally with high accuracy. Process generated voiceovers or background tracks automatically.

import whisper

model = whisper.load_model("base")
result = model.transcribe("voiceover.mp3", language="en")
print(result["text"])

SRT Generation & Syncing

Convert timestamps to SRT format using the srt package. This enables platform-native captioning without manual alignment. To maximize reach on short-form platforms, implement the Auto-caption TikTok videos using Whisper API pipeline for accurate, timestamped subtitles.

import srt
from datetime import timedelta

def generate_srt(segments: list, output_path: str):
 subs = [srt.Subtitle(index=i, start=timedelta(seconds=s["start"]), 
 end=timedelta(seconds=s["end"]), content=s["text"]) 
 for i, s in enumerate(segments, 1)]
 with open(output_path, "w") as f:
 f.write(srt.compose(subs))

Voice Cloning & TTS Integration

Use pydub to normalize audio levels and merge background music with AI voiceovers. Maintain consistent loudness standards across platforms.

from pydub import AudioSegment

voice = AudioSegment.from_mp3("voiceover.mp3")
bgm = AudioSegment.from_mp3("background.mp3")
bgm = bgm - 15 # Lower music volume
final = voice.overlay(bgm)
final.export("final_mix.mp3", format="mp3")

Debugging Tip: If pydub raises RuntimeError: Couldn't find ffmpeg, ensure ffmpeg is installed system-wide. Add it to your system PATH.

6. Deployment, Scheduling & Analytics

Cloud Storage & CDN Routing

Rendered assets require fast delivery. Upload to S3-compatible storage using boto3 and generate pre-signed URLs for secure sharing.

import boto3

def upload_to_s3(file_path: str, bucket: str, key: str):
 s3 = boto3.client("s3")
 s3.upload_file(file_path, bucket, key, ExtraArgs={"ContentType": "video/mp4"})
 return f"https://{bucket}.s3.amazonaws.com/{key}"

Platform API Publishing

After rendering and optimizing assets, connect your pipeline directly to Automated Social Media Posting endpoints to publish content on optimal schedules without manual intervention. Use requests to hit platform-specific upload endpoints.

Performance Tracking

Log generation costs, API latency, and engagement metrics using pandas. Export to CSV or push to a BI dashboard for continuous optimization.

import pandas as pd

metrics = pd.DataFrame({
 "asset_id": ["vid_001", "img_002"],
 "generation_time_sec": [12.4, 3.1],
 "api_cost_usd": [0.08, 0.04],
 "engagement_score": [85, 92]
})
metrics.to_csv("pipeline_metrics.csv", index=False)

Debugging Tip: If boto3 throws NoCredentialsError, verify your AWS CLI is configured. Pass aws_access_key_id and aws_secret_access_key explicitly if needed.