aiqtech commited on
Commit
1bb836a
·
verified ·
1 Parent(s): 1fd13a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -34
app.py CHANGED
@@ -581,9 +581,9 @@ def add_text_to_image(
581
  font_choice
582
  ):
583
  try:
584
- if input_image is None:
585
- return None
586
-
587
  # PIL Image 객체로 변환
588
  if not isinstance(input_image, Image.Image):
589
  if isinstance(input_image, np.ndarray):
@@ -622,8 +622,9 @@ def add_text_to_image(
622
  }
623
  rgb_color = color_map.get(color, (255, 255, 255))
624
 
625
- # 텍스트 크기 계산
626
- text_bbox = draw.textbbox((0, 0), text, font=font)
 
627
  text_width = text_bbox[2] - text_bbox[0]
628
  text_height = text_bbox[3] - text_bbox[1]
629
 
@@ -635,32 +636,36 @@ def add_text_to_image(
635
  text_color = (*rgb_color, int(opacity))
636
 
637
  if text_position_type == "Text Behind Image":
638
- # 원본 이미지에서 전경 객체만 추출
639
- foreground = remove_background(image)
640
-
641
- # 배경 이미지 생성 (원본 이미지 복사)
642
- background = image.copy()
643
-
644
- # 텍스트를 그릴 임시 레이어 생성
645
- text_layer = Image.new('RGBA', image.size, (255, 255, 255, 0))
646
- draw_text = ImageDraw.Draw(text_layer)
647
-
648
- # 텍스트 그리기
649
- add_text_with_stroke(
650
- draw_text,
651
- text,
652
- actual_x,
653
- actual_y,
654
- font,
655
- text_color,
656
- int(thickness)
657
- )
658
-
659
- # 배경에 텍스트 합성
660
- background = Image.alpha_composite(background, text_layer)
661
-
662
- # 텍스트가 있는 배경 위에 전경 객체 합성
663
- output_image = Image.alpha_composite(background, foreground)
 
 
 
 
664
  else:
665
  # 텍스트 오버레이 생성
666
  txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
@@ -925,11 +930,12 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
925
  queue=True
926
  )
927
 
 
928
  add_text_btn.click(
929
  fn=add_text_to_image,
930
  inputs=[
931
- combined_image,
932
- text_input,
933
  font_size,
934
  color_dropdown,
935
  opacity_slider,
@@ -939,8 +945,11 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
939
  text_position_type,
940
  font_choice
941
  ],
942
- outputs=combined_image
 
943
  )
 
 
944
 
945
  demo.queue(max_size=5)
946
  demo.launch(
 
581
  font_choice
582
  ):
583
  try:
584
+ if input_image is None or text.strip() == "":
585
+ return input_image
586
+
587
  # PIL Image 객체로 변환
588
  if not isinstance(input_image, Image.Image):
589
  if isinstance(input_image, np.ndarray):
 
622
  }
623
  rgb_color = color_map.get(color, (255, 255, 255))
624
 
625
+ # 임시 Draw 객체 생성하여 텍스트 크기 계산
626
+ temp_draw = ImageDraw.Draw(image)
627
+ text_bbox = temp_draw.textbbox((0, 0), text, font=font)
628
  text_width = text_bbox[2] - text_bbox[0]
629
  text_height = text_bbox[3] - text_bbox[1]
630
 
 
636
  text_color = (*rgb_color, int(opacity))
637
 
638
  if text_position_type == "Text Behind Image":
639
+ try:
640
+ # 원본 이미지에서 전경 객체만 추출
641
+ foreground = remove_background(image)
642
+
643
+ # 배경 이미지 생성 (원본 이미지 복사)
644
+ background = image.copy()
645
+
646
+ # 텍스트를 그릴 임시 레이어 생성
647
+ text_layer = Image.new('RGBA', image.size, (255, 255, 255, 0))
648
+ draw_text = ImageDraw.Draw(text_layer)
649
+
650
+ # 텍스트 그리기
651
+ add_text_with_stroke(
652
+ draw_text,
653
+ text,
654
+ actual_x,
655
+ actual_y,
656
+ font,
657
+ text_color,
658
+ int(thickness)
659
+ )
660
+
661
+ # 배경에 텍스트 합성
662
+ background = Image.alpha_composite(background, text_layer)
663
+
664
+ # 텍스트가 있는 배경 위에 전경 객체 합성
665
+ output_image = Image.alpha_composite(background, foreground)
666
+ except Exception as e:
667
+ print(f"Error in Text Behind Image processing: {str(e)}")
668
+ return input_image
669
  else:
670
  # 텍스트 오버레이 생성
671
  txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
 
930
  queue=True
931
  )
932
 
933
+ # 이벤트 바인딩 부분에서
934
  add_text_btn.click(
935
  fn=add_text_to_image,
936
  inputs=[
937
+ combined_image, # 첫 번째 인자로 이미지
938
+ text_input, # 두 번째 인자로 텍스트
939
  font_size,
940
  color_dropdown,
941
  opacity_slider,
 
945
  text_position_type,
946
  font_choice
947
  ],
948
+ outputs=combined_image,
949
+ api_name="add_text" # API 이름 추가
950
  )
951
+
952
+
953
 
954
  demo.queue(max_size=5)
955
  demo.launch(