blind1234 commited on
Commit
b8ceb17
·
verified ·
1 Parent(s): 2da0dea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -3
app.py CHANGED
@@ -1,8 +1,19 @@
1
  import gradio as gr
2
  import numpy as np
3
- import matplotlib.pyplot as plt
4
  from PIL import Image, ImageDraw, ImageFont
5
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  # Define the function to add text to the image
7
  def add_text_to_image(image, text, text_color, text_size, text_position_x, text_position_y, font):
8
  # Convert the image to a PIL Image object if it's not already
@@ -19,8 +30,11 @@ def add_text_to_image(image, text, text_color, text_size, text_position_x, text_
19
  # Fallback to a default font if the specified font is not available
20
  font = ImageFont.load_default()
21
 
 
 
 
22
  # Add the text to the image
23
- draw.text((text_position_x, text_position_y), text, fill=text_color, font=font)
24
 
25
  # Convert the image back to a numpy array for Gradio compatibility
26
  return np.array(image)
@@ -31,7 +45,7 @@ iface = gr.Interface(
31
  inputs=[
32
  gr.Image(label="Input Image"),
33
  gr.Textbox(label="Text"),
34
- gr.ColorPicker(label="Text Color", value="#FFFFFF"),
35
  gr.Slider(minimum=10, maximum=100, value=30, label="Text Size"),
36
  gr.Slider(minimum=0, maximum=500, value=50, label="Text Position X"),
37
  gr.Slider(minimum=0, maximum=500, value=50, label="Text Position Y"),
 
1
  import gradio as gr
2
  import numpy as np
 
3
  from PIL import Image, ImageDraw, ImageFont
4
 
5
+ # Define a dictionary to map color names to their RGB values
6
+ COLOR_MAP = {
7
+ "white": (255, 255, 255),
8
+ "black": (0, 0, 0),
9
+ "red": (255, 0, 0),
10
+ "green": (0, 255, 0),
11
+ "blue": (0, 0, 255),
12
+ "yellow": (255, 255, 0),
13
+ "cyan": (0, 255, 255),
14
+ "magenta": (255, 0, 255),
15
+ }
16
+
17
  # Define the function to add text to the image
18
  def add_text_to_image(image, text, text_color, text_size, text_position_x, text_position_y, font):
19
  # Convert the image to a PIL Image object if it's not already
 
30
  # Fallback to a default font if the specified font is not available
31
  font = ImageFont.load_default()
32
 
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
 
39
  # Convert the image back to a numpy array for Gradio compatibility
40
  return np.array(image)
 
45
  inputs=[
46
  gr.Image(label="Input Image"),
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=0, maximum=500, value=50, label="Text Position X"),
51
  gr.Slider(minimum=0, maximum=500, value=50, label="Text Position Y"),