aiqtech commited on
Commit
e8da47e
ยท
verified ยท
1 Parent(s): 4fc817b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -38
app.py CHANGED
@@ -569,7 +569,6 @@ def superimpose(image_with_text, overlay_image):
569
  return image_with_text
570
 
571
 
572
-
573
  def add_text_to_image(
574
  input_image,
575
  text,
@@ -600,10 +599,7 @@ def add_text_to_image(
600
 
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:
@@ -629,51 +625,76 @@ def add_text_to_image(
629
  'Purple': (128, 0, 128)
630
  }.get(color, (255, 255, 255))
631
 
632
- # ํ…์ŠคํŠธ ์œ„์น˜ ๊ณ„์‚ฐ
633
- text_bbox = draw.textbbox((0, 0), text, font=font)
 
 
634
  text_width = text_bbox[2] - text_bbox[0]
635
  text_height = text_bbox[3] - text_bbox[1]
636
 
637
- actual_x = int((image.width - text_width) * (x_position / 100))
638
- actual_y = int((image.height - text_height) * (y_position / 100))
 
639
 
640
  # ํ…์ŠคํŠธ ์ƒ‰์ƒ ์„ค์ • (ํˆฌ๋ช…๋„ ํฌํ•จ)
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(
665
- (actual_x + dx, actual_y + dy),
666
- text,
667
- font=font,
668
- fill=text_color
669
- )
670
- output_image = Image.alpha_composite(image, txt_overlay)
671
-
672
- # RGB๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜
673
- return output_image.convert('RGB')
 
 
 
 
 
 
 
 
 
674
 
675
  except Exception as e:
676
- print(f"Text addition error: {str(e)}")
677
  traceback.print_exc()
678
  return input_image
679
 
 
569
  return image_with_text
570
 
571
 
 
572
  def add_text_to_image(
573
  input_image,
574
  text,
 
599
 
600
  # ์ด๋ฏธ์ง€๋ฅผ RGBA ๋ชจ๋“œ๋กœ ๋ณ€ํ™˜
601
  image = image.convert('RGBA')
602
+ width, height = image.size
 
 
 
603
 
604
  # ํฐํŠธ ์„ค์ •
605
  try:
 
625
  'Purple': (128, 0, 128)
626
  }.get(color, (255, 255, 255))
627
 
628
+ # ์ž„์‹œ ๋“œ๋กœ์ž‰ ์ปจํ…์ŠคํŠธ๋กœ ํ…์ŠคํŠธ ํฌ๊ธฐ ๊ณ„์‚ฐ
629
+ temp_img = Image.new('RGBA', (1, 1), (0, 0, 0, 0))
630
+ temp_draw = ImageDraw.Draw(temp_img)
631
+ text_bbox = temp_draw.textbbox((0, 0), text, font=font)
632
  text_width = text_bbox[2] - text_bbox[0]
633
  text_height = text_bbox[3] - text_bbox[1]
634
 
635
+ # ํ…์ŠคํŠธ ์œ„์น˜ ๊ณ„์‚ฐ
636
+ actual_x = int((width - text_width) * (x_position / 100))
637
+ actual_y = int((height - text_height) * (y_position / 100))
638
 
639
  # ํ…์ŠคํŠธ ์ƒ‰์ƒ ์„ค์ • (ํˆฌ๋ช…๋„ ํฌํ•จ)
640
  text_color = (*rgb_color, int(opacity))
641
 
642
  if text_position_type == "Text Behind Image":
643
+ try:
644
+ # 1. ๋ฐฐ๊ฒฝ ๋ ˆ์ด์–ด (ํฐ์ƒ‰)
645
+ background = Image.new('RGBA', (width, height), (255, 255, 255, 255))
646
+
647
+ # 2. ํ…์ŠคํŠธ ๋ ˆ์ด์–ด
648
+ text_layer = Image.new('RGBA', (width, height), (0, 0, 0, 0))
649
+ text_draw = ImageDraw.Draw(text_layer)
650
+
651
+ # ํ…์ŠคํŠธ ๊ทธ๋ฆฌ๊ธฐ (์™ธ๊ณฝ์„  ํฌํ•จ)
652
+ for dx in range(-thickness, thickness + 1):
653
+ for dy in range(-thickness, thickness + 1):
654
+ text_draw.text(
655
+ (actual_x + dx, actual_y + dy),
656
+ text,
657
+ font=font,
658
+ fill=text_color
659
+ )
660
+
661
+ # 3. ๋ ˆ์ด์–ด ํ•ฉ์„ฑ
662
+ result = Image.alpha_composite(background, text_layer)
663
+ result = Image.alpha_composite(result, image)
664
+
665
+ print("Text Behind Image processing completed") # ๋””๋ฒ„๊น…์šฉ
666
+ return result.convert('RGB')
667
+
668
+ except Exception as e:
669
+ print(f"Text Behind Image error: {str(e)}")
670
+ traceback.print_exc()
671
+ return input_image
672
  else:
673
  # Text Over Image
674
+ try:
675
+ overlay = Image.new('RGBA', (width, height), (0, 0, 0, 0))
676
+ draw = ImageDraw.Draw(overlay)
677
+
678
+ # ํ…์ŠคํŠธ ๊ทธ๋ฆฌ๊ธฐ
679
+ for dx in range(-thickness, thickness + 1):
680
+ for dy in range(-thickness, thickness + 1):
681
+ draw.text(
682
+ (actual_x + dx, actual_y + dy),
683
+ text,
684
+ font=font,
685
+ fill=text_color
686
+ )
687
+
688
+ result = Image.alpha_composite(image, overlay)
689
+ return result.convert('RGB')
690
+
691
+ except Exception as e:
692
+ print(f"Text Over Image error: {str(e)}")
693
+ traceback.print_exc()
694
+ return input_image
695
 
696
  except Exception as e:
697
+ print(f"General text addition error: {str(e)}")
698
  traceback.print_exc()
699
  return input_image
700