Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -21,6 +21,12 @@ from diffusers import FluxPipeline
|
|
21 |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
22 |
import gc
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
def clear_memory():
|
25 |
"""메모리 정리 함수"""
|
26 |
gc.collect()
|
@@ -113,6 +119,8 @@ try:
|
|
113 |
except Exception as e:
|
114 |
print(f"Warning: Could not move pipeline to CUDA: {str(e)}")
|
115 |
|
|
|
|
|
116 |
class timer:
|
117 |
def __init__(self, method_name="timed process"):
|
118 |
self.method = method_name
|
@@ -482,9 +490,117 @@ button.primary:hover {
|
|
482 |
}
|
483 |
"""
|
484 |
|
485 |
-
# UI 구성
|
486 |
-
# UI 구성 부분에서 process_btn을 위로 이동하고 position_grid.click 부분 제거
|
487 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
488 |
# UI 구성
|
489 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
490 |
gr.HTML("""
|
|
|
21 |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
22 |
import gc
|
23 |
|
24 |
+
from PIL import Image, ImageDraw, ImageFont
|
25 |
+
from PIL import Image
|
26 |
+
from gradio_client import Client, handle_file
|
27 |
+
import uuid
|
28 |
+
|
29 |
+
|
30 |
def clear_memory():
|
31 |
"""메모리 정리 함수"""
|
32 |
gc.collect()
|
|
|
119 |
except Exception as e:
|
120 |
print(f"Warning: Could not move pipeline to CUDA: {str(e)}")
|
121 |
|
122 |
+
client = Client("NabeelShar/BiRefNet_for_text_writing")
|
123 |
+
|
124 |
class timer:
|
125 |
def __init__(self, method_name="timed process"):
|
126 |
self.method = method_name
|
|
|
490 |
}
|
491 |
"""
|
492 |
|
|
|
|
|
493 |
|
494 |
+
|
495 |
+
def add_text_with_stroke(draw, text, x, y, font, text_color, stroke_width):
|
496 |
+
"""Helper function to draw text with stroke"""
|
497 |
+
# Draw the stroke/outline
|
498 |
+
for adj_x in range(-stroke_width, stroke_width + 1):
|
499 |
+
for adj_y in range(-stroke_width, stroke_width + 1):
|
500 |
+
draw.text((x + adj_x, y + adj_y), text, font=font, fill=text_color)
|
501 |
+
|
502 |
+
def remove_background(image):
|
503 |
+
# Save the image to a specific location
|
504 |
+
filename = f"image_{uuid.uuid4()}.png" # Generates a universally unique identifier (UUID) for the filename
|
505 |
+
image.save(filename)
|
506 |
+
# Call gradio client for background removal
|
507 |
+
result = client.predict(images=handle_file(filename), api_name="/image")
|
508 |
+
return Image.open(result[0])
|
509 |
+
|
510 |
+
def superimpose(image_with_text, overlay_image):
|
511 |
+
# Open image as RGBA to handle transparency
|
512 |
+
overlay_image = overlay_image.convert("RGBA")
|
513 |
+
# Paste overlay on the background
|
514 |
+
image_with_text.paste(overlay_image, (0, 0), overlay_image)
|
515 |
+
# Save the final image
|
516 |
+
# image_with_text.save("output_image.png")
|
517 |
+
return image_with_text
|
518 |
+
|
519 |
+
def add_text_to_image(
|
520 |
+
input_image,
|
521 |
+
text,
|
522 |
+
font_size,
|
523 |
+
color,
|
524 |
+
opacity,
|
525 |
+
x_position,
|
526 |
+
y_position,
|
527 |
+
thickness
|
528 |
+
):
|
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 |
+
font = ImageFont.truetype("DejaVuSans.ttf", int(font_size))
|
547 |
+
except:
|
548 |
+
# If DejaVu font is not found, try to use Arial or default
|
549 |
+
try:
|
550 |
+
font = ImageFont.truetype("arial.ttf", int(font_size))
|
551 |
+
except:
|
552 |
+
print("Using default font as system fonts not found")
|
553 |
+
font = ImageFont.load_default()
|
554 |
+
|
555 |
+
# Convert color name to RGB
|
556 |
+
color_map = {
|
557 |
+
'White': (255, 255, 255),
|
558 |
+
'Black': (0, 0, 0),
|
559 |
+
'Red': (255, 0, 0),
|
560 |
+
'Green': (0, 255, 0),
|
561 |
+
'Blue': (0, 0, 255),
|
562 |
+
'Yellow': (255, 255, 0),
|
563 |
+
'Purple': (128, 0, 128)
|
564 |
+
}
|
565 |
+
rgb_color = color_map.get(color, (255, 255, 255))
|
566 |
+
|
567 |
+
# Get text size for positioning
|
568 |
+
text_bbox = draw.textbbox((0, 0), text, font=font)
|
569 |
+
text_width = text_bbox[2] - text_bbox[0]
|
570 |
+
text_height = text_bbox[3] - text_bbox[1]
|
571 |
+
|
572 |
+
# Calculate actual x and y positions based on percentages
|
573 |
+
actual_x = int((image.width - text_width) * (x_position / 100))
|
574 |
+
actual_y = int((image.height - text_height) * (y_position / 100))
|
575 |
+
|
576 |
+
# Create final color with opacity
|
577 |
+
text_color = (*rgb_color, int(opacity))
|
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 |
+
# Combine the original image with the text overlay
|
591 |
+
if image.mode != 'RGBA':
|
592 |
+
image = image.convert('RGBA')
|
593 |
+
output_image = Image.alpha_composite(image, txt_overlay)
|
594 |
+
|
595 |
+
# Convert back to RGB for display
|
596 |
+
output_image = output_image.convert('RGB')
|
597 |
+
|
598 |
+
# superimpose images
|
599 |
+
output_image = superimpose(output_image, overlay_image)
|
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("""
|