aiqtech commited on
Commit
4fc817b
·
verified ·
1 Parent(s): c72485e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -16
app.py CHANGED
@@ -25,7 +25,7 @@ from PIL import Image, ImageDraw, ImageFont
25
  from PIL import Image
26
  from gradio_client import Client, handle_file
27
  import uuid
28
-
29
 
30
  def clear_memory():
31
  """메모리 정리 함수"""
@@ -568,6 +568,8 @@ def superimpose(image_with_text, overlay_image):
568
  # image_with_text.save("output_image.png")
569
  return image_with_text
570
 
 
 
571
  def add_text_to_image(
572
  input_image,
573
  text,
@@ -599,6 +601,10 @@ def add_text_to_image(
599
  # 이미지를 RGBA 모드로 변환
600
  image = image.convert('RGBA')
601
 
 
 
 
 
602
  # 폰트 설정
603
  try:
604
  font_path = {
@@ -635,31 +641,24 @@ def add_text_to_image(
635
  text_color = (*rgb_color, int(opacity))
636
 
637
  if text_position_type == "Text Behind Image":
638
- # 텍스트 레이어 생성
639
- text_layer = Image.new('RGBA', image.size, (255, 255, 255, 0))
640
- text_draw = ImageDraw.Draw(text_layer)
641
 
642
  # 텍스트 그리기
643
  for dx in range(-thickness, thickness + 1):
644
  for dy in range(-thickness, thickness + 1):
645
- text_draw.text(
646
  (actual_x + dx, actual_y + dy),
647
  text,
648
  font=font,
649
  fill=text_color
650
  )
651
 
652
- # 최종 이미지 합성
653
- output_image = Image.new('RGBA', image.size, (255, 255, 255, 255)) # 흰색 배경
654
- output_image = Image.alpha_composite(output_image, text_layer) # 텍스트 추가
655
- output_image = Image.alpha_composite(output_image, image) # 원본 이미지 추가
656
-
657
  else:
658
  # Text Over Image
659
- txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
660
- draw = ImageDraw.Draw(txt_overlay)
661
-
662
- # 텍스트 그리기
663
  for dx in range(-thickness, thickness + 1):
664
  for dy in range(-thickness, thickness + 1):
665
  draw.text(
@@ -668,7 +667,6 @@ def add_text_to_image(
668
  font=font,
669
  fill=text_color
670
  )
671
-
672
  output_image = Image.alpha_composite(image, txt_overlay)
673
 
674
  # RGB로 변환하여 반환
@@ -676,7 +674,7 @@ def add_text_to_image(
676
 
677
  except Exception as e:
678
  print(f"Text addition error: {str(e)}")
679
- traceback.print_exc() # 상세한 에러 정보 출력
680
  return input_image
681
 
682
 
 
25
  from PIL import Image
26
  from gradio_client import Client, handle_file
27
  import uuid
28
+ import traceback # 상단에 추가
29
 
30
  def clear_memory():
31
  """메모리 정리 함수"""
 
568
  # image_with_text.save("output_image.png")
569
  return image_with_text
570
 
571
+
572
+
573
  def add_text_to_image(
574
  input_image,
575
  text,
 
601
  # 이미지를 RGBA 모드로 변환
602
  image = image.convert('RGBA')
603
 
604
+ # 텍스트 레이어 생성
605
+ txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
606
+ draw = ImageDraw.Draw(txt_overlay)
607
+
608
  # 폰트 설정
609
  try:
610
  font_path = {
 
641
  text_color = (*rgb_color, int(opacity))
642
 
643
  if text_position_type == "Text Behind Image":
644
+ # 배경 레이어 생성
645
+ background = Image.new('RGBA', image.size, (255, 255, 255, 255))
 
646
 
647
  # 텍스트 그리기
648
  for dx in range(-thickness, thickness + 1):
649
  for dy in range(-thickness, thickness + 1):
650
+ draw.text(
651
  (actual_x + dx, actual_y + dy),
652
  text,
653
  font=font,
654
  fill=text_color
655
  )
656
 
657
+ # 레이어 합성
658
+ output_image = Image.alpha_composite(background, txt_overlay)
659
+ output_image = Image.alpha_composite(output_image, image)
 
 
660
  else:
661
  # Text Over Image
 
 
 
 
662
  for dx in range(-thickness, thickness + 1):
663
  for dy in range(-thickness, thickness + 1):
664
  draw.text(
 
667
  font=font,
668
  fill=text_color
669
  )
 
670
  output_image = Image.alpha_composite(image, txt_overlay)
671
 
672
  # RGB로 변환하여 반환
 
674
 
675
  except Exception as e:
676
  print(f"Text addition error: {str(e)}")
677
+ traceback.print_exc()
678
  return input_image
679
 
680