File size: 2,632 Bytes
07180e9
 
ed5e427
c67b8fa
ed5e427
e83a791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07180e9
c67b8fa
e83a791
 
 
 
 
07180e9
e83a791
 
 
ed5e427
 
e83a791
ed5e427
e83a791
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import gradio as gr


# Function to detect fabric texture and generate a displacement map
def generate_displacement_map(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    laplacian = cv2.Laplacian(gray, cv2.CV_32F)  # Detect edges and texture
    displacement_map = cv2.normalize(laplacian, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
    return displacement_map


# Function to blend text with the cloth image
def blend_text_with_cloth(cloth_image, text, font_size=32, font_color=(255, 0, 0), x=50, y=50):
    # Convert cloth image to BGR for OpenCV processing
    cloth_image = np.array(cloth_image)
    cloth_bgr = cv2.cvtColor(cloth_image, cv2.COLOR_RGBA2BGRA)

    # Generate displacement map
    displacement_map = generate_displacement_map(cloth_bgr)

    # Render text
    img_pil = Image.fromarray(cloth_image)
    draw = ImageDraw.Draw(img_pil)

    # Load a font (use a default font if custom font isn't available)
    try:
        font = ImageFont.truetype("arial.ttf", font_size)
    except:
        font = ImageFont.load_default()

    draw.text((x, y), text, font=font, fill=font_color)
    rendered_text = np.array(img_pil)

    # Convert rendered text image to BGR
    text_bgr = cv2.cvtColor(rendered_text, cv2.COLOR_RGBA2BGRA)

    # Blend text with cloth image using displacement map
    blended = cv2.addWeighted(cloth_bgr, 0.8, text_bgr, 0.5, 0)
    final_image = cv2.cvtColor(blended, cv2.COLOR_BGRA2RGBA)

    return Image.fromarray(final_image)


# Gradio function
def process_image(image, text, font_size, font_color, x, y):
    font_color = tuple(map(int, font_color.strip("()").split(",")))  # Convert string to tuple
    result = blend_text_with_cloth(image, text, font_size, font_color, x, y)
    return result


# Gradio Interface
interface = gr.Interface(
    fn=process_image,
    inputs=[
        gr.Image(type="pil", label="Upload Cloth Image"),
        gr.Textbox(label="Text to Blend", default="Sample Text"),
        gr.Slider(10, 100, step=2, label="Font Size", default=32),
        gr.Textbox(label="Font Color (RGB)", default="(255, 0, 0)"),
        gr.Slider(0, 1000, step=10, label="X Coordinate", default=50),
        gr.Slider(0, 1000, step=10, label="Y Coordinate", default=50),
    ],
    outputs=gr.Image(type="pil", label="Blended Output"),
    title="Text-Cloth Blending",
    description="Upload a cloth image and add text that blends naturally into the fabric. Adjust parameters like font size, color, and position.",
)

# Launch the app
if __name__ == "__main__":
    interface.launch()