aiqtech commited on
Commit
0205c92
·
verified ·
1 Parent(s): 807c7ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -34
app.py CHANGED
@@ -578,11 +578,8 @@ 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
@@ -600,11 +597,6 @@ def add_text_to_image(
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)
@@ -612,8 +604,7 @@ def add_text_to_image(
612
  # 폰트 설정
613
  font_files = {
614
  "Default": "DejaVuSans.ttf",
615
- "Korean Regular": "ko-Regular.ttf",
616
- "Korean Son": "ko-son.ttf"
617
  }
618
 
619
  try:
@@ -621,11 +612,7 @@ def add_text_to_image(
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 = {
@@ -651,23 +638,36 @@ def add_text_to_image(
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로 변환
@@ -793,11 +793,14 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
793
  with gr.Row():
794
  with gr.Column(scale=1):
795
  font_choice = gr.Dropdown(
796
- choices=["Default", "Korean Regular", "Korean Son"],
 
797
  value="Default",
798
  label="Font Selection",
799
  interactive=True
800
  )
 
 
801
  font_size = gr.Slider(
802
  minimum=10,
803
  maximum=200,
@@ -830,14 +833,14 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
830
  maximum=100,
831
  value=50,
832
  step=1,
833
- label="X Position (%)"
834
  )
835
  y_position = gr.Slider(
836
  minimum=0,
837
  maximum=100,
838
  value=50,
839
  step=1,
840
- label="Y Position (%)"
841
  )
842
  add_text_btn = gr.Button("Apply Text", variant="primary")
843
 
 
578
  y_position,
579
  thickness,
580
  text_position_type,
581
+ font_choice
582
  ):
 
 
 
583
  try:
584
  if input_image is None:
585
  return None
 
597
  if image.mode != 'RGBA':
598
  image = image.convert('RGBA')
599
 
 
 
 
 
 
600
  # 텍스트 오버레이 생성
601
  txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
602
  draw = ImageDraw.Draw(txt_overlay)
 
604
  # 폰트 설정
605
  font_files = {
606
  "Default": "DejaVuSans.ttf",
607
+ "Korean Regular": "ko-Regular.ttf"
 
608
  }
609
 
610
  try:
 
612
  font = ImageFont.truetype(font_file, int(font_size))
613
  except Exception as e:
614
  print(f"Font loading error ({font_choice}): {str(e)}")
615
+ font = ImageFont.load_default()
 
 
 
 
616
 
617
  # 색상 설정
618
  color_map = {
 
638
  # 텍스트 색상 설정
639
  text_color = (*rgb_color, int(opacity))
640
 
 
 
 
 
 
 
 
 
 
 
 
641
  if text_position_type == "Text Behind Image":
642
+ # 배경 제거된 이미지 생성
643
+ foreground = remove_background(image)
644
+
645
+ # 텍스트를 먼저 그리기
646
+ background = Image.new('RGBA', image.size, (255, 255, 255, 0))
647
+ draw_bg = ImageDraw.Draw(background)
648
+ add_text_with_stroke(
649
+ draw_bg,
650
+ text,
651
+ actual_x,
652
+ actual_y,
653
+ font,
654
+ text_color,
655
+ int(thickness)
656
+ )
657
+
658
+ # 텍스트 위에 배경 제거된 이미지 합성
659
+ output_image = Image.alpha_composite(background, foreground)
660
  else:
661
+ # 텍스트를 이미지 위에 그리기
662
+ add_text_with_stroke(
663
+ draw,
664
+ text,
665
+ actual_x,
666
+ actual_y,
667
+ font,
668
+ text_color,
669
+ int(thickness)
670
+ )
671
  output_image = Image.alpha_composite(image, txt_overlay)
672
 
673
  # RGB로 변환
 
793
  with gr.Row():
794
  with gr.Column(scale=1):
795
  font_choice = gr.Dropdown(
796
+ choices=["Default", "Korean Regular"], # "Korean Son" 제거
797
+
798
  value="Default",
799
  label="Font Selection",
800
  interactive=True
801
  )
802
+
803
+
804
  font_size = gr.Slider(
805
  minimum=10,
806
  maximum=200,
 
833
  maximum=100,
834
  value=50,
835
  step=1,
836
+ label="Left(0%)~Right(100%)"
837
  )
838
  y_position = gr.Slider(
839
  minimum=0,
840
  maximum=100,
841
  value=50,
842
  step=1,
843
+ label="High(0%)~Low(100%)"
844
  )
845
  add_text_btn = gr.Button("Apply Text", variant="primary")
846