Implementing Automatic Social Media Image Generation from Product Cards
Creating graphics manually for each product and each social network is expensive and slow. Automation: take data from a product card (photo, name, price, brand) and generate ready-made banners in required formats—for Telegram, VK, Instagram, Stories.
Two Generation Approaches
Template-based approach (fast, predictable) — overlay product data onto pre-prepared design templates. Use Pillow (Python) or Sharp (Node.js).
AI generation (creative, variable) — generate backgrounds or entire images via Stable Diffusion, Midjourney API, DALL-E with subsequent text overlay.
For most stores, the first approach—template-based—provides branded results without surprises.
Pillow Implementation
from PIL import Image, ImageDraw, ImageFont
import httpx
from io import BytesIO
class ProductBannerGenerator:
TEMPLATES = {
'feed': (1080, 1080), # Instagram/VK post
'story': (1080, 1920), # Instagram/VK Stories
'telegram': (1280, 640), # Telegram preview
}
def generate(self, product: dict, format: str = 'feed') -> bytes:
width, height = self.TEMPLATES[format]
# Create canvas
canvas = Image.new('RGB', (width, height), color='#FFFFFF')
draw = ImageDraw.Draw(canvas)
# Load and place product photo
if product.get('image_url'):
product_img = self._load_image(product['image_url'])
product_img = self._fit_image(product_img, (width, int(height * 0.65)))
canvas.paste(product_img, (0, 0))
# Gradient underlay for text
gradient = self._create_gradient((width, int(height * 0.4)), '#000000', alpha_start=0, alpha_end=180)
canvas.paste(gradient, (0, int(height * 0.6)), mask=gradient)
# Fonts
font_title = ImageFont.truetype('fonts/Inter-Bold.ttf', size=52)
font_price = ImageFont.truetype('fonts/Inter-ExtraBold.ttf', size=72)
font_small = ImageFont.truetype('fonts/Inter-Regular.ttf', size=36)
# Text
y_offset = int(height * 0.68)
draw.text((60, y_offset), product['name'], font=font_title, fill='white')
draw.text((60, y_offset + 70), f"{product['price']:,.0f} USD", font=font_price, fill='#FFD700')
# Brand logo
logo = Image.open('assets/logo.png').resize((200, 60))
canvas.paste(logo, (width - 220, 20), mask=logo)
# Convert to bytes
output = BytesIO()
canvas.save(output, 'JPEG', quality=90)
return output.getvalue()
def _load_image(self, url: str) -> Image.Image:
resp = httpx.get(url, timeout=15)
return Image.open(BytesIO(resp.content)).convert('RGBA')
Generation via Replicate API (Stable Diffusion)
import replicate
def generate_ai_background(product_name: str, category: str) -> str:
output = replicate.run(
"stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf",
input={
"prompt": f"minimalist product photography background for {category}, "
f"clean white studio, soft shadows, high-end commercial photo",
"negative_prompt": "text, watermark, ugly, blurry",
"width": 1080,
"height": 1080,
}
)
return output[0] # URL of generated image
Formats and Sizes
| Platform | Post | Stories |
|---|---|---|
| 1080×1080, 1080×1350 | 1080×1920 | |
| VK | 1080×1080 | 1080×1920 |
| Telegram channel | 1280×640 | — |
The system generates all variants in one call.
Timeline
Banner generator on templates with 3–5 formats: 4–6 business days. With AI background generation via Replicate: +2–3 days.







