Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -524,17 +524,17 @@ def add_text_to_image(
|
|
524 |
opacity,
|
525 |
x_position,
|
526 |
y_position,
|
527 |
-
thickness
|
|
|
528 |
):
|
529 |
"""
|
530 |
Add text to an image with customizable properties
|
531 |
"""
|
532 |
try:
|
533 |
-
# 입력 이미지 처리 수정
|
534 |
if input_image is None:
|
535 |
return None
|
536 |
|
537 |
-
# PIL Image 객체로 변환
|
538 |
if not isinstance(input_image, Image.Image):
|
539 |
if isinstance(input_image, np.ndarray):
|
540 |
image = Image.fromarray(input_image)
|
@@ -547,11 +547,16 @@ def add_text_to_image(
|
|
547 |
if image.mode != 'RGBA':
|
548 |
image = image.convert('RGBA')
|
549 |
|
550 |
-
#
|
|
|
|
|
|
|
|
|
|
|
551 |
txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
|
552 |
draw = ImageDraw.Draw(txt_overlay)
|
553 |
|
554 |
-
#
|
555 |
try:
|
556 |
font = ImageFont.truetype("DejaVuSans.ttf", int(font_size))
|
557 |
except:
|
@@ -561,7 +566,7 @@ def add_text_to_image(
|
|
561 |
print("Using default font")
|
562 |
font = ImageFont.load_default()
|
563 |
|
564 |
-
#
|
565 |
color_map = {
|
566 |
'White': (255, 255, 255),
|
567 |
'Black': (0, 0, 0),
|
@@ -573,19 +578,19 @@ def add_text_to_image(
|
|
573 |
}
|
574 |
rgb_color = color_map.get(color, (255, 255, 255))
|
575 |
|
576 |
-
#
|
577 |
text_bbox = draw.textbbox((0, 0), text, font=font)
|
578 |
text_width = text_bbox[2] - text_bbox[0]
|
579 |
text_height = text_bbox[3] - text_bbox[1]
|
580 |
|
581 |
-
#
|
582 |
actual_x = int((image.width - text_width) * (x_position / 100))
|
583 |
actual_y = int((image.height - text_height) * (y_position / 100))
|
584 |
|
585 |
-
#
|
586 |
text_color = (*rgb_color, int(opacity))
|
587 |
|
588 |
-
#
|
589 |
add_text_with_stroke(
|
590 |
draw,
|
591 |
text,
|
@@ -596,17 +601,22 @@ def add_text_to_image(
|
|
596 |
int(thickness)
|
597 |
)
|
598 |
|
599 |
-
|
600 |
-
|
|
|
|
|
|
|
|
|
|
|
601 |
|
602 |
-
#
|
603 |
output_image = output_image.convert('RGB')
|
604 |
|
605 |
return output_image
|
606 |
|
607 |
except Exception as e:
|
608 |
print(f"Error in add_text_to_image: {str(e)}")
|
609 |
-
return input_image
|
610 |
|
611 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
612 |
gr.HTML("""
|
@@ -686,10 +696,18 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
686 |
# 텍스트 삽입 컨트롤을 더 명확하게 구분
|
687 |
with gr.Group():
|
688 |
gr.Markdown("### Add Text to Image")
|
689 |
-
|
690 |
-
|
691 |
-
|
692 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
693 |
with gr.Row():
|
694 |
with gr.Column(scale=1):
|
695 |
font_size = gr.Slider(
|
@@ -812,7 +830,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
812 |
opacity_slider,
|
813 |
x_position,
|
814 |
y_position,
|
815 |
-
thickness
|
|
|
816 |
],
|
817 |
outputs=combined_image
|
818 |
)
|
|
|
524 |
opacity,
|
525 |
x_position,
|
526 |
y_position,
|
527 |
+
thickness,
|
528 |
+
text_position_type
|
529 |
):
|
530 |
"""
|
531 |
Add text to an image with customizable properties
|
532 |
"""
|
533 |
try:
|
|
|
534 |
if input_image is None:
|
535 |
return None
|
536 |
|
537 |
+
# PIL Image 객체로 변환
|
538 |
if not isinstance(input_image, Image.Image):
|
539 |
if isinstance(input_image, np.ndarray):
|
540 |
image = Image.fromarray(input_image)
|
|
|
547 |
if image.mode != 'RGBA':
|
548 |
image = image.convert('RGBA')
|
549 |
|
550 |
+
# Text Behind Image 처리
|
551 |
+
if text_position_type == "Text Behind Image":
|
552 |
+
# 원본 이미지의 배경 제거
|
553 |
+
overlay_image = remove_background(image)
|
554 |
+
|
555 |
+
# 텍스트 오버레이 생성
|
556 |
txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
|
557 |
draw = ImageDraw.Draw(txt_overlay)
|
558 |
|
559 |
+
# 폰트 설정
|
560 |
try:
|
561 |
font = ImageFont.truetype("DejaVuSans.ttf", int(font_size))
|
562 |
except:
|
|
|
566 |
print("Using default font")
|
567 |
font = ImageFont.load_default()
|
568 |
|
569 |
+
# 색상 설정
|
570 |
color_map = {
|
571 |
'White': (255, 255, 255),
|
572 |
'Black': (0, 0, 0),
|
|
|
578 |
}
|
579 |
rgb_color = color_map.get(color, (255, 255, 255))
|
580 |
|
581 |
+
# 텍스트 크기 계산
|
582 |
text_bbox = draw.textbbox((0, 0), text, font=font)
|
583 |
text_width = text_bbox[2] - text_bbox[0]
|
584 |
text_height = text_bbox[3] - text_bbox[1]
|
585 |
|
586 |
+
# 위치 계산
|
587 |
actual_x = int((image.width - text_width) * (x_position / 100))
|
588 |
actual_y = int((image.height - text_height) * (y_position / 100))
|
589 |
|
590 |
+
# 텍스트 색상 설정
|
591 |
text_color = (*rgb_color, int(opacity))
|
592 |
|
593 |
+
# 텍스트 그리기
|
594 |
add_text_with_stroke(
|
595 |
draw,
|
596 |
text,
|
|
|
601 |
int(thickness)
|
602 |
)
|
603 |
|
604 |
+
if text_position_type == "Text Behind Image":
|
605 |
+
# 텍스트를 먼저 그리고 그 위에 이미지 오버레이
|
606 |
+
output_image = Image.alpha_composite(image, txt_overlay)
|
607 |
+
output_image = superimpose(output_image, overlay_image)
|
608 |
+
else:
|
609 |
+
# 기존 방식대로 텍스트를 이미지 위에 그리기
|
610 |
+
output_image = Image.alpha_composite(image, txt_overlay)
|
611 |
|
612 |
+
# RGB로 변환
|
613 |
output_image = output_image.convert('RGB')
|
614 |
|
615 |
return output_image
|
616 |
|
617 |
except Exception as e:
|
618 |
print(f"Error in add_text_to_image: {str(e)}")
|
619 |
+
return input_image
|
620 |
|
621 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
622 |
gr.HTML("""
|
|
|
696 |
# 텍스트 삽입 컨트롤을 더 명확하게 구분
|
697 |
with gr.Group():
|
698 |
gr.Markdown("### Add Text to Image")
|
699 |
+
with gr.Row():
|
700 |
+
text_input = gr.Textbox(
|
701 |
+
label="Text Content",
|
702 |
+
placeholder="Enter text to add to image..."
|
703 |
+
)
|
704 |
+
text_position_type = gr.Radio(
|
705 |
+
choices=["Text Over Image", "Text Behind Image"],
|
706 |
+
value="Text Over Image",
|
707 |
+
label="Text Position Type",
|
708 |
+
interactive=True
|
709 |
+
)
|
710 |
+
|
711 |
with gr.Row():
|
712 |
with gr.Column(scale=1):
|
713 |
font_size = gr.Slider(
|
|
|
830 |
opacity_slider,
|
831 |
x_position,
|
832 |
y_position,
|
833 |
+
thickness,
|
834 |
+
text_position_type
|
835 |
],
|
836 |
outputs=combined_image
|
837 |
)
|