Spaces:
Paused
Paused
Update app.py
Browse files
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 |
-
|
|
|
|
|
634 |
text_width = text_bbox[2] - text_bbox[0]
|
635 |
text_height = text_bbox[3] - text_bbox[1]
|
636 |
|
637 |
-
|
638 |
-
|
|
|
639 |
|
640 |
# ํ
์คํธ ์์ ์ค์ (ํฌ๋ช
๋ ํฌํจ)
|
641 |
text_color = (*rgb_color, int(opacity))
|
642 |
|
643 |
if text_position_type == "Text Behind Image":
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
-
|
654 |
-
|
655 |
-
|
656 |
-
|
657 |
-
|
658 |
-
|
659 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
660 |
else:
|
661 |
# Text Over Image
|
662 |
-
|
663 |
-
|
664 |
-
|
665 |
-
|
666 |
-
|
667 |
-
|
668 |
-
|
669 |
-
|
670 |
-
|
671 |
-
|
672 |
-
|
673 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
674 |
|
675 |
except Exception as e:
|
676 |
-
print(f"
|
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 |
|