aiqtech commited on
Commit
0a18d9b
ยท
verified ยท
1 Parent(s): 584e173

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -60
app.py CHANGED
@@ -578,57 +578,54 @@ def add_text_to_image(
578
  y_position,
579
  thickness,
580
  text_position_type,
581
- font_choice # ์ƒˆ๋กœ์šด ํŒŒ๋ผ๋ฏธํ„ฐ ์ถ”๊ฐ€
582
  ):
583
  """
584
  Add text to an image with customizable properties
585
  """
586
  try:
587
- if input_image is None:
588
- return None
589
-
590
  # PIL Image ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜
591
- if not isinstance(input_image, Image.Image):
592
- if isinstance(input_image, np.ndarray):
593
- image = Image.fromarray(input_image)
594
- else:
595
- raise ValueError("Unsupported image type")
596
- else:
597
  image = input_image.copy()
 
 
 
598
 
599
  # ์ด๋ฏธ์ง€๋ฅผ RGBA ๋ชจ๋“œ๋กœ ๋ณ€ํ™˜
600
- if image.mode != 'RGBA':
601
- image = image.convert('RGBA')
602
 
603
  # Text Behind Image ์ฒ˜๋ฆฌ
604
  if text_position_type == "Text Behind Image":
605
- # ์›๋ณธ ์ด๋ฏธ์ง€์˜ ๋ฐฐ๊ฒฝ ์ œ๊ฑฐ
606
- overlay_image = remove_background(image)
 
 
 
607
 
608
  # ํ…์ŠคํŠธ ์˜ค๋ฒ„๋ ˆ์ด ์ƒ์„ฑ
609
  txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
610
  draw = ImageDraw.Draw(txt_overlay)
611
-
612
  # ํฐํŠธ ์„ค์ •
613
- font_files = {
614
- "Default": "DejaVuSans.ttf",
615
- "Korean Regular": "ko-Regular.ttf",
616
- "Korean Son": "ko-son.ttf"
617
- }
618
-
619
  try:
620
- font_file = font_files.get(font_choice, "DejaVuSans.ttf")
621
- font = ImageFont.truetype(font_file, int(font_size))
 
 
 
 
 
622
  except Exception as e:
623
- print(f"Font loading error ({font_choice}): {str(e)}")
624
- try:
625
- font = ImageFont.truetype("arial.ttf", int(font_size))
626
- except:
627
- print("Using default font")
628
- font = ImageFont.load_default()
629
-
630
  # ์ƒ‰์ƒ ์„ค์ •
631
- color_map = {
632
  'White': (255, 255, 255),
633
  'Black': (0, 0, 0),
634
  'Red': (255, 0, 0),
@@ -636,49 +633,48 @@ def add_text_to_image(
636
  'Blue': (0, 0, 255),
637
  'Yellow': (255, 255, 0),
638
  'Purple': (128, 0, 128)
639
- }
640
- rgb_color = color_map.get(color, (255, 255, 255))
641
-
642
- # ํ…์ŠคํŠธ ํฌ๊ธฐ ๊ณ„์‚ฐ
643
  text_bbox = draw.textbbox((0, 0), text, font=font)
644
  text_width = text_bbox[2] - text_bbox[0]
645
  text_height = text_bbox[3] - text_bbox[1]
646
 
647
- # ์œ„์น˜ ๊ณ„์‚ฐ
648
  actual_x = int((image.width - text_width) * (x_position / 100))
649
  actual_y = int((image.height - text_height) * (y_position / 100))
650
 
651
- # ํ…์ŠคํŠธ ์ƒ‰์ƒ ์„ค์ •
652
  text_color = (*rgb_color, int(opacity))
653
-
654
- # ํ…์ŠคํŠธ ๊ทธ๋ฆฌ๊ธฐ
655
- add_text_with_stroke(
656
- draw,
657
- text,
658
- actual_x,
659
- actual_y,
660
- font,
661
- text_color,
662
- int(thickness)
663
- )
664
 
 
 
 
 
 
 
 
 
 
 
 
665
  if text_position_type == "Text Behind Image":
666
- # ํ…์ŠคํŠธ๋ฅผ ๋จผ์ € ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์œ„์— ์ด๋ฏธ์ง€ ์˜ค๋ฒ„๋ ˆ์ด
667
- output_image = Image.alpha_composite(image, txt_overlay)
668
- output_image = superimpose(output_image, overlay_image)
 
669
  else:
670
- # ๊ธฐ์กด ๋ฐฉ์‹๋Œ€๋กœ ํ…์ŠคํŠธ๋ฅผ ์ด๋ฏธ์ง€ ์œ„์— ๊ทธ๋ฆฌ๊ธฐ
671
  output_image = Image.alpha_composite(image, txt_overlay)
672
-
673
- # RGB๋กœ ๋ณ€ํ™˜
674
- output_image = output_image.convert('RGB')
675
-
676
- return output_image
677
 
678
  except Exception as e:
679
- print(f"Error in add_text_to_image: {str(e)}")
680
  return input_image
681
 
 
 
682
 
683
  def update_position(new_position):
684
  """์œ„์น˜ ์—…๋ฐ์ดํŠธ ํ•จ์ˆ˜"""
@@ -914,10 +910,13 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
914
  queue=True
915
  )
916
 
 
 
 
917
  add_text_btn.click(
918
  fn=add_text_to_image,
919
  inputs=[
920
- combined_image,
921
  text_input,
922
  font_size,
923
  color_dropdown,
@@ -928,7 +927,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
928
  text_position_type,
929
  font_choice
930
  ],
931
- outputs=combined_image
 
932
  )
933
 
934
  demo.queue(max_size=5)
 
578
  y_position,
579
  thickness,
580
  text_position_type,
581
+ font_choice
582
  ):
583
  """
584
  Add text to an image with customizable properties
585
  """
586
  try:
587
+ if input_image is None or text.strip() == "":
588
+ return input_image
589
+
590
  # PIL Image ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜
591
+ if isinstance(input_image, np.ndarray):
592
+ image = Image.fromarray(input_image)
593
+ elif isinstance(input_image, Image.Image):
 
 
 
594
  image = input_image.copy()
595
+ else:
596
+ print(f"Unexpected image type: {type(input_image)}")
597
+ return input_image
598
 
599
  # ์ด๋ฏธ์ง€๋ฅผ RGBA ๋ชจ๋“œ๋กœ ๋ณ€ํ™˜
600
+ image = image.convert('RGBA')
 
601
 
602
  # Text Behind Image ์ฒ˜๋ฆฌ
603
  if text_position_type == "Text Behind Image":
604
+ try:
605
+ overlay_image = remove_background(image)
606
+ except Exception as e:
607
+ print(f"Background removal failed: {str(e)}")
608
+ overlay_image = image
609
 
610
  # ํ…์ŠคํŠธ ์˜ค๋ฒ„๋ ˆ์ด ์ƒ์„ฑ
611
  txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
612
  draw = ImageDraw.Draw(txt_overlay)
613
+
614
  # ํฐํŠธ ์„ค์ •
 
 
 
 
 
 
615
  try:
616
+ font_path = {
617
+ "Default": "DejaVuSans.ttf",
618
+ "Korean Regular": "ko-Regular.ttf",
619
+ "Korean Son": "ko-son.ttf"
620
+ }.get(font_choice, "DejaVuSans.ttf")
621
+
622
+ font = ImageFont.truetype(font_path, int(font_size))
623
  except Exception as e:
624
+ print(f"Font error: {str(e)}, using default")
625
+ font = ImageFont.load_default()
626
+
 
 
 
 
627
  # ์ƒ‰์ƒ ์„ค์ •
628
+ rgb_color = {
629
  'White': (255, 255, 255),
630
  'Black': (0, 0, 0),
631
  'Red': (255, 0, 0),
 
633
  'Blue': (0, 0, 255),
634
  'Yellow': (255, 255, 0),
635
  'Purple': (128, 0, 128)
636
+ }.get(color, (255, 255, 255))
637
+
638
+ # ํ…์ŠคํŠธ ์œ„์น˜ ๊ณ„์‚ฐ
 
639
  text_bbox = draw.textbbox((0, 0), text, font=font)
640
  text_width = text_bbox[2] - text_bbox[0]
641
  text_height = text_bbox[3] - text_bbox[1]
642
 
 
643
  actual_x = int((image.width - text_width) * (x_position / 100))
644
  actual_y = int((image.height - text_height) * (y_position / 100))
645
 
646
+ # ํ…์ŠคํŠธ ์ƒ‰์ƒ ์„ค์ • (ํˆฌ๋ช…๋„ ํฌํ•จ)
647
  text_color = (*rgb_color, int(opacity))
 
 
 
 
 
 
 
 
 
 
 
648
 
649
+ # ํ…์ŠคํŠธ ๊ทธ๋ฆฌ๊ธฐ (์™ธ๊ณฝ์„  ํฌํ•จ)
650
+ for dx in range(-thickness, thickness + 1):
651
+ for dy in range(-thickness, thickness + 1):
652
+ draw.text(
653
+ (actual_x + dx, actual_y + dy),
654
+ text,
655
+ font=font,
656
+ fill=text_color
657
+ )
658
+
659
+ # ์ตœ์ข… ์ด๋ฏธ์ง€ ํ•ฉ์„ฑ
660
  if text_position_type == "Text Behind Image":
661
+ # ํ…์ŠคํŠธ ๋จผ์ € ๊ทธ๋ฆฌ๊ณ  ์ด๋ฏธ์ง€ ์˜ค๋ฒ„๋ ˆ์ด
662
+ base = Image.new('RGBA', image.size, (255, 255, 255, 0))
663
+ base = Image.alpha_composite(base, txt_overlay)
664
+ output_image = Image.alpha_composite(base, overlay_image)
665
  else:
666
+ # ์ด๏ฟฝ๏ฟฝ๏ฟฝ์ง€ ์œ„์— ํ…์ŠคํŠธ ์˜ค๋ฒ„๋ ˆ์ด
667
  output_image = Image.alpha_composite(image, txt_overlay)
668
+
669
+ # RGB๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜
670
+ return output_image.convert('RGB')
 
 
671
 
672
  except Exception as e:
673
+ print(f"Text addition error: {str(e)}")
674
  return input_image
675
 
676
+
677
+
678
 
679
  def update_position(new_position):
680
  """์œ„์น˜ ์—…๋ฐ์ดํŠธ ํ•จ์ˆ˜"""
 
910
  queue=True
911
  )
912
 
913
+
914
+
915
+ # ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์ˆ˜์ •
916
  add_text_btn.click(
917
  fn=add_text_to_image,
918
  inputs=[
919
+ combined_image, # ํ˜„์žฌ ํ‘œ์‹œ๋œ ์ด๋ฏธ์ง€
920
  text_input,
921
  font_size,
922
  color_dropdown,
 
927
  text_position_type,
928
  font_choice
929
  ],
930
+ outputs=combined_image,
931
+ show_progress=True
932
  )
933
 
934
  demo.queue(max_size=5)