Spaces:
Sleeping
Sleeping
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()
|