File size: 3,881 Bytes
345855e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | import os
import uuid
import asyncio
from datetime import datetime
from PIL import Image, ImageDraw, ImageFont
# -------------------------------------------------
# SAFE OUTPUT DIRECTORY
# -------------------------------------------------
OUTPUT_DIR = "jobs/thumbnails"
os.makedirs(OUTPUT_DIR, exist_ok=True)
# -------------------------------------------------
# CONTEXT NORMALIZER
# -------------------------------------------------
def normalize_context(context):
if isinstance(context, dict):
return {
"title": context.get("title"),
"hook": context.get("hook"),
"strategy": context.get("strategy", {}),
"text": context.get("text", ""),
"video_path": context.get("video_path")
}
return {
"title": getattr(context, "title", None),
"hook": getattr(context, "hook", None),
"strategy": getattr(context, "strategy", {}),
"text": getattr(context, "text", ""),
"video_path": getattr(context, "video_path", None)
}
# -------------------------------------------------
# TEXT EXTRACTOR
# -------------------------------------------------
def extract_text(ctx):
if ctx.get("hook"):
return ctx["hook"]
if isinstance(ctx.get("strategy"), dict):
hook = ctx["strategy"].get("hook")
if hook:
return hook
return ctx.get("text") or "Create engaging content that stands out."
# -------------------------------------------------
# SAFE FONT LOADER
# -------------------------------------------------
def load_font(size):
"""
Tries system fonts safely.
Falls back to default PIL font if unavailable.
"""
try:
return ImageFont.truetype("arial.ttf", size)
except Exception:
return ImageFont.load_default()
# -------------------------------------------------
# THUMBNAIL GENERATOR CORE
# -------------------------------------------------
def build_thumbnail(text, width=1280, height=720):
img = Image.new("RGB", (width, height), color=(10, 10, 10))
draw = ImageDraw.Draw(img)
# Accent style bar
draw.rectangle([0, 0, 20, height], fill=(232, 255, 71))
# Title text
font_large = load_font(64)
font_small = load_font(36)
wrapped_text = text[:120]
draw.text(
(60, 200),
wrapped_text,
font=font_large,
fill=(232, 232, 232)
)
draw.text(
(60, 320),
"AI-Generated Content",
font=font_small,
fill=(136, 136, 136)
)
return img
# -------------------------------------------------
# MAIN ENTRYPOINT
# -------------------------------------------------
async def run(context):
ctx = normalize_context(context)
batch_id = str(uuid.uuid4())
started_at = datetime.utcnow().isoformat()
try:
text = extract_text(ctx)
# -------------------------------------------------
# BUILD IMAGE (CPU SAFE)
# -------------------------------------------------
img = await asyncio.to_thread(build_thumbnail, text)
file_name = f"{batch_id}_thumbnail.png"
output_path = os.path.join(OUTPUT_DIR, file_name)
img.save(output_path)
# -------------------------------------------------
# RESPONSE
# -------------------------------------------------
return {
"status": "success",
"task": "generate-thumbnail",
"batch_id": batch_id,
"started_at": started_at,
"completed_at": datetime.utcnow().isoformat(),
"thumbnail_path": output_path
}
except Exception as e:
return {
"status": "error",
"task": "generate-thumbnail",
"batch_id": batch_id,
"message": str(e),
"stage": "thumbnail_generation_failed"
} |