Spaces:
Sleeping
Sleeping
File size: 3,221 Bytes
a7a2a27 45a83bb a7a2a27 e38378d a7a2a27 e38378d 45a83bb 71e6bed 45a83bb e38378d 71e6bed 45a83bb 71e6bed 45a83bb 1cdc356 45a83bb a7a2a27 45a83bb e38378d 45a83bb e38378d 45a83bb 299e3a2 45a83bb 299e3a2 e38378d 45a83bb 299e3a2 45a83bb e38378d 45a83bb a7a2a27 45a83bb |
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import gradio as gr
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
def combine_images(dress_image, design_image, position, size, text, text_position, font_size, font_color):
# Convert images to numpy arrays
dress = np.array(dress_image)
design = np.array(design_image)
# Convert position and size from strings to tuples of integers
x, y = map(int, position.split(","))
width, height = map(int, size.split(","))
tx, ty = map(int, text_position.split(","))
# Resize design image to fit the size
design_resized = cv2.resize(design, (width, height))
# Create a mask for the design image
if design_resized.shape[2] == 4: # Handle transparency if PNG
design_mask = design_resized[:, :, 3] > 0
design_resized = design_resized[:, :, :3]
else:
design_mask = np.ones(design_resized.shape[:2], dtype=bool)
# Define region where the design will be placed
h, w = design_resized.shape[:2]
# Ensure the design fits within the dress image
if x + w > dress.shape[1]:
w = dress.shape[1] - x
design_resized = design_resized[:, :w]
design_mask = design_mask[:, :w]
if y + h > dress.shape[0]:
h = dress.shape[0] - y
design_resized = design_resized[:h]
design_mask = design_mask[:h]
# Place the design on the dress image
for c in range(3):
dress[y:y+h, x:x+w, c] = dress[y:y+h, x:x+w, c] * (1 - design_mask) + design_resized[:, :, c] * design_mask
# Convert to PIL for text addition
final_image = Image.fromarray(dress)
draw = ImageDraw.Draw(final_image)
# Define font (you may need to provide the path to a ttf file or use a basic font)
try:
font = ImageFont.truetype("arial.ttf", int(font_size))
except IOError:
font = ImageFont.load_default()
# Add text to the image
draw.text((tx, ty), text, font=font, fill=font_color)
return final_image
def interface():
with gr.Blocks() as demo:
gr.Markdown("## Image Editor with Text Addition")
dress_image = gr.Image(type="pil", label="Upload Dress Image")
design_image = gr.Image(type="pil", label="Upload Design Image")
position = gr.Textbox(placeholder="Enter position as x,y", value="100,100", label="Position")
size = gr.Textbox(placeholder="Enter size as width,height", value="200,200", label="Size")
text = gr.Textbox(placeholder="Enter text", label="Text")
text_position = gr.Textbox(placeholder="Enter text position as x,y", value="50,50", label="Text Position")
font_size = gr.Textbox(placeholder="Enter font size", value="24", label="Font Size")
font_color = gr.Textbox(placeholder="Enter font color (e.g., #FFFFFF)", value="#000000", label="Font Color")
btn = gr.Button("Fit Design")
output = gr.Image(type="pil", label="Final Output")
# Button click function
btn.click(
combine_images,
inputs=[dress_image, design_image, position, size, text, text_position, font_size, font_color],
outputs=output
)
return demo
demo = interface()
demo.launch()
|