Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -3,8 +3,9 @@ import numpy as np
|
|
3 |
import gradio as gr
|
4 |
from PIL import Image, ImageDraw, ImageFont
|
5 |
|
6 |
-
|
7 |
-
|
|
|
8 |
font_path = {
|
9 |
"Bold": "arialbd.ttf",
|
10 |
"Italic": "ariali.ttf",
|
@@ -15,8 +16,8 @@ def generate_text_image(text, style, width, height):
|
|
15 |
img = Image.new("RGBA", (width, height), (255, 255, 255, 0))
|
16 |
draw = ImageDraw.Draw(img)
|
17 |
|
18 |
-
# Dynamically calculate font size based on
|
19 |
-
font_size = int(min(width, height) *
|
20 |
try:
|
21 |
font = ImageFont.truetype(font_path[style], size=font_size)
|
22 |
except:
|
@@ -32,6 +33,7 @@ def generate_text_image(text, style, width, height):
|
|
32 |
|
33 |
return img
|
34 |
|
|
|
35 |
def apply_displacement_map(text_img, clothing_img, strength=20):
|
36 |
"""Apply displacement map to blend text onto clothing."""
|
37 |
gray = cv2.cvtColor(clothing_img, cv2.COLOR_BGR2GRAY)
|
@@ -45,20 +47,22 @@ def apply_displacement_map(text_img, clothing_img, strength=20):
|
|
45 |
displacement_map[:, :, 0] = grad_x * strength
|
46 |
displacement_map[:, :, 1] = grad_y * strength
|
47 |
|
48 |
-
text_warped = cv2.remap(
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
52 |
|
53 |
return text_warped
|
54 |
|
55 |
|
56 |
-
def overlay_text_on_clothing(clothing_image, text_input, style, strength=20, alpha=0.7, photo=None, blend_alpha=0.5):
|
57 |
"""Blend generated text and optional photo onto the clothing image."""
|
58 |
clothing_img = cv2.imread(clothing_image) # Read image from file path
|
59 |
|
60 |
# Generate text image dynamically
|
61 |
-
text_img_pil = generate_text_image(text_input, style, clothing_img.shape[1], clothing_img.shape[0])
|
62 |
text_img = cv2.cvtColor(np.array(text_img_pil), cv2.COLOR_RGBA2BGRA)
|
63 |
|
64 |
alpha_channel = text_img[:, :, 3] / 255.0
|
@@ -80,12 +84,14 @@ def overlay_text_on_clothing(clothing_image, text_input, style, strength=20, alp
|
|
80 |
blended_img = Image.fromarray(cv2.cvtColor(clothing_img, cv2.COLOR_BGR2RGB))
|
81 |
return blended_img
|
82 |
|
|
|
83 |
interface = gr.Interface(
|
84 |
fn=overlay_text_on_clothing,
|
85 |
inputs=[
|
86 |
gr.Image(type="filepath", label="Upload Clothing Image", interactive=True),
|
87 |
gr.Textbox(label="Enter Text for Design"),
|
88 |
gr.Radio(["Bold", "Italic", "Graffiti", "Calligraphy"], label="Select Style", value="Bold"),
|
|
|
89 |
gr.Slider(10, 50, step=5, value=20, label="Displacement Strength"),
|
90 |
gr.Slider(0.1, 1.0, step=0.1, value=0.7, label="Alpha Blending"),
|
91 |
gr.Image(type="filepath", label="Optional Photo for Blending", interactive=True),
|
@@ -93,7 +99,7 @@ interface = gr.Interface(
|
|
93 |
],
|
94 |
outputs=gr.Image(type="pil", label="Final Design"),
|
95 |
title="AI-Powered Clothing Text and Photo Overlay",
|
96 |
-
description="Upload a clothing image, enter a text design, and select a style
|
97 |
allow_flagging="never"
|
98 |
)
|
99 |
|
|
|
3 |
import gradio as gr
|
4 |
from PIL import Image, ImageDraw, ImageFont
|
5 |
|
6 |
+
|
7 |
+
def generate_text_image(text, style, width, height, font_scale):
|
8 |
+
"""Generate an image of the input text with the selected style and scale."""
|
9 |
font_path = {
|
10 |
"Bold": "arialbd.ttf",
|
11 |
"Italic": "ariali.ttf",
|
|
|
16 |
img = Image.new("RGBA", (width, height), (255, 255, 255, 0))
|
17 |
draw = ImageDraw.Draw(img)
|
18 |
|
19 |
+
# Dynamically calculate font size based on user-defined font scale
|
20 |
+
font_size = int(min(width, height) * font_scale)
|
21 |
try:
|
22 |
font = ImageFont.truetype(font_path[style], size=font_size)
|
23 |
except:
|
|
|
33 |
|
34 |
return img
|
35 |
|
36 |
+
|
37 |
def apply_displacement_map(text_img, clothing_img, strength=20):
|
38 |
"""Apply displacement map to blend text onto clothing."""
|
39 |
gray = cv2.cvtColor(clothing_img, cv2.COLOR_BGR2GRAY)
|
|
|
47 |
displacement_map[:, :, 0] = grad_x * strength
|
48 |
displacement_map[:, :, 1] = grad_y * strength
|
49 |
|
50 |
+
text_warped = cv2.remap(
|
51 |
+
text_img,
|
52 |
+
displacement_map[:, :, 0].astype(np.float32),
|
53 |
+
displacement_map[:, :, 1].astype(np.float32),
|
54 |
+
interpolation=cv2.INTER_LINEAR
|
55 |
+
)
|
56 |
|
57 |
return text_warped
|
58 |
|
59 |
|
60 |
+
def overlay_text_on_clothing(clothing_image, text_input, style, font_scale, strength=20, alpha=0.7, photo=None, blend_alpha=0.5):
|
61 |
"""Blend generated text and optional photo onto the clothing image."""
|
62 |
clothing_img = cv2.imread(clothing_image) # Read image from file path
|
63 |
|
64 |
# Generate text image dynamically
|
65 |
+
text_img_pil = generate_text_image(text_input, style, clothing_img.shape[1], clothing_img.shape[0], font_scale)
|
66 |
text_img = cv2.cvtColor(np.array(text_img_pil), cv2.COLOR_RGBA2BGRA)
|
67 |
|
68 |
alpha_channel = text_img[:, :, 3] / 255.0
|
|
|
84 |
blended_img = Image.fromarray(cv2.cvtColor(clothing_img, cv2.COLOR_BGR2RGB))
|
85 |
return blended_img
|
86 |
|
87 |
+
|
88 |
interface = gr.Interface(
|
89 |
fn=overlay_text_on_clothing,
|
90 |
inputs=[
|
91 |
gr.Image(type="filepath", label="Upload Clothing Image", interactive=True),
|
92 |
gr.Textbox(label="Enter Text for Design"),
|
93 |
gr.Radio(["Bold", "Italic", "Graffiti", "Calligraphy"], label="Select Style", value="Bold"),
|
94 |
+
gr.Slider(0.05, 0.5, step=0.05, value=0.1, label="Text Size (Scale)"),
|
95 |
gr.Slider(10, 50, step=5, value=20, label="Displacement Strength"),
|
96 |
gr.Slider(0.1, 1.0, step=0.1, value=0.7, label="Alpha Blending"),
|
97 |
gr.Image(type="filepath", label="Optional Photo for Blending", interactive=True),
|
|
|
99 |
],
|
100 |
outputs=gr.Image(type="pil", label="Final Design"),
|
101 |
title="AI-Powered Clothing Text and Photo Overlay",
|
102 |
+
description="Upload a clothing image, enter a text design, and select a style. Adjust text size, blending, and optional photo overlay for enhanced designs.",
|
103 |
allow_flagging="never"
|
104 |
)
|
105 |
|