Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -578,57 +578,54 @@ def add_text_to_image(
|
|
| 578 |
y_position,
|
| 579 |
thickness,
|
| 580 |
text_position_type,
|
| 581 |
-
font_choice
|
| 582 |
):
|
| 583 |
"""
|
| 584 |
Add text to an image with customizable properties
|
| 585 |
"""
|
| 586 |
try:
|
| 587 |
-
if input_image is None:
|
| 588 |
-
return
|
| 589 |
-
|
| 590 |
# PIL Image ๊ฐ์ฒด๋ก ๋ณํ
|
| 591 |
-
if
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
else:
|
| 595 |
-
raise ValueError("Unsupported image type")
|
| 596 |
-
else:
|
| 597 |
image = input_image.copy()
|
|
|
|
|
|
|
|
|
|
| 598 |
|
| 599 |
# ์ด๋ฏธ์ง๋ฅผ RGBA ๋ชจ๋๋ก ๋ณํ
|
| 600 |
-
|
| 601 |
-
image = image.convert('RGBA')
|
| 602 |
|
| 603 |
# Text Behind Image ์ฒ๋ฆฌ
|
| 604 |
if text_position_type == "Text Behind Image":
|
| 605 |
-
|
| 606 |
-
|
|
|
|
|
|
|
|
|
|
| 607 |
|
| 608 |
# ํ
์คํธ ์ค๋ฒ๋ ์ด ์์ฑ
|
| 609 |
txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
|
| 610 |
draw = ImageDraw.Draw(txt_overlay)
|
| 611 |
-
|
| 612 |
# ํฐํธ ์ค์
|
| 613 |
-
font_files = {
|
| 614 |
-
"Default": "DejaVuSans.ttf",
|
| 615 |
-
"Korean Regular": "ko-Regular.ttf",
|
| 616 |
-
"Korean Son": "ko-son.ttf"
|
| 617 |
-
}
|
| 618 |
-
|
| 619 |
try:
|
| 620 |
-
|
| 621 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
except Exception as e:
|
| 623 |
-
print(f"Font
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
except:
|
| 627 |
-
print("Using default font")
|
| 628 |
-
font = ImageFont.load_default()
|
| 629 |
-
|
| 630 |
# ์์ ์ค์
|
| 631 |
-
|
| 632 |
'White': (255, 255, 255),
|
| 633 |
'Black': (0, 0, 0),
|
| 634 |
'Red': (255, 0, 0),
|
|
@@ -636,49 +633,48 @@ def add_text_to_image(
|
|
| 636 |
'Blue': (0, 0, 255),
|
| 637 |
'Yellow': (255, 255, 0),
|
| 638 |
'Purple': (128, 0, 128)
|
| 639 |
-
}
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
# ํ
์คํธ ํฌ๊ธฐ ๊ณ์ฐ
|
| 643 |
text_bbox = draw.textbbox((0, 0), text, font=font)
|
| 644 |
text_width = text_bbox[2] - text_bbox[0]
|
| 645 |
text_height = text_bbox[3] - text_bbox[1]
|
| 646 |
|
| 647 |
-
# ์์น ๊ณ์ฐ
|
| 648 |
actual_x = int((image.width - text_width) * (x_position / 100))
|
| 649 |
actual_y = int((image.height - text_height) * (y_position / 100))
|
| 650 |
|
| 651 |
-
# ํ
์คํธ ์์ ์ค์
|
| 652 |
text_color = (*rgb_color, int(opacity))
|
| 653 |
-
|
| 654 |
-
# ํ
์คํธ ๊ทธ๋ฆฌ๊ธฐ
|
| 655 |
-
add_text_with_stroke(
|
| 656 |
-
draw,
|
| 657 |
-
text,
|
| 658 |
-
actual_x,
|
| 659 |
-
actual_y,
|
| 660 |
-
font,
|
| 661 |
-
text_color,
|
| 662 |
-
int(thickness)
|
| 663 |
-
)
|
| 664 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 665 |
if text_position_type == "Text Behind Image":
|
| 666 |
-
#
|
| 667 |
-
|
| 668 |
-
|
|
|
|
| 669 |
else:
|
| 670 |
-
#
|
| 671 |
output_image = Image.alpha_composite(image, txt_overlay)
|
| 672 |
-
|
| 673 |
-
# RGB๋ก
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
return output_image
|
| 677 |
|
| 678 |
except Exception as e:
|
| 679 |
-
print(f"
|
| 680 |
return input_image
|
| 681 |
|
|
|
|
|
|
|
| 682 |
|
| 683 |
def update_position(new_position):
|
| 684 |
"""์์น ์
๋ฐ์ดํธ ํจ์"""
|
|
@@ -914,10 +910,13 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 914 |
queue=True
|
| 915 |
)
|
| 916 |
|
|
|
|
|
|
|
|
|
|
| 917 |
add_text_btn.click(
|
| 918 |
fn=add_text_to_image,
|
| 919 |
inputs=[
|
| 920 |
-
combined_image,
|
| 921 |
text_input,
|
| 922 |
font_size,
|
| 923 |
color_dropdown,
|
|
@@ -928,7 +927,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 928 |
text_position_type,
|
| 929 |
font_choice
|
| 930 |
],
|
| 931 |
-
outputs=combined_image
|
|
|
|
| 932 |
)
|
| 933 |
|
| 934 |
demo.queue(max_size=5)
|
|
|
|
| 578 |
y_position,
|
| 579 |
thickness,
|
| 580 |
text_position_type,
|
| 581 |
+
font_choice
|
| 582 |
):
|
| 583 |
"""
|
| 584 |
Add text to an image with customizable properties
|
| 585 |
"""
|
| 586 |
try:
|
| 587 |
+
if input_image is None or text.strip() == "":
|
| 588 |
+
return input_image
|
| 589 |
+
|
| 590 |
# PIL Image ๊ฐ์ฒด๋ก ๋ณํ
|
| 591 |
+
if isinstance(input_image, np.ndarray):
|
| 592 |
+
image = Image.fromarray(input_image)
|
| 593 |
+
elif isinstance(input_image, Image.Image):
|
|
|
|
|
|
|
|
|
|
| 594 |
image = input_image.copy()
|
| 595 |
+
else:
|
| 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)
|
| 613 |
+
|
| 614 |
# ํฐํธ ์ค์
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
try:
|
| 616 |
+
font_path = {
|
| 617 |
+
"Default": "DejaVuSans.ttf",
|
| 618 |
+
"Korean Regular": "ko-Regular.ttf",
|
| 619 |
+
"Korean Son": "ko-son.ttf"
|
| 620 |
+
}.get(font_choice, "DejaVuSans.ttf")
|
| 621 |
+
|
| 622 |
+
font = ImageFont.truetype(font_path, int(font_size))
|
| 623 |
except Exception as e:
|
| 624 |
+
print(f"Font error: {str(e)}, using default")
|
| 625 |
+
font = ImageFont.load_default()
|
| 626 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 627 |
# ์์ ์ค์
|
| 628 |
+
rgb_color = {
|
| 629 |
'White': (255, 255, 255),
|
| 630 |
'Black': (0, 0, 0),
|
| 631 |
'Red': (255, 0, 0),
|
|
|
|
| 633 |
'Blue': (0, 0, 255),
|
| 634 |
'Yellow': (255, 255, 0),
|
| 635 |
'Purple': (128, 0, 128)
|
| 636 |
+
}.get(color, (255, 255, 255))
|
| 637 |
+
|
| 638 |
+
# ํ
์คํธ ์์น ๊ณ์ฐ
|
|
|
|
| 639 |
text_bbox = draw.textbbox((0, 0), text, font=font)
|
| 640 |
text_width = text_bbox[2] - text_bbox[0]
|
| 641 |
text_height = text_bbox[3] - text_bbox[1]
|
| 642 |
|
|
|
|
| 643 |
actual_x = int((image.width - text_width) * (x_position / 100))
|
| 644 |
actual_y = int((image.height - text_height) * (y_position / 100))
|
| 645 |
|
| 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๋ก ๋ณํํ์ฌ ๋ฐํ
|
| 670 |
+
return output_image.convert('RGB')
|
|
|
|
|
|
|
| 671 |
|
| 672 |
except Exception as e:
|
| 673 |
+
print(f"Text addition error: {str(e)}")
|
| 674 |
return input_image
|
| 675 |
|
| 676 |
+
|
| 677 |
+
|
| 678 |
|
| 679 |
def update_position(new_position):
|
| 680 |
"""์์น ์
๋ฐ์ดํธ ํจ์"""
|
|
|
|
| 910 |
queue=True
|
| 911 |
)
|
| 912 |
|
| 913 |
+
|
| 914 |
+
|
| 915 |
+
# ์ด๋ฒคํธ ํธ๋ค๋ฌ ์์
|
| 916 |
add_text_btn.click(
|
| 917 |
fn=add_text_to_image,
|
| 918 |
inputs=[
|
| 919 |
+
combined_image, # ํ์ฌ ํ์๋ ์ด๋ฏธ์ง
|
| 920 |
text_input,
|
| 921 |
font_size,
|
| 922 |
color_dropdown,
|
|
|
|
| 927 |
text_position_type,
|
| 928 |
font_choice
|
| 929 |
],
|
| 930 |
+
outputs=combined_image,
|
| 931 |
+
show_progress=True
|
| 932 |
)
|
| 933 |
|
| 934 |
demo.queue(max_size=5)
|