Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +114 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import io
|
| 4 |
+
import PIL.Image as Image
|
| 5 |
+
from rembg import remove
|
| 6 |
+
from PIL import ImageDraw, ImageFont
|
| 7 |
+
import textwrap
|
| 8 |
+
|
| 9 |
+
# Function to remove background and place text behind person
|
| 10 |
+
def text_behind_image(input_image, text, text_color, font_size, text_opacity):
|
| 11 |
+
if input_image is None:
|
| 12 |
+
return None
|
| 13 |
+
|
| 14 |
+
# Convert color hex to RGB
|
| 15 |
+
try:
|
| 16 |
+
r = int(text_color[1:3], 16)
|
| 17 |
+
g = int(text_color[3:5], 16)
|
| 18 |
+
b = int(text_color[5:7], 16)
|
| 19 |
+
text_color_rgb = (r, g, b)
|
| 20 |
+
except:
|
| 21 |
+
text_color_rgb = (255, 255, 255) # Default to white
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
# Open the image
|
| 25 |
+
img = Image.fromarray(input_image)
|
| 26 |
+
|
| 27 |
+
# Get dimensions
|
| 28 |
+
width, height = img.size
|
| 29 |
+
|
| 30 |
+
# Create a new image with white background (this will be the canvas)
|
| 31 |
+
background = Image.new('RGBA', (width, height), (255, 255, 255, 255))
|
| 32 |
+
|
| 33 |
+
# Create a drawing context for the text
|
| 34 |
+
draw = ImageDraw.Draw(background)
|
| 35 |
+
|
| 36 |
+
# Determine font size (relative to image size)
|
| 37 |
+
font_size = int(min(width, height) * (font_size / 100)) # Convert percentage to actual size
|
| 38 |
+
|
| 39 |
+
# Try to load a nice font, fall back to default if not available
|
| 40 |
+
try:
|
| 41 |
+
font = ImageFont.truetype("arial.ttf", font_size)
|
| 42 |
+
except:
|
| 43 |
+
font = ImageFont.load_default()
|
| 44 |
+
|
| 45 |
+
# Word wrap the text to fit within the image width
|
| 46 |
+
margin = 20
|
| 47 |
+
wrapper = textwrap.TextWrapper(width=int(width/font_size*2) - 2*margin)
|
| 48 |
+
word_list = wrapper.wrap(text)
|
| 49 |
+
|
| 50 |
+
# Calculate text block height
|
| 51 |
+
text_height = len(word_list) * (font_size + 10)
|
| 52 |
+
|
| 53 |
+
# Position text in the center
|
| 54 |
+
y_text = (height - text_height) // 2
|
| 55 |
+
|
| 56 |
+
# Draw text with specified opacity
|
| 57 |
+
for line in word_list:
|
| 58 |
+
# Get line width
|
| 59 |
+
line_width = font.getbbox(line)[2] - font.getbbox(line)[0]
|
| 60 |
+
# Center the line
|
| 61 |
+
x_text = (width - line_width) // 2
|
| 62 |
+
|
| 63 |
+
# Draw text with specified opacity
|
| 64 |
+
text_color_with_opacity = text_color_rgb + (int(text_opacity * 255),)
|
| 65 |
+
draw.text((x_text, y_text), line, font=font, fill=text_color_with_opacity)
|
| 66 |
+
y_text += font_size + 10
|
| 67 |
+
|
| 68 |
+
# Remove background from the original image to get the person silhouette
|
| 69 |
+
person_img = remove(img)
|
| 70 |
+
|
| 71 |
+
# Composite the images: text in background, then person on top
|
| 72 |
+
final_image = Image.alpha_composite(background.convert('RGBA'), person_img.convert('RGBA'))
|
| 73 |
+
|
| 74 |
+
# Convert back to RGB for display
|
| 75 |
+
return np.array(final_image.convert('RGB'))
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
print(f"Error processing image: {e}")
|
| 79 |
+
return input_image # Return original image on error
|
| 80 |
+
|
| 81 |
+
# Create Gradio interface
|
| 82 |
+
with gr.Blocks(title="Text Behind Image") as demo:
|
| 83 |
+
gr.Markdown("# Text Behind Image")
|
| 84 |
+
gr.Markdown("Upload an image with a person and add text behind them")
|
| 85 |
+
|
| 86 |
+
with gr.Row():
|
| 87 |
+
with gr.Column():
|
| 88 |
+
input_image = gr.Image(label="Upload Image", type="numpy")
|
| 89 |
+
text_input = gr.Textbox(label="Text to place behind", placeholder="Enter text here...")
|
| 90 |
+
|
| 91 |
+
with gr.Row():
|
| 92 |
+
text_color = gr.ColorPicker(label="Text Color", value="#FFFFFF")
|
| 93 |
+
font_size = gr.Slider(label="Font Size (%)", minimum=1, maximum=30, value=10, step=1)
|
| 94 |
+
text_opacity = gr.Slider(label="Text Opacity", minimum=0.1, maximum=1.0, value=0.8, step=0.1)
|
| 95 |
+
|
| 96 |
+
submit_btn = gr.Button("Generate", variant="primary")
|
| 97 |
+
|
| 98 |
+
with gr.Column():
|
| 99 |
+
output_image = gr.Image(label="Result", type="numpy")
|
| 100 |
+
|
| 101 |
+
submit_btn.click(
|
| 102 |
+
fn=text_behind_image,
|
| 103 |
+
inputs=[input_image, text_input, text_color, font_size, text_opacity],
|
| 104 |
+
outputs=output_image
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
gr.Markdown("## How it works")
|
| 108 |
+
gr.Markdown("1. Upload an image with a person")
|
| 109 |
+
gr.Markdown("2. Enter the text you want to place behind the person")
|
| 110 |
+
gr.Markdown("3. Customize text color, size, and opacity")
|
| 111 |
+
gr.Markdown("4. Click 'Generate' to create your image")
|
| 112 |
+
|
| 113 |
+
# Launch the app
|
| 114 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=3.50.2
|
| 2 |
+
rembg>=2.0.50
|
| 3 |
+
pillow>=10.0.0
|
| 4 |
+
numpy>=1.24.0
|