Spaces:
Runtime error
Runtime error
Update image_generator.py
Browse files- image_generator.py +137 -30
image_generator.py
CHANGED
|
@@ -118,9 +118,93 @@ PRESET_HEADERS = [
|
|
| 118 |
"吉祥如意",
|
| 119 |
"福氣滿滿",
|
| 120 |
"吉祥安康",
|
| 121 |
-
"順心如意"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
]
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
# 經典長輩圖必備霓虹彩虹漸層字體配色系列
|
| 125 |
RAINBOW_COLORS = [
|
| 126 |
"#FF0033", # 霓虹紅
|
|
@@ -235,20 +319,25 @@ def generate_text_with_gemini(api_key, query):
|
|
| 235 |
|
| 236 |
def expand_prompt_with_gemini(api_key, query):
|
| 237 |
query_en = translate_to_english(query)
|
|
|
|
| 238 |
|
| 239 |
default_expanded = (
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
)
|
| 249 |
|
| 250 |
if not api_key:
|
| 251 |
-
logger.info("未偵測到 Gemini API Key。使用預設封裝 Prompt。")
|
| 252 |
return default_expanded
|
| 253 |
|
| 254 |
try:
|
|
@@ -257,31 +346,42 @@ def expand_prompt_with_gemini(api_key, query):
|
|
| 257 |
model = genai.GenerativeModel('gemini-2.5-flash')
|
| 258 |
|
| 259 |
prompt = (
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
)
|
|
|
|
| 278 |
response = model.generate_content(prompt)
|
| 279 |
expanded = response.text.strip().replace('"', '').replace('「', '').replace('」', '')
|
| 280 |
-
logger.info(f"Gemini 擴寫的英文生圖提示詞: {expanded}")
|
| 281 |
return expanded
|
|
|
|
| 282 |
except Exception as e:
|
| 283 |
logger.error(f"Gemini 提示詞擴寫錯誤: {e}")
|
| 284 |
-
return default_expanded
|
|
|
|
| 285 |
|
| 286 |
def draw_rainbow_text(draw, text, x_center, y_start, font, stroke_width=6, stroke_fill="#000000"):
|
| 287 |
if not font:
|
|
@@ -354,7 +454,14 @@ def overlay_text_on_image(image_path, greeting_text, output_path):
|
|
| 354 |
stroke_fill="#000000"
|
| 355 |
)
|
| 356 |
|
| 357 |
-
footer_text =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
footer_y = 930
|
| 359 |
if footer_font:
|
| 360 |
draw.text(
|
|
|
|
| 118 |
"吉祥如意",
|
| 119 |
"福氣滿滿",
|
| 120 |
"吉祥安康",
|
| 121 |
+
"順心如意",
|
| 122 |
+
"今日開機",
|
| 123 |
+
"福氣載入",
|
| 124 |
+
"平安上線",
|
| 125 |
+
"人生重整",
|
| 126 |
+
"群組復活",
|
| 127 |
+
"努力省電",
|
| 128 |
+
"腦袋離線",
|
| 129 |
+
"系統正常",
|
| 130 |
+
"快樂重啟"
|
| 131 |
]
|
| 132 |
|
| 133 |
+
# ==============================================================================
|
| 134 |
+
# 怪圖風格素材池:讓每次早安圖更有「好怪,再看一眼」的變化
|
| 135 |
+
# ==============================================================================
|
| 136 |
+
|
| 137 |
+
WEIRD_STYLE_PRESETS = [
|
| 138 |
+
"a low-budget 1980s Taiwanese TV shopping commercial set with fake luxury props and overly dramatic lighting",
|
| 139 |
+
"a surreal plastic toy diorama where tiny objects become huge and huge objects become tiny",
|
| 140 |
+
"a chaotic internet meme collage with awkward cheerful energy and impossible scale",
|
| 141 |
+
"a retro Windows 95 clip-art poster mixed with Taiwanese neon sign aesthetics",
|
| 142 |
+
"an overdecorated karaoke music video background with fog machine, rainbow spotlights, and fake glamour",
|
| 143 |
+
"a fake inspirational corporate poster gone wrong, treating a meaningless action like a world-saving mission",
|
| 144 |
+
"a children's craft project made by an overconfident robot, with stickers, glitter, paper cutouts, and messy charm",
|
| 145 |
+
"a surreal supermarket advertisement where the subject is displayed like a sacred limited-edition product",
|
| 146 |
+
"a bizarre morning news broadcast set reporting a completely unimportant event as breaking news",
|
| 147 |
+
"a dreamlike school bulletin board poster with too many decorations and zero design logic"
|
| 148 |
+
]
|
| 149 |
+
|
| 150 |
+
WEIRD_VISUAL_GAGS = [
|
| 151 |
+
"the subject is posing heroically while doing something completely useless",
|
| 152 |
+
"the subject is being celebrated by tiny objects like a world-saving hero",
|
| 153 |
+
"the subject is surrounded by floating breakfast items as if they are planets",
|
| 154 |
+
"the subject is receiving an official award for a meaningless achievement",
|
| 155 |
+
"the subject is dramatically illuminated while failing at a very simple task",
|
| 156 |
+
"the subject is sitting on a plastic throne made of random everyday objects",
|
| 157 |
+
"the subject appears in a breaking-news scene about a tiny inconvenience",
|
| 158 |
+
"the subject is leading a ceremonial parade for no clear reason",
|
| 159 |
+
"the subject is treated like a legendary mascot of emotional damage",
|
| 160 |
+
"the subject looks extremely confident despite clearly misunderstanding the situation"
|
| 161 |
+
]
|
| 162 |
+
|
| 163 |
+
WEIRD_COMPOSITIONS = [
|
| 164 |
+
"fisheye lens perspective, slightly too close to the subject, funny and uncomfortable but not scary",
|
| 165 |
+
"symmetrical poster composition with the subject placed too seriously in the center",
|
| 166 |
+
"busy collage composition with many strange but harmless background details",
|
| 167 |
+
"dramatic low-angle hero shot for a completely ridiculous subject",
|
| 168 |
+
"fake advertisement layout with exaggerated product-poster energy",
|
| 169 |
+
"crowded scene with tiny background characters reacting in confusion",
|
| 170 |
+
"overly majestic composition with sparkles, fog, and unnecessary visual importance",
|
| 171 |
+
"awkward family-photo composition, but everyone is an object or mascot"
|
| 172 |
+
]
|
| 173 |
+
|
| 174 |
+
ELDER_CARD_ELEMENTS = [
|
| 175 |
+
"dramatic golden sunrise",
|
| 176 |
+
"glowing clouds",
|
| 177 |
+
"oversized lotus flowers",
|
| 178 |
+
"sparkles",
|
| 179 |
+
"rainbow gradients",
|
| 180 |
+
"shiny flowers",
|
| 181 |
+
"neon blessing-card borders",
|
| 182 |
+
"floating hearts",
|
| 183 |
+
"cheap clip-art stars",
|
| 184 |
+
"butterflies",
|
| 185 |
+
"glitter effects",
|
| 186 |
+
"overly saturated colorful background"
|
| 187 |
+
]
|
| 188 |
+
|
| 189 |
+
COLOR_MOODS = [
|
| 190 |
+
"extremely oversaturated rainbow colors",
|
| 191 |
+
"neon pink and gold morning glow",
|
| 192 |
+
"cheap fluorescent green and purple color palette",
|
| 193 |
+
"glossy candy-like colors",
|
| 194 |
+
"overexposed golden sunlight with chaotic colorful shadows",
|
| 195 |
+
"unnecessarily cheerful pastel colors with dramatic contrast"
|
| 196 |
+
]
|
| 197 |
+
|
| 198 |
+
def build_weird_recipe():
|
| 199 |
+
elder_elements = random.sample(ELDER_CARD_ELEMENTS, k=random.randint(3, 5))
|
| 200 |
+
return {
|
| 201 |
+
"style": random.choice(WEIRD_STYLE_PRESETS),
|
| 202 |
+
"gag": random.choice(WEIRD_VISUAL_GAGS),
|
| 203 |
+
"composition": random.choice(WEIRD_COMPOSITIONS),
|
| 204 |
+
"elder_elements": ", ".join(elder_elements),
|
| 205 |
+
"color_mood": random.choice(COLOR_MOODS)
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
# 經典長輩圖必備霓虹彩虹漸層字體配色系列
|
| 209 |
RAINBOW_COLORS = [
|
| 210 |
"#FF0033", # 霓虹紅
|
|
|
|
| 319 |
|
| 320 |
def expand_prompt_with_gemini(api_key, query):
|
| 321 |
query_en = translate_to_english(query)
|
| 322 |
+
weird_recipe = build_weird_recipe()
|
| 323 |
|
| 324 |
default_expanded = (
|
| 325 |
+
f"A funny, absurd, and strangely memorable Taiwanese good morning meme image featuring {query_en} as the main subject. "
|
| 326 |
+
f"The image should create a 'this is so weird that I need to look again' feeling, while remaining harmless and funny. "
|
| 327 |
+
f"Weird visual style: {weird_recipe['style']}. "
|
| 328 |
+
f"Visual gag: {weird_recipe['gag']}. "
|
| 329 |
+
f"Composition: {weird_recipe['composition']}. "
|
| 330 |
+
f"Taiwanese elder-card elements to include: {weird_recipe['elder_elements']}. "
|
| 331 |
+
f"Color mood: {weird_recipe['color_mood']}. "
|
| 332 |
+
f"The subject must be clearly visible and related to {query_en}, but the scene should feel surreal, ridiculous, overly confident, and awkwardly cheerful. "
|
| 333 |
+
f"Do not always use the same lotus-and-sunrise layout. Make the scene visually different each time. "
|
| 334 |
+
f"No readable text inside the image, because Traditional Chinese text will be added later by the program. "
|
| 335 |
+
f"No real public figures, no political symbols, no hate symbols, no violence, no sexual content, no horror, no offensive stereotypes. "
|
| 336 |
+
f"High resolution, colorful, detailed, humorous, absurd, meme-like, strange but not scary."
|
| 337 |
)
|
| 338 |
|
| 339 |
if not api_key:
|
| 340 |
+
logger.info("未偵測到 Gemini API Key。使用預設怪圖封裝 Prompt。")
|
| 341 |
return default_expanded
|
| 342 |
|
| 343 |
try:
|
|
|
|
| 346 |
model = genai.GenerativeModel('gemini-2.5-flash')
|
| 347 |
|
| 348 |
prompt = (
|
| 349 |
+
f"Please translate the Chinese phrase '{query}' into English, "
|
| 350 |
+
f"then expand it into a detailed image generation prompt for a funny, absurd Taiwanese good morning meme image.\n\n"
|
| 351 |
+
|
| 352 |
+
f"Goal:\n"
|
| 353 |
+
f"Make the image feel strange, ridiculous, and visually memorable, like something so weird that group chat members would look twice and want to roast it.\n\n"
|
| 354 |
+
|
| 355 |
+
f"Random weirdness recipe for this image:\n"
|
| 356 |
+
f"- Main subject: {query_en}\n"
|
| 357 |
+
f"- Weird visual style: {weird_recipe['style']}\n"
|
| 358 |
+
f"- Visual gag: {weird_recipe['gag']}\n"
|
| 359 |
+
f"- Composition: {weird_recipe['composition']}\n"
|
| 360 |
+
f"- Elder-card elements: {weird_recipe['elder_elements']}\n"
|
| 361 |
+
f"- Color mood: {weird_recipe['color_mood']}\n\n"
|
| 362 |
+
|
| 363 |
+
f"Requirements:\n"
|
| 364 |
+
f"1. The main subject must clearly be related to: '{query_en}'.\n"
|
| 365 |
+
f"2. The image must look different from a normal good morning card. Avoid repeating the same lotus-sunrise-flower layout every time.\n"
|
| 366 |
+
f"3. Combine Taiwanese elder-style good morning card aesthetics with absurd internet meme humor.\n"
|
| 367 |
+
f"4. Make the scene strange but harmless: funny, awkwardly cheerful, overly serious, and visually ridiculous.\n"
|
| 368 |
+
f"5. Include one clear visual gag where the subject looks extremely confident while doing something meaningless, useless, or misunderstood.\n"
|
| 369 |
+
f"6. Keep it colorful, detailed, oversaturated, flamboyant, and easy for group chat members to roast.\n"
|
| 370 |
+
f"7. Do not include real public figures, political symbols, hate symbols, violence, sexual content, horror, or offensive stereotypes.\n"
|
| 371 |
+
f"8. Do not generate readable text inside the image, because Traditional Chinese text will be added later by the program.\n\n"
|
| 372 |
+
|
| 373 |
+
f"Only return the final English image prompt. Do not include explanations, labels, or quotation marks."
|
| 374 |
)
|
| 375 |
+
|
| 376 |
response = model.generate_content(prompt)
|
| 377 |
expanded = response.text.strip().replace('"', '').replace('「', '').replace('」', '')
|
| 378 |
+
logger.info(f"Gemini 擴寫的怪圖英文生圖提示詞: {expanded}")
|
| 379 |
return expanded
|
| 380 |
+
|
| 381 |
except Exception as e:
|
| 382 |
logger.error(f"Gemini 提示詞擴寫錯誤: {e}")
|
| 383 |
+
return default_expanded
|
| 384 |
+
|
| 385 |
|
| 386 |
def draw_rainbow_text(draw, text, x_center, y_start, font, stroke_width=6, stroke_fill="#000000"):
|
| 387 |
if not font:
|
|
|
|
| 454 |
stroke_fill="#000000"
|
| 455 |
)
|
| 456 |
|
| 457 |
+
footer_text = random.choice([
|
| 458 |
+
"認同請分享,不認同也沒差!",
|
| 459 |
+
"看到請回,群組需要復活!",
|
| 460 |
+
"今日能量不足,請互相充電!",
|
| 461 |
+
"早安成功,腦袋另行通知!",
|
| 462 |
+
"分享此圖,福氣自動重開機!",
|
| 463 |
+
"本圖無用,但非常努力!"
|
| 464 |
+
])
|
| 465 |
footer_y = 930
|
| 466 |
if footer_font:
|
| 467 |
draw.text(
|