Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image, ImageDraw, ImageFont
|
3 |
+
import numpy as np
|
4 |
+
import os
|
5 |
+
from PIL import Image
|
6 |
+
from gradio_client import Client, handle_file
|
7 |
+
import uuid
|
8 |
+
|
9 |
+
client = Client("ysharma/BiRefNet_for_text_writing")
|
10 |
+
|
11 |
+
def remove_background(image):
|
12 |
+
# Convert NumPy array to PIL Image
|
13 |
+
#image = Image.fromarray(image_array)
|
14 |
+
# Save the image to a specific location
|
15 |
+
filename = f"image_{uuid.uuid4()}.png" # Generates a universally unique identifier (UUID) for the filename
|
16 |
+
image.save(filename)
|
17 |
+
|
18 |
+
result = client.predict(images=handle_file(filename), api_name="/image")
|
19 |
+
return Image.open(result[0])
|
20 |
+
|
21 |
+
def superimpose(image_with_text, overlay_image):
|
22 |
+
# Open both images as RGBA to handle transparency
|
23 |
+
#background = image_with_text #Image.open("/content/out2.png").convert("RGBA")
|
24 |
+
overlay_image = overlay_image.convert("RGBA")
|
25 |
+
# Paste overlay on the background
|
26 |
+
image_with_text.paste(overlay_image, (0, 0), overlay)
|
27 |
+
|
28 |
+
# Display or save the final image
|
29 |
+
#background.show()
|
30 |
+
# background.save("output_image.png")
|
31 |
+
return image_with_text
|
32 |
+
|
33 |
+
|
34 |
+
def add_text_to_image(
|
35 |
+
input_image,
|
36 |
+
text,
|
37 |
+
font_size,
|
38 |
+
color,
|
39 |
+
opacity,
|
40 |
+
x_position,
|
41 |
+
y_position
|
42 |
+
):
|
43 |
+
"""
|
44 |
+
Add text to an image with customizable properties
|
45 |
+
"""
|
46 |
+
# Convert gradio image (numpy array) to PIL Image
|
47 |
+
if input_image is None:
|
48 |
+
return None
|
49 |
+
|
50 |
+
image = Image.fromarray(input_image)
|
51 |
+
# remove background
|
52 |
+
overlay_image = remove_background(image)
|
53 |
+
|
54 |
+
# Create a transparent overlay for the text
|
55 |
+
txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
|
56 |
+
draw = ImageDraw.Draw(txt_overlay)
|
57 |
+
|
58 |
+
# Create a font with specified size
|
59 |
+
try:
|
60 |
+
font = ImageFont.truetype("DejaVuSans.ttf", int(font_size))
|
61 |
+
except:
|
62 |
+
# If DejaVu font is not found, try to use Arial or default
|
63 |
+
try:
|
64 |
+
font = ImageFont.truetype("arial.ttf", int(font_size))
|
65 |
+
except:
|
66 |
+
print("Using default font as system fonts not found")
|
67 |
+
font = ImageFont.load_default()
|
68 |
+
|
69 |
+
# Convert color name to RGB
|
70 |
+
color_map = {
|
71 |
+
'White': (255, 255, 255),
|
72 |
+
'Black': (0, 0, 0),
|
73 |
+
'Red': (255, 0, 0),
|
74 |
+
'Green': (0, 255, 0),
|
75 |
+
'Blue': (0, 0, 255),
|
76 |
+
'Yellow': (255, 255, 0),
|
77 |
+
'Purple': (128, 0, 128)
|
78 |
+
}
|
79 |
+
rgb_color = color_map.get(color, (255, 255, 255))
|
80 |
+
|
81 |
+
# Get text size for positioning
|
82 |
+
text_bbox = draw.textbbox((0, 0), text, font=font)
|
83 |
+
text_width = text_bbox[2] - text_bbox[0]
|
84 |
+
text_height = text_bbox[3] - text_bbox[1]
|
85 |
+
|
86 |
+
# Calculate actual x and y positions based on percentages
|
87 |
+
actual_x = int((image.width - text_width) * (x_position / 100))
|
88 |
+
actual_y = int((image.height - text_height) * (y_position / 100))
|
89 |
+
|
90 |
+
# Draw the main text
|
91 |
+
draw.text(
|
92 |
+
(actual_x, actual_y),
|
93 |
+
text,
|
94 |
+
font=font,
|
95 |
+
fill=(*rgb_color, int(opacity))
|
96 |
+
)
|
97 |
+
|
98 |
+
# Combine the original image with the text overlay
|
99 |
+
if image.mode != 'RGBA':
|
100 |
+
image = image.convert('RGBA')
|
101 |
+
output_image = Image.alpha_composite(image, txt_overlay)
|
102 |
+
|
103 |
+
# Convert back to RGB for display
|
104 |
+
output_image = output_image.convert('RGB')
|
105 |
+
|
106 |
+
# superimpose images
|
107 |
+
output_image = superimpose(output_image, overlay_image)
|
108 |
+
|
109 |
+
# Convert PIL image back to numpy array for Gradio
|
110 |
+
return np.array(output_image)
|
111 |
+
|
112 |
+
# Create the Gradio interface
|
113 |
+
def create_interface():
|
114 |
+
with gr.Blocks(title="Text on Image Editor", theme="citrus") as app:
|
115 |
+
gr.Markdown("# Add Text to Image")
|
116 |
+
gr.Markdown("Upload an image and customize text properties to add text overlay.")
|
117 |
+
|
118 |
+
with gr.Row():
|
119 |
+
with gr.Column():
|
120 |
+
# Input components
|
121 |
+
input_image = gr.Image(label="Upload Image", type="numpy")
|
122 |
+
text_input = gr.Textbox(label="Enter Text", placeholder="Type your text here...")
|
123 |
+
font_size = gr.Slider(minimum=10, maximum=800, value=400, step=10,
|
124 |
+
label="Font Size")
|
125 |
+
color_dropdown = gr.Dropdown(
|
126 |
+
choices=["White", "Black", "Red", "Green", "Blue", "Yellow", "Purple"],
|
127 |
+
value="White",
|
128 |
+
label="Text Color"
|
129 |
+
)
|
130 |
+
opacity_slider = gr.Slider(minimum=0, maximum=255, value=255, step=1,
|
131 |
+
label="Opacity")
|
132 |
+
x_position = gr.Slider(minimum=0, maximum=100, value=50, step=1,
|
133 |
+
label="X Position (%)")
|
134 |
+
y_position = gr.Slider(minimum=0, maximum=100, value=50, step=1,
|
135 |
+
label="Y Position (%)")
|
136 |
+
|
137 |
+
with gr.Column():
|
138 |
+
# Output image
|
139 |
+
output_image = gr.Image(label="Output Image")
|
140 |
+
|
141 |
+
# Process button
|
142 |
+
process_btn = gr.Button("Add Text to Image")
|
143 |
+
|
144 |
+
# Connect the input components to the processing function
|
145 |
+
process_btn.click(
|
146 |
+
fn=add_text_to_image,
|
147 |
+
inputs=[
|
148 |
+
input_image,
|
149 |
+
text_input,
|
150 |
+
font_size,
|
151 |
+
color_dropdown,
|
152 |
+
opacity_slider,
|
153 |
+
x_position,
|
154 |
+
y_position
|
155 |
+
],
|
156 |
+
outputs=output_image
|
157 |
+
)
|
158 |
+
|
159 |
+
# Add example inputs
|
160 |
+
gr.Examples(
|
161 |
+
examples=[
|
162 |
+
[
|
163 |
+
"sample.jpg", # Replace with actual example image path
|
164 |
+
"SAMPLE TEXT",
|
165 |
+
400,
|
166 |
+
"White",
|
167 |
+
255,
|
168 |
+
50,
|
169 |
+
50
|
170 |
+
]
|
171 |
+
],
|
172 |
+
inputs=[
|
173 |
+
input_image,
|
174 |
+
text_input,
|
175 |
+
font_size,
|
176 |
+
color_dropdown,
|
177 |
+
opacity_slider,
|
178 |
+
x_position,
|
179 |
+
y_position
|
180 |
+
]
|
181 |
+
)
|
182 |
+
|
183 |
+
return app
|
184 |
+
|
185 |
+
# Launch the app
|
186 |
+
if __name__ == "__main__":
|
187 |
+
# Try to install required font
|
188 |
+
try:
|
189 |
+
import subprocess
|
190 |
+
subprocess.run(['apt-get', 'update'])
|
191 |
+
subprocess.run(['apt-get', 'install', '-y', 'fonts-dejavu'])
|
192 |
+
print("Font installed successfully")
|
193 |
+
except:
|
194 |
+
print("Could not install font automatically. Please install DejaVu font manually.")
|
195 |
+
|
196 |
+
# Create and launch the interface
|
197 |
+
app = create_interface()
|
198 |
+
app.launch()
|