Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -685,125 +685,6 @@ def add_text_to_image(
|
|
685 |
print("Text Over Image process completed") # ๋๋ฒ๊น
|
686 |
return result.convert('RGB')
|
687 |
|
688 |
-
except Exception as e:
|
689 |
-
print(f"Error in add_text_to_image: {str(e)}")
|
690 |
-
traceback.print_exc()
|
691 |
-
return input_imagedef add_text_to_image(
|
692 |
-
input_image,
|
693 |
-
text,
|
694 |
-
font_size,
|
695 |
-
color,
|
696 |
-
opacity,
|
697 |
-
x_position,
|
698 |
-
y_position,
|
699 |
-
thickness,
|
700 |
-
text_position_type,
|
701 |
-
font_choice
|
702 |
-
):
|
703 |
-
"""
|
704 |
-
Add text to an image with customizable properties
|
705 |
-
"""
|
706 |
-
try:
|
707 |
-
if input_image is None or text.strip() == "":
|
708 |
-
return input_image
|
709 |
-
|
710 |
-
# PIL Image ๊ฐ์ฒด๋ก ๋ณํ
|
711 |
-
if isinstance(input_image, np.ndarray):
|
712 |
-
image = Image.fromarray(input_image)
|
713 |
-
elif isinstance(input_image, Image.Image):
|
714 |
-
image = input_image.copy()
|
715 |
-
else:
|
716 |
-
print(f"Unexpected image type: {type(input_image)}")
|
717 |
-
return input_image
|
718 |
-
|
719 |
-
# ์ด๋ฏธ์ง๋ฅผ RGBA ๋ชจ๋๋ก ๋ณํ
|
720 |
-
image = image.convert('RGBA')
|
721 |
-
width, height = image.size
|
722 |
-
|
723 |
-
# ํฐํธ ์ค์
|
724 |
-
try:
|
725 |
-
font_path = {
|
726 |
-
"Default": "DejaVuSans.ttf",
|
727 |
-
"Korean Regular": "ko-Regular.ttf",
|
728 |
-
"Korean Son": "ko-son.ttf"
|
729 |
-
}.get(font_choice, "DejaVuSans.ttf")
|
730 |
-
font = ImageFont.truetype(font_path, int(font_size))
|
731 |
-
except Exception as e:
|
732 |
-
print(f"Font error: {str(e)}, using default")
|
733 |
-
font = ImageFont.load_default()
|
734 |
-
|
735 |
-
# ์์ ์ค์
|
736 |
-
rgb_color = {
|
737 |
-
'White': (255, 255, 255),
|
738 |
-
'Black': (0, 0, 0),
|
739 |
-
'Red': (255, 0, 0),
|
740 |
-
'Green': (0, 255, 0),
|
741 |
-
'Blue': (0, 0, 255),
|
742 |
-
'Yellow': (255, 255, 0),
|
743 |
-
'Purple': (128, 0, 128)
|
744 |
-
}.get(color, (255, 255, 255))
|
745 |
-
|
746 |
-
# ํ
์คํธ ํฌ๊ธฐ ๊ณ์ฐ
|
747 |
-
temp_draw = ImageDraw.Draw(Image.new('RGBA', (1, 1)))
|
748 |
-
text_bbox = temp_draw.textbbox((0, 0), text, font=font)
|
749 |
-
text_width = text_bbox[2] - text_bbox[0]
|
750 |
-
text_height = text_bbox[3] - text_bbox[1]
|
751 |
-
|
752 |
-
# ํ
์คํธ ์์น ๊ณ์ฐ
|
753 |
-
actual_x = int((width - text_width) * (x_position / 100))
|
754 |
-
actual_y = int((height - text_height) * (y_position / 100))
|
755 |
-
|
756 |
-
# ํ
์คํธ ์์ ์ค์
|
757 |
-
text_color = (*rgb_color, int(opacity))
|
758 |
-
|
759 |
-
print(f"Processing {text_position_type}") # ๋๋ฒ๊น
|
760 |
-
|
761 |
-
if text_position_type == "Text Behind Image":
|
762 |
-
print("Starting Text Behind Image process") # ๋๋ฒ๊น
|
763 |
-
|
764 |
-
# 1. ํฐ์ ๋ฐฐ๊ฒฝ ์์ฑ
|
765 |
-
background = Image.new('RGBA', (width, height), (255, 255, 255, 255))
|
766 |
-
|
767 |
-
# 2. ํ
์คํธ ๋ ์ด์ด ์์ฑ
|
768 |
-
text_layer = Image.new('RGBA', (width, height), (0, 0, 0, 0))
|
769 |
-
text_draw = ImageDraw.Draw(text_layer)
|
770 |
-
|
771 |
-
# 3. ํ
์คํธ ๊ทธ๋ฆฌ๊ธฐ
|
772 |
-
text_draw.text(
|
773 |
-
(actual_x, actual_y),
|
774 |
-
text,
|
775 |
-
font=font,
|
776 |
-
fill=text_color
|
777 |
-
)
|
778 |
-
|
779 |
-
# 4. ๋ ์ด์ด ํฉ์ฑ
|
780 |
-
result = Image.alpha_composite(background, text_layer)
|
781 |
-
result = Image.alpha_composite(result, image)
|
782 |
-
|
783 |
-
print("Text Behind Image process completed") # ๋๋ฒ๊น
|
784 |
-
return result.convert('RGB')
|
785 |
-
|
786 |
-
else: # Text Over Image
|
787 |
-
print("Starting Text Over Image process") # ๋๋ฒ๊น
|
788 |
-
|
789 |
-
# ํ
์คํธ ์ค๋ฒ๋ ์ด ์์ฑ
|
790 |
-
overlay = Image.new('RGBA', (width, height), (0, 0, 0, 0))
|
791 |
-
draw = ImageDraw.Draw(overlay)
|
792 |
-
|
793 |
-
# ํ
์คํธ ๊ทธ๋ฆฌ๊ธฐ
|
794 |
-
draw.text(
|
795 |
-
(actual_x, actual_y),
|
796 |
-
text,
|
797 |
-
font=font,
|
798 |
-
fill=text_color
|
799 |
-
)
|
800 |
-
|
801 |
-
# ์ด๋ฏธ์ง ํฉ์ฑ
|
802 |
-
result = Image.alpha_composite(image, overlay)
|
803 |
-
|
804 |
-
print("Text Over Image process completed") # ๋๋ฒ๊น
|
805 |
-
return result.convert('RGB')
|
806 |
-
|
807 |
except Exception as e:
|
808 |
print(f"Error in add_text_to_image: {str(e)}")
|
809 |
traceback.print_exc()
|
|
|
685 |
print("Text Over Image process completed") # ๋๋ฒ๊น
|
686 |
return result.convert('RGB')
|
687 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
688 |
except Exception as e:
|
689 |
print(f"Error in add_text_to_image: {str(e)}")
|
690 |
traceback.print_exc()
|