bakyt92 commited on
Commit
5cafc1f
Β·
1 Parent(s): 0091a87

update app.py - add pattern preview

Browse files
Files changed (1) hide show
  1. app.py +114 -15
app.py CHANGED
@@ -3,6 +3,8 @@ import requests
3
  from PIL import Image, ImageDraw, ImageFont
4
  import io
5
  import openai
 
 
6
 
7
  # Image Generation Functions
8
  def generate_ideogram_image(api_key, prompt, aspect_ratio="ASPECT_1_1"):
@@ -315,6 +317,81 @@ def test_text_overlay():
315
  print(f"Test failed completely: {e}")
316
  return None, f"Test failed: {e}", None
317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318
  # Create the Gradio Interface
319
  with gr.Blocks(title="AI Image Generator & Text Overlay") as demo:
320
  gr.Markdown("# 🎨 AI Image Generator & Text Overlay")
@@ -402,19 +479,32 @@ with gr.Blocks(title="AI Image Generator & Text Overlay") as demo:
402
 
403
  with gr.Row():
404
  with gr.Column():
405
- # Text pattern selection
406
- pattern = gr.Dropdown(
407
- choices=[
408
- "2-lines-top",
409
- "2-lines-center",
410
- "2-lines-bottom",
411
- "3-lines-top",
412
- "3-lines-center",
413
- "3-lines-bottom"
414
- ],
415
- value="2-lines-top",
416
- label="Layout Pattern"
417
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
418
 
419
  # Text input fields
420
  line1_inp = gr.Textbox(
@@ -444,7 +534,7 @@ with gr.Blocks(title="AI Image Generator & Text Overlay") as demo:
444
  value="#FFFFFF"
445
  )
446
 
447
- # NEW: Add outline option
448
  add_outline = gr.Checkbox(
449
  label="Add black outline",
450
  value=True,
@@ -457,7 +547,16 @@ with gr.Blocks(title="AI Image Generator & Text Overlay") as demo:
457
  # Output
458
  text_image = gr.Image(label="Image with Text Overlay")
459
 
460
- # Event handler for Tab 2
 
 
 
 
 
 
 
 
 
461
  add_text_btn.click(
462
  add_text_to_image,
463
  inputs=[current_image, pattern, line1_inp, line2_inp, line3_inp, font_size, color, add_outline],
 
3
  from PIL import Image, ImageDraw, ImageFont
4
  import io
5
  import openai
6
+ import base64
7
+ from io import BytesIO
8
 
9
  # Image Generation Functions
10
  def generate_ideogram_image(api_key, prompt, aspect_ratio="ASPECT_1_1"):
 
317
  print(f"Test failed completely: {e}")
318
  return None, f"Test failed: {e}", None
319
 
320
+ # Function to create template images
321
+ def create_pattern_template(pattern, width=200, height=120):
322
+ """Create a visual template showing text placement for each pattern"""
323
+ # Create black background
324
+ img = Image.new('RGB', (width, height), color='black')
325
+ draw = ImageDraw.Draw(img)
326
+
327
+ # Define positions based on pattern
328
+ positions = []
329
+ if pattern == "2-lines-top":
330
+ positions = [(width // 2, int(height * 0.15)), (width // 2, int(height * 0.35))]
331
+ elif pattern == "2-lines-center":
332
+ positions = [(width // 2, int(height * 0.35)), (width // 2, int(height * 0.65))]
333
+ elif pattern == "2-lines-bottom":
334
+ positions = [(width // 2, int(height * 0.65)), (width // 2, int(height * 0.85))]
335
+ elif pattern == "3-lines-top":
336
+ positions = [(width // 2, int(height * 0.15)), (width // 2, int(height * 0.35)), (width // 2, int(height * 0.55))]
337
+ elif pattern == "3-lines-center":
338
+ positions = [(width // 2, int(height * 0.25)), (width // 2, int(height * 0.50)), (width // 2, int(height * 0.75))]
339
+ elif pattern == "3-lines-bottom":
340
+ positions = [(width // 2, int(height * 0.45)), (width // 2, int(height * 0.65)), (width // 2, int(height * 0.85))]
341
+
342
+ # Draw white rectangles to represent text boxes
343
+ try:
344
+ font = ImageFont.load_default()
345
+ except:
346
+ font = None
347
+
348
+ for i, (x, y) in enumerate(positions):
349
+ # Draw white rectangle as text placeholder
350
+ box_width = 60
351
+ box_height = 12
352
+ left = x - box_width // 2
353
+ top = y - box_height // 2
354
+ right = x + box_width // 2
355
+ bottom = y + box_height // 2
356
+
357
+ draw.rectangle([left, top, right, bottom], fill='white', outline='white')
358
+
359
+ # Add small text label
360
+ if font:
361
+ try:
362
+ label = f"Line {i+1}"
363
+ draw.text((x, y), label, fill='black', font=font, anchor="mm")
364
+ except TypeError:
365
+ # Fallback for older PIL
366
+ draw.text((x-15, y-5), f"L{i+1}", fill='black', font=font)
367
+
368
+ return img
369
+
370
+ # Function to convert PIL image to base64 for Gradio
371
+ def pil_to_base64(img):
372
+ """Convert PIL image to base64 string for Gradio"""
373
+ buffer = BytesIO()
374
+ img.save(buffer, format='PNG')
375
+ img_str = base64.b64encode(buffer.getvalue()).decode()
376
+ return f"data:image/png;base64,{img_str}"
377
+
378
+ # Create all template images
379
+ pattern_templates = {}
380
+ pattern_names = ["2-lines-top", "2-lines-center", "2-lines-bottom", "3-lines-top", "3-lines-center", "3-lines-bottom"]
381
+
382
+ for pattern in pattern_names:
383
+ template_img = create_pattern_template(pattern)
384
+ pattern_templates[pattern] = template_img
385
+
386
+ # Function to update template display
387
+ def update_pattern_template(selected_pattern):
388
+ """Return the template image for the selected pattern"""
389
+ if selected_pattern in pattern_templates:
390
+ return pattern_templates[selected_pattern]
391
+ else:
392
+ # Return default template
393
+ return create_pattern_template("2-lines-top")
394
+
395
  # Create the Gradio Interface
396
  with gr.Blocks(title="AI Image Generator & Text Overlay") as demo:
397
  gr.Markdown("# 🎨 AI Image Generator & Text Overlay")
 
479
 
480
  with gr.Row():
481
  with gr.Column():
482
+ # Text pattern selection with template preview
483
+ with gr.Row():
484
+ with gr.Column(scale=2):
485
+ pattern = gr.Dropdown(
486
+ choices=[
487
+ ("2-lines-top", "πŸ“„ 2 Lines - Top (Header style)"),
488
+ ("2-lines-center", "πŸ“„ 2 Lines - Center (Title style)"),
489
+ ("2-lines-bottom", "πŸ“„ 2 Lines - Bottom (Caption style)"),
490
+ ("3-lines-top", "πŸ“‹ 3 Lines - Top (Header + subtitle)"),
491
+ ("3-lines-center", "πŸ“‹ 3 Lines - Center (Full title)"),
492
+ ("3-lines-bottom", "πŸ“‹ 3 Lines - Bottom (Credits style)")
493
+ ],
494
+ value="2-lines-top",
495
+ label="Layout Pattern"
496
+ )
497
+
498
+ with gr.Column(scale=1):
499
+ # Template preview image
500
+ template_preview = gr.Image(
501
+ label="Template Preview",
502
+ value=pattern_templates["2-lines-top"],
503
+ show_label=True,
504
+ interactive=False,
505
+ width=200,
506
+ height=120
507
+ )
508
 
509
  # Text input fields
510
  line1_inp = gr.Textbox(
 
534
  value="#FFFFFF"
535
  )
536
 
537
+ # Add outline option
538
  add_outline = gr.Checkbox(
539
  label="Add black outline",
540
  value=True,
 
547
  # Output
548
  text_image = gr.Image(label="Image with Text Overlay")
549
 
550
+ # Event handlers for Tab 2
551
+
552
+ # Update template when pattern changes
553
+ pattern.change(
554
+ update_pattern_template,
555
+ inputs=[pattern],
556
+ outputs=[template_preview]
557
+ )
558
+
559
+ # Add text to image
560
  add_text_btn.click(
561
  add_text_to_image,
562
  inputs=[current_image, pattern, line1_inp, line2_inp, line3_inp, font_size, color, add_outline],