blind1234 commited on
Commit
9ec6468
·
verified ·
1 Parent(s): 8bffbad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -37
app.py CHANGED
@@ -16,42 +16,65 @@ COLOR_MAP = {
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
-
29
- # Load the font (you can specify a path to a .ttf file if needed)
30
  try:
31
- font = ImageFont.truetype(font, text_size)
32
- except IOError:
33
- # Fallback to a default font if the specified font is not available
34
- font = ImageFont.load_default()
35
-
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
-
53
- # Convert the image back to a numpy array for Gradio compatibility
54
- return np.array(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  # Gradio interface
57
  iface = gr.Interface(
@@ -61,8 +84,8 @@ iface = gr.Interface(
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
  ],
 
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
  try:
20
+ # Convert the image to a PIL Image object if it's not already
21
+ if isinstance(image, np.ndarray):
22
+ image = Image.fromarray(image.astype('uint8'), 'RGB')
23
+
24
+ # Resize the image to the specified width and height while maintaining aspect ratio
25
+ original_width, original_height = image.size
26
+ aspect_ratio = original_width / original_height
27
+
28
+ # Calculate new dimensions while maintaining aspect ratio
29
+ if width / height > aspect_ratio:
30
+ new_width = int(height * aspect_ratio)
31
+ new_height = height
32
+ else:
33
+ new_width = width
34
+ new_height = int(width / aspect_ratio)
35
+
36
+ # Resize the image
37
+ image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
38
+
39
+ # Create a new blank image with the specified width and height
40
+ new_image = Image.new("RGB", (width, height), (255, 255, 255)) # White background
41
+ # Paste the resized image onto the new image, centered
42
+ new_image.paste(image, ((width - new_width) // 2, (height - new_height) // 2))
43
+
44
+ # Create a drawing context
45
+ draw = ImageDraw.Draw(new_image)
46
+
47
+ # Load the font (you can specify a path to a .ttf file if needed)
48
+ try:
49
+ font = ImageFont.truetype(font, text_size)
50
+ except IOError:
51
+ # Fallback to a default font if the specified font is not available
52
+ font = ImageFont.load_default()
53
+
54
+ # Get the RGB color from the COLOR_MAP
55
+ color_rgb = COLOR_MAP.get(text_color, (255, 255, 255)) # Default to white if color not found
56
+
57
+ # Calculate text position based on alignment
58
+ text_width, text_height = draw.textsize(text, font=font)
59
+ if text_align == "left":
60
+ text_position_x = 10 # 10 pixels from the left edge
61
+ elif text_align == "right":
62
+ text_position_x = width - text_width - 10 # 10 pixels from the right edge
63
+ elif text_align == "center":
64
+ text_position_x = (width - text_width) // 2 # Centered horizontally
65
+
66
+ text_position_y = (height - text_height) // 2 # Centered vertically by default
67
+
68
+ # Add the text to the image
69
+ draw.text((text_position_x, text_position_y), text, fill=color_rgb, font=font)
70
+
71
+ # Convert the image back to a numpy array for Gradio compatibility
72
+ return np.array(new_image)
73
+
74
+ except Exception as e:
75
+ # Handle any errors and display a message
76
+ print(f"Error: {e}")
77
+ return None
78
 
79
  # Gradio interface
80
  iface = gr.Interface(
 
84
  gr.Textbox(label="Text"),
85
  gr.Dropdown(label="Text Color", choices=list(COLOR_MAP.keys()), value="white"),
86
  gr.Slider(minimum=10, maximum=100, value=30, label="Text Size"),
87
+ gr.Slider(minimum=100, maximum=2048, value=1024, label="Width"),
88
+ gr.Slider(minimum=100, maximum=2048, value=1024, label="Height"),
89
  gr.Dropdown(label="Text Alignment", choices=["left", "right", "center"], value="center"),
90
  gr.Textbox(label="Font (e.g., 'arial.ttf')", value="arial.ttf")
91
  ],