Spaces:
Paused
Paused
Update app-backup.py
Browse files- app-backup.py +132 -126
app-backup.py
CHANGED
@@ -529,79 +529,85 @@ def add_text_to_image(
|
|
529 |
"""
|
530 |
Add text to an image with customizable properties
|
531 |
"""
|
532 |
-
# Convert gradio image (numpy array) to PIL Image
|
533 |
-
if input_image is None:
|
534 |
-
return None
|
535 |
-
|
536 |
-
image = Image.fromarray(input_image)
|
537 |
-
# remove background
|
538 |
-
overlay_image = remove_background(image)
|
539 |
-
|
540 |
-
# Create a transparent overlay for the text
|
541 |
-
txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
|
542 |
-
draw = ImageDraw.Draw(txt_overlay)
|
543 |
-
|
544 |
-
# Create a font with specified size
|
545 |
try:
|
546 |
-
|
547 |
-
|
548 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
549 |
try:
|
550 |
-
font = ImageFont.truetype("
|
551 |
except:
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
actual_y = int((image.height - text_height) * (y_position / 100))
|
575 |
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
# Draw the text with stroke for thickness
|
580 |
-
add_text_with_stroke(
|
581 |
-
draw,
|
582 |
-
text,
|
583 |
-
actual_x,
|
584 |
-
actual_y,
|
585 |
-
font,
|
586 |
-
text_color,
|
587 |
-
int(thickness)
|
588 |
-
)
|
589 |
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
597 |
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
# Convert PIL image back to numpy array for Gradio
|
602 |
-
return np.array(output_image)
|
603 |
|
604 |
-
# UI 구성
|
605 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
606 |
gr.HTML("""
|
607 |
<div class="main-title">
|
@@ -669,73 +675,73 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
669 |
)
|
670 |
|
671 |
with gr.Column(scale=1):
|
672 |
-
with gr.
|
673 |
combined_image = gr.Image(
|
674 |
label="Combined Result",
|
675 |
show_download_button=True,
|
676 |
type="pil",
|
677 |
height=512
|
678 |
)
|
679 |
-
|
680 |
-
# 텍스트 삽입 컨트롤 섹션 추가
|
681 |
-
with gr.Accordion("Add Text to Image", open=False):
|
682 |
-
text_input = gr.Textbox(
|
683 |
-
label="Text to Add",
|
684 |
-
placeholder="Enter text to overlay on image..."
|
685 |
-
)
|
686 |
-
with gr.Row():
|
687 |
-
with gr.Column(scale=1):
|
688 |
-
font_size = gr.Slider(
|
689 |
-
minimum=10,
|
690 |
-
maximum=200,
|
691 |
-
value=40,
|
692 |
-
step=5,
|
693 |
-
label="Font Size"
|
694 |
-
)
|
695 |
-
color_dropdown = gr.Dropdown(
|
696 |
-
choices=["White", "Black", "Red", "Green", "Blue", "Yellow", "Purple"],
|
697 |
-
value="White",
|
698 |
-
label="Text Color"
|
699 |
-
)
|
700 |
-
thickness = gr.Slider(
|
701 |
-
minimum=0,
|
702 |
-
maximum=10,
|
703 |
-
value=1,
|
704 |
-
step=1,
|
705 |
-
label="Text Thickness"
|
706 |
-
)
|
707 |
-
with gr.Column(scale=1):
|
708 |
-
opacity_slider = gr.Slider(
|
709 |
-
minimum=0,
|
710 |
-
maximum=255,
|
711 |
-
value=255,
|
712 |
-
step=1,
|
713 |
-
label="Opacity"
|
714 |
-
)
|
715 |
-
x_position = gr.Slider(
|
716 |
-
minimum=0,
|
717 |
-
maximum=100,
|
718 |
-
value=50,
|
719 |
-
step=1,
|
720 |
-
label="X Position (%)"
|
721 |
-
)
|
722 |
-
y_position = gr.Slider(
|
723 |
-
minimum=0,
|
724 |
-
maximum=100,
|
725 |
-
value=50,
|
726 |
-
step=1,
|
727 |
-
label="Y Position (%)"
|
728 |
-
)
|
729 |
|
730 |
-
|
731 |
-
|
732 |
-
|
733 |
-
|
734 |
-
|
735 |
-
|
736 |
-
|
737 |
-
|
738 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
739 |
|
740 |
# 각 버튼에 대한 클릭 이벤트 처리
|
741 |
def update_position(new_position):
|
|
|
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 객체로 변환 (이미 PIL Image인 경우 그대로 사용)
|
538 |
+
if not isinstance(input_image, Image.Image):
|
539 |
+
if isinstance(input_image, np.ndarray):
|
540 |
+
image = Image.fromarray(input_image)
|
541 |
+
else:
|
542 |
+
raise ValueError("Unsupported image type")
|
543 |
+
else:
|
544 |
+
image = input_image.copy()
|
545 |
+
|
546 |
+
# 이미지를 RGBA 모드로 변환
|
547 |
+
if image.mode != 'RGBA':
|
548 |
+
image = image.convert('RGBA')
|
549 |
+
|
550 |
+
# Create a transparent overlay for the text
|
551 |
+
txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
|
552 |
+
draw = ImageDraw.Draw(txt_overlay)
|
553 |
+
|
554 |
+
# Create a font with specified size
|
555 |
try:
|
556 |
+
font = ImageFont.truetype("DejaVuSans.ttf", int(font_size))
|
557 |
except:
|
558 |
+
try:
|
559 |
+
font = ImageFont.truetype("arial.ttf", int(font_size))
|
560 |
+
except:
|
561 |
+
print("Using default font")
|
562 |
+
font = ImageFont.load_default()
|
563 |
+
|
564 |
+
# Convert color name to RGB
|
565 |
+
color_map = {
|
566 |
+
'White': (255, 255, 255),
|
567 |
+
'Black': (0, 0, 0),
|
568 |
+
'Red': (255, 0, 0),
|
569 |
+
'Green': (0, 255, 0),
|
570 |
+
'Blue': (0, 0, 255),
|
571 |
+
'Yellow': (255, 255, 0),
|
572 |
+
'Purple': (128, 0, 128)
|
573 |
+
}
|
574 |
+
rgb_color = color_map.get(color, (255, 255, 255))
|
575 |
+
|
576 |
+
# Get text size for positioning
|
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 |
+
# Calculate actual positions
|
582 |
+
actual_x = int((image.width - text_width) * (x_position / 100))
|
583 |
+
actual_y = int((image.height - text_height) * (y_position / 100))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
584 |
|
585 |
+
# Create final color with opacity
|
586 |
+
text_color = (*rgb_color, int(opacity))
|
587 |
+
|
588 |
+
# Draw text with stroke
|
589 |
+
add_text_with_stroke(
|
590 |
+
draw,
|
591 |
+
text,
|
592 |
+
actual_x,
|
593 |
+
actual_y,
|
594 |
+
font,
|
595 |
+
text_color,
|
596 |
+
int(thickness)
|
597 |
+
)
|
598 |
+
|
599 |
+
# Combine the original image with the text overlay
|
600 |
+
output_image = Image.alpha_composite(image, txt_overlay)
|
601 |
+
|
602 |
+
# Convert back to RGB for display
|
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("""
|
613 |
<div class="main-title">
|
|
|
675 |
)
|
676 |
|
677 |
with gr.Column(scale=1):
|
678 |
+
with gr.Tab("Result"):
|
679 |
combined_image = gr.Image(
|
680 |
label="Combined Result",
|
681 |
show_download_button=True,
|
682 |
type="pil",
|
683 |
height=512
|
684 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
685 |
|
686 |
+
# 텍스트 삽입 컨트롤을 더 명확하게 구분
|
687 |
+
with gr.Group():
|
688 |
+
gr.Markdown("### Add Text to Image")
|
689 |
+
text_input = gr.Textbox(
|
690 |
+
label="Text Content",
|
691 |
+
placeholder="Enter text to add to image..."
|
692 |
+
)
|
693 |
+
with gr.Row():
|
694 |
+
with gr.Column(scale=1):
|
695 |
+
font_size = gr.Slider(
|
696 |
+
minimum=10,
|
697 |
+
maximum=200,
|
698 |
+
value=40,
|
699 |
+
step=5,
|
700 |
+
label="Font Size"
|
701 |
+
)
|
702 |
+
color_dropdown = gr.Dropdown(
|
703 |
+
choices=["White", "Black", "Red", "Green", "Blue", "Yellow", "Purple"],
|
704 |
+
value="White",
|
705 |
+
label="Text Color"
|
706 |
+
)
|
707 |
+
thickness = gr.Slider(
|
708 |
+
minimum=0,
|
709 |
+
maximum=10,
|
710 |
+
value=1,
|
711 |
+
step=1,
|
712 |
+
label="Text Thickness"
|
713 |
+
)
|
714 |
+
with gr.Column(scale=1):
|
715 |
+
opacity_slider = gr.Slider(
|
716 |
+
minimum=0,
|
717 |
+
maximum=255,
|
718 |
+
value=255,
|
719 |
+
step=1,
|
720 |
+
label="Opacity"
|
721 |
+
)
|
722 |
+
x_position = gr.Slider(
|
723 |
+
minimum=0,
|
724 |
+
maximum=100,
|
725 |
+
value=50,
|
726 |
+
step=1,
|
727 |
+
label="X Position (%)"
|
728 |
+
)
|
729 |
+
y_position = gr.Slider(
|
730 |
+
minimum=0,
|
731 |
+
maximum=100,
|
732 |
+
value=50,
|
733 |
+
step=1,
|
734 |
+
label="Y Position (%)"
|
735 |
+
)
|
736 |
+
add_text_btn = gr.Button("Apply Text", variant="primary")
|
737 |
+
|
738 |
+
with gr.Row():
|
739 |
+
extracted_image = gr.Image(
|
740 |
+
label="Extracted Object",
|
741 |
+
show_download_button=True,
|
742 |
+
type="pil",
|
743 |
+
height=256
|
744 |
+
)
|
745 |
|
746 |
# 각 버튼에 대한 클릭 이벤트 처리
|
747 |
def update_position(new_position):
|