Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -3,70 +3,56 @@ import numpy as np
|
|
3 |
from PIL import Image, ImageDraw, ImageFont
|
4 |
import gradio as gr
|
5 |
|
6 |
-
|
7 |
-
# Function to detect fabric texture and generate a displacement map
|
8 |
def generate_displacement_map(image):
|
9 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
10 |
laplacian = cv2.Laplacian(gray, cv2.CV_32F) # Detect edges and texture
|
11 |
displacement_map = cv2.normalize(laplacian, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
|
12 |
return displacement_map
|
13 |
|
14 |
-
|
15 |
-
# Function to blend text with the cloth image
|
16 |
-
def blend_text_with_cloth(cloth_image, text, font_size=32, font_color=(255, 0, 0), x=50, y=50):
|
17 |
-
# Convert cloth image to BGR for OpenCV processing
|
18 |
cloth_image = np.array(cloth_image)
|
19 |
-
cloth_bgr = cv2.cvtColor(cloth_image, cv2.
|
20 |
-
|
21 |
-
# Generate displacement map
|
22 |
displacement_map = generate_displacement_map(cloth_bgr)
|
23 |
|
24 |
-
# Render text
|
25 |
img_pil = Image.fromarray(cloth_image)
|
26 |
draw = ImageDraw.Draw(img_pil)
|
27 |
-
|
28 |
-
# Load a font (use a default font if custom font isn't available)
|
29 |
try:
|
30 |
font = ImageFont.truetype("arial.ttf", font_size)
|
31 |
except:
|
32 |
font = ImageFont.load_default()
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
44 |
return Image.fromarray(final_image)
|
45 |
|
46 |
-
|
47 |
-
# Gradio function
|
48 |
def process_image(image, text, font_size, font_color, x, y):
|
49 |
-
font_color = tuple(map(int, font_color.strip("()").split(",")))
|
50 |
-
result =
|
51 |
return result
|
52 |
|
53 |
-
|
54 |
-
# Gradio Interface
|
55 |
interface = gr.Interface(
|
56 |
fn=process_image,
|
57 |
inputs=[
|
58 |
gr.Image(type="pil", label="Upload Cloth Image"),
|
59 |
-
gr.Textbox(label="Text to Blend", value="
|
60 |
gr.Slider(10, 100, step=2, label="Font Size", value=32),
|
61 |
-
gr.Textbox(label="Font Color (RGB)", value="(255,
|
62 |
gr.Slider(0, 1000, step=10, label="X Coordinate", value=50),
|
63 |
gr.Slider(0, 1000, step=10, label="Y Coordinate", value=50),
|
64 |
],
|
65 |
outputs=gr.Image(type="pil", label="Blended Output"),
|
66 |
-
title="Text-Cloth Blending",
|
67 |
-
description="Upload a cloth image and add
|
68 |
)
|
69 |
|
70 |
-
# Launch the app
|
71 |
if __name__ == "__main__":
|
72 |
-
interface.launch(
|
|
|
3 |
from PIL import Image, ImageDraw, ImageFont
|
4 |
import gradio as gr
|
5 |
|
|
|
|
|
6 |
def generate_displacement_map(image):
|
7 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
8 |
laplacian = cv2.Laplacian(gray, cv2.CV_32F) # Detect edges and texture
|
9 |
displacement_map = cv2.normalize(laplacian, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
|
10 |
return displacement_map
|
11 |
|
12 |
+
def apply_text_warping(cloth_image, text, font_size=32, font_color=(255, 255, 255), x=50, y=50):
|
|
|
|
|
|
|
13 |
cloth_image = np.array(cloth_image)
|
14 |
+
cloth_bgr = cv2.cvtColor(cloth_image, cv2.COLOR_RGBA2BGR)
|
|
|
|
|
15 |
displacement_map = generate_displacement_map(cloth_bgr)
|
16 |
|
|
|
17 |
img_pil = Image.fromarray(cloth_image)
|
18 |
draw = ImageDraw.Draw(img_pil)
|
|
|
|
|
19 |
try:
|
20 |
font = ImageFont.truetype("arial.ttf", font_size)
|
21 |
except:
|
22 |
font = ImageFont.load_default()
|
23 |
+
|
24 |
+
text_img = Image.new("RGBA", img_pil.size, (255, 255, 255, 0))
|
25 |
+
text_draw = ImageDraw.Draw(text_img)
|
26 |
+
text_draw.text((x, y), text, font=font, fill=font_color+(255,))
|
27 |
+
text_img = np.array(text_img)
|
28 |
+
text_gray = cv2.cvtColor(text_img, cv2.COLOR_RGBA2GRAY)
|
29 |
+
_, text_mask = cv2.threshold(text_gray, 1, 255, cv2.THRESH_BINARY)
|
30 |
+
text_mask = cv2.GaussianBlur(text_mask, (5, 5), 3)
|
31 |
+
text_mask = cv2.cvtColor(text_mask, cv2.COLOR_GRAY2BGRA) / 255.0
|
32 |
+
cloth_bgr = cv2.cvtColor(cloth_image, cv2.COLOR_RGBA2BGRA)
|
33 |
+
blended = cloth_bgr * (1 - text_mask) + text_img * text_mask
|
34 |
+
final_image = cv2.cvtColor(blended.astype(np.uint8), cv2.COLOR_BGRA2RGBA)
|
35 |
return Image.fromarray(final_image)
|
36 |
|
|
|
|
|
37 |
def process_image(image, text, font_size, font_color, x, y):
|
38 |
+
font_color = tuple(map(int, font_color.strip("()").split(",")))
|
39 |
+
result = apply_text_warping(image, text, font_size, font_color, x, y)
|
40 |
return result
|
41 |
|
|
|
|
|
42 |
interface = gr.Interface(
|
43 |
fn=process_image,
|
44 |
inputs=[
|
45 |
gr.Image(type="pil", label="Upload Cloth Image"),
|
46 |
+
gr.Textbox(label="Text to Blend", value="Your Text Here"),
|
47 |
gr.Slider(10, 100, step=2, label="Font Size", value=32),
|
48 |
+
gr.Textbox(label="Font Color (RGB)", value="(255,255,255)"),
|
49 |
gr.Slider(0, 1000, step=10, label="X Coordinate", value=50),
|
50 |
gr.Slider(0, 1000, step=10, label="Y Coordinate", value=50),
|
51 |
],
|
52 |
outputs=gr.Image(type="pil", label="Blended Output"),
|
53 |
+
title="Advanced Text-Cloth Blending",
|
54 |
+
description="Upload a cloth image and add naturally blended, warped text.",
|
55 |
)
|
56 |
|
|
|
57 |
if __name__ == "__main__":
|
58 |
+
interface.launch()
|