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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -28
app.py CHANGED
@@ -596,17 +596,9 @@ def add_text_to_image(
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)
@@ -646,24 +638,42 @@ def add_text_to_image(
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๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜
@@ -673,8 +683,6 @@ def add_text_to_image(
673
  print(f"Text addition error: {str(e)}")
674
  return input_image
675
 
676
-
677
-
678
 
679
  def update_position(new_position):
680
  """์œ„์น˜ ์—…๋ฐ์ดํŠธ ํ•จ์ˆ˜"""
 
596
  print(f"Unexpected image type: {type(input_image)}")
597
  return input_image
598
 
599
+ # ์›๋ณธ ์ด๋ฏธ์ง€๋ฅผ RGBA ๋ชจ๋“œ๋กœ ๋ณ€ํ™˜
600
  image = image.convert('RGBA')
601
+
 
 
 
 
 
 
 
 
602
  # ํ…์ŠคํŠธ ์˜ค๋ฒ„๋ ˆ์ด ์ƒ์„ฑ
603
  txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
604
  draw = ImageDraw.Draw(txt_overlay)
 
638
  # ํ…์ŠคํŠธ ์ƒ‰์ƒ ์„ค์ • (ํˆฌ๋ช…๋„ ํฌํ•จ)
639
  text_color = (*rgb_color, int(opacity))
640
 
 
 
 
 
 
 
 
 
 
 
 
641
  if text_position_type == "Text Behind Image":
642
+ # 1. ์›๋ณธ ์ด๋ฏธ์ง€์—์„œ ํˆฌ๋ช…ํ•œ ๋ถ€๋ถ„ ์ถ”์ถœ
643
+ alpha_mask = image.split()[3] if len(image.split()) == 4 else None
644
+
645
+ # 2. ํ…์ŠคํŠธ๋ฅผ ๋จผ์ € ๊ทธ๋ฆฌ๊ธฐ
646
+ background = Image.new('RGBA', image.size, (255, 255, 255, 0))
647
+ text_layer = Image.new('RGBA', image.size, (255, 255, 255, 0))
648
+ text_draw = ImageDraw.Draw(text_layer)
649
+
650
+ # ํ…์ŠคํŠธ ๊ทธ๋ฆฌ๊ธฐ (์™ธ๊ณฝ์„  ํฌํ•จ)
651
+ for dx in range(-thickness, thickness + 1):
652
+ for dy in range(-thickness, thickness + 1):
653
+ text_draw.text(
654
+ (actual_x + dx, actual_y + dy),
655
+ text,
656
+ font=font,
657
+ fill=text_color
658
+ )
659
+
660
+ # 3. ์ด๋ฏธ์ง€ ํ•ฉ์„ฑ
661
+ if alpha_mask:
662
+ # ์›๋ณธ ์ด๋ฏธ์ง€์˜ ํˆฌ๋ช…ํ•˜์ง€ ์•Š์€ ๋ถ€๋ถ„๋งŒ ์œ ์ง€
663
+ output_image = Image.alpha_composite(text_layer, image)
664
+ else:
665
+ # ์•ŒํŒŒ ์ฑ„๋„์ด ์—†๋Š” ๊ฒฝ์šฐ
666
+ output_image = Image.alpha_composite(text_layer, image)
667
  else:
668
+ # Text Over Image - ๊ธฐ์กด ๋ฐฉ์‹๋Œ€๋กœ ์ฒ˜๋ฆฌ
669
+ for dx in range(-thickness, thickness + 1):
670
+ for dy in range(-thickness, thickness + 1):
671
+ draw.text(
672
+ (actual_x + dx, actual_y + dy),
673
+ text,
674
+ font=font,
675
+ fill=text_color
676
+ )
677
  output_image = Image.alpha_composite(image, txt_overlay)
678
 
679
  # RGB๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜
 
683
  print(f"Text addition error: {str(e)}")
684
  return input_image
685
 
 
 
686
 
687
  def update_position(new_position):
688
  """์œ„์น˜ ์—…๋ฐ์ดํŠธ ํ•จ์ˆ˜"""