Update app.py
Browse files
app.py
CHANGED
@@ -15,11 +15,14 @@ COLOR_MAP = {
|
|
15 |
}
|
16 |
|
17 |
# Define the function to add text to the image
|
18 |
-
def add_text_to_image(image, text, text_color, text_size,
|
19 |
# Convert the image to a PIL Image object if it's not already
|
20 |
if isinstance(image, np.ndarray):
|
21 |
image = Image.fromarray(image.astype('uint8'), 'RGB')
|
22 |
|
|
|
|
|
|
|
23 |
# Create a drawing context
|
24 |
draw = ImageDraw.Draw(image)
|
25 |
|
@@ -33,6 +36,17 @@ def add_text_to_image(image, text, text_color, text_size, text_position_x, text_
|
|
33 |
# Get the RGB color from the COLOR_MAP
|
34 |
color_rgb = COLOR_MAP.get(text_color, (255, 255, 255)) # Default to white if color not found
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
# Add the text to the image
|
37 |
draw.text((text_position_x, text_position_y), text, fill=color_rgb, font=font)
|
38 |
|
@@ -47,13 +61,14 @@ iface = gr.Interface(
|
|
47 |
gr.Textbox(label="Text"),
|
48 |
gr.Dropdown(label="Text Color", choices=list(COLOR_MAP.keys()), value="white"),
|
49 |
gr.Slider(minimum=10, maximum=100, value=30, label="Text Size"),
|
50 |
-
gr.Slider(minimum=
|
51 |
-
gr.Slider(minimum=
|
|
|
52 |
gr.Textbox(label="Font (e.g., 'arial.ttf')", value="arial.ttf")
|
53 |
],
|
54 |
outputs=gr.Image(label="Output Image"),
|
55 |
title="Add Text to Image",
|
56 |
-
description="Upload an image and add customizable text
|
57 |
)
|
58 |
|
59 |
# Launch the Gradio app
|
|
|
15 |
}
|
16 |
|
17 |
# Define the function to add text to the image
|
18 |
+
def add_text_to_image(image, text, text_color, text_size, width, height, text_align, font):
|
19 |
# Convert the image to a PIL Image object if it's not already
|
20 |
if isinstance(image, np.ndarray):
|
21 |
image = Image.fromarray(image.astype('uint8'), 'RGB')
|
22 |
|
23 |
+
# Resize the image to the specified width and height
|
24 |
+
image = image.resize((width, height))
|
25 |
+
|
26 |
# Create a drawing context
|
27 |
draw = ImageDraw.Draw(image)
|
28 |
|
|
|
36 |
# Get the RGB color from the COLOR_MAP
|
37 |
color_rgb = COLOR_MAP.get(text_color, (255, 255, 255)) # Default to white if color not found
|
38 |
|
39 |
+
# Calculate text position based on alignment
|
40 |
+
text_width, text_height = draw.textsize(text, font=font)
|
41 |
+
if text_align == "left":
|
42 |
+
text_position_x = 10 # 10 pixels from the left edge
|
43 |
+
elif text_align == "right":
|
44 |
+
text_position_x = width - text_width - 10 # 10 pixels from the right edge
|
45 |
+
elif text_align == "center":
|
46 |
+
text_position_x = (width - text_width) // 2 # Centered horizontally
|
47 |
+
|
48 |
+
text_position_y = (height - text_height) // 2 # Centered vertically by default
|
49 |
+
|
50 |
# Add the text to the image
|
51 |
draw.text((text_position_x, text_position_y), text, fill=color_rgb, font=font)
|
52 |
|
|
|
61 |
gr.Textbox(label="Text"),
|
62 |
gr.Dropdown(label="Text Color", choices=list(COLOR_MAP.keys()), value="white"),
|
63 |
gr.Slider(minimum=10, maximum=100, value=30, label="Text Size"),
|
64 |
+
gr.Slider(minimum=100, maximum=1000, value=500, label="Width"),
|
65 |
+
gr.Slider(minimum=100, maximum=1000, value=500, label="Height"),
|
66 |
+
gr.Dropdown(label="Text Alignment", choices=["left", "right", "center"], value="center"),
|
67 |
gr.Textbox(label="Font (e.g., 'arial.ttf')", value="arial.ttf")
|
68 |
],
|
69 |
outputs=gr.Image(label="Output Image"),
|
70 |
title="Add Text to Image",
|
71 |
+
description="Upload an image, resize it, and add customizable text with alignment options."
|
72 |
)
|
73 |
|
74 |
# Launch the Gradio app
|