Spaces:
Sleeping
Sleeping
update app.py - color conversion
Browse files
app.py
CHANGED
|
@@ -144,12 +144,45 @@ def add_text_to_image(img, pattern, line1, line2, line3, font_size, color, add_o
|
|
| 144 |
|
| 145 |
print(f"Text positions calculated: {positions}")
|
| 146 |
|
| 147 |
-
# FIXED:
|
| 148 |
original_color = color
|
| 149 |
final_color = (255, 255, 255) # Default white
|
| 150 |
|
| 151 |
if isinstance(color, str):
|
| 152 |
-
if color.startswith('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
print(f"Converting hex color {color}")
|
| 154 |
hex_color = color.lstrip('#')
|
| 155 |
if len(hex_color) == 6:
|
|
@@ -160,7 +193,6 @@ def add_text_to_image(img, pattern, line1, line2, line3, font_size, color, add_o
|
|
| 160 |
print(f"Invalid hex color {color}: {e}, using white")
|
| 161 |
final_color = (255, 255, 255)
|
| 162 |
elif len(hex_color) == 3:
|
| 163 |
-
# Short hex format #RGB -> #RRGGBB
|
| 164 |
try:
|
| 165 |
final_color = tuple(int(hex_color[i]*2, 16) for i in range(3))
|
| 166 |
print(f"Successfully converted short hex {color} to RGB: {final_color}")
|
|
@@ -171,11 +203,11 @@ def add_text_to_image(img, pattern, line1, line2, line3, font_size, color, add_o
|
|
| 171 |
print(f"Invalid hex color length {color}, using white")
|
| 172 |
final_color = (255, 255, 255)
|
| 173 |
else:
|
| 174 |
-
# Try to
|
| 175 |
-
print(f"
|
| 176 |
final_color = color # Let PIL try to handle it
|
| 177 |
elif isinstance(color, (tuple, list)) and len(color) >= 3:
|
| 178 |
-
final_color = tuple(color[:3])
|
| 179 |
print(f"Using tuple/list color: {final_color}")
|
| 180 |
else:
|
| 181 |
print(f"Unrecognized color format: {color}, using white")
|
|
@@ -215,10 +247,10 @@ def add_text_to_image(img, pattern, line1, line2, line3, font_size, color, add_o
|
|
| 215 |
else:
|
| 216 |
print("Skipping outline (disabled)")
|
| 217 |
|
| 218 |
-
# Draw main text
|
| 219 |
try:
|
| 220 |
draw.text((x, y), txt, fill=final_color, font=font, anchor="mm")
|
| 221 |
-
print(f"Main text drawn with color {final_color} at ({x}, {y})")
|
| 222 |
except TypeError:
|
| 223 |
# Fallback for older PIL versions
|
| 224 |
try:
|
|
@@ -228,27 +260,28 @@ def add_text_to_image(img, pattern, line1, line2, line3, font_size, color, add_o
|
|
| 228 |
fallback_x = x - text_w//2
|
| 229 |
fallback_y = y - text_h//2
|
| 230 |
draw.text((fallback_x, fallback_y), txt, fill=final_color, font=font)
|
| 231 |
-
print(f"Fallback text drawn with color {final_color} at ({fallback_x}, {fallback_y})")
|
| 232 |
-
except Exception:
|
|
|
|
| 233 |
draw.text((x, y), txt, fill=final_color, font=font)
|
| 234 |
-
print(f"Basic text drawn with color {final_color} at ({x}, {y})")
|
| 235 |
except Exception as e:
|
| 236 |
-
print(f"ERROR drawing main text: {e}")
|
| 237 |
-
#
|
| 238 |
try:
|
| 239 |
-
draw.text((x, y), txt, fill=
|
| 240 |
-
print(f"Drew text
|
| 241 |
except Exception as e2:
|
| 242 |
-
print(f"Even
|
| 243 |
|
| 244 |
text_drawn = True
|
| 245 |
-
print(f"
|
| 246 |
|
| 247 |
if not text_drawn:
|
| 248 |
print("No text was drawn")
|
| 249 |
return img, "No text to add (all lines were empty)"
|
| 250 |
|
| 251 |
-
print("=== Text drawing completed ===")
|
| 252 |
return image, f"Text added! Color: {original_color} -> {final_color}, Outline: {add_outline}"
|
| 253 |
|
| 254 |
except Exception as e:
|
|
|
|
| 144 |
|
| 145 |
print(f"Text positions calculated: {positions}")
|
| 146 |
|
| 147 |
+
# FIXED: Parse RGBA string from Gradio ColorPicker
|
| 148 |
original_color = color
|
| 149 |
final_color = (255, 255, 255) # Default white
|
| 150 |
|
| 151 |
if isinstance(color, str):
|
| 152 |
+
if color.startswith('rgba(') and color.endswith(')'):
|
| 153 |
+
# Parse RGBA string: 'rgba(253.2203125, 3.331846217105286, 3.331846217105286, 1)'
|
| 154 |
+
print(f"Parsing RGBA string: {color}")
|
| 155 |
+
try:
|
| 156 |
+
# Extract numbers from the string
|
| 157 |
+
rgba_part = color[5:-1] # Remove 'rgba(' and ')'
|
| 158 |
+
values = [float(x.strip()) for x in rgba_part.split(',')]
|
| 159 |
+
if len(values) >= 3:
|
| 160 |
+
# Convert to integers and take only RGB (ignore alpha)
|
| 161 |
+
final_color = (int(round(values[0])), int(round(values[1])), int(round(values[2])))
|
| 162 |
+
print(f"Successfully parsed RGBA {color} to RGB: {final_color}")
|
| 163 |
+
else:
|
| 164 |
+
print(f"Invalid RGBA format {color}, using white")
|
| 165 |
+
final_color = (255, 255, 255)
|
| 166 |
+
except (ValueError, IndexError) as e:
|
| 167 |
+
print(f"Error parsing RGBA {color}: {e}, using white")
|
| 168 |
+
final_color = (255, 255, 255)
|
| 169 |
+
elif color.startswith('rgb(') and color.endswith(')'):
|
| 170 |
+
# Parse RGB string: 'rgb(253, 3, 3)'
|
| 171 |
+
print(f"Parsing RGB string: {color}")
|
| 172 |
+
try:
|
| 173 |
+
rgb_part = color[4:-1] # Remove 'rgb(' and ')'
|
| 174 |
+
values = [float(x.strip()) for x in rgb_part.split(',')]
|
| 175 |
+
if len(values) >= 3:
|
| 176 |
+
final_color = (int(round(values[0])), int(round(values[1])), int(round(values[2])))
|
| 177 |
+
print(f"Successfully parsed RGB {color} to RGB: {final_color}")
|
| 178 |
+
else:
|
| 179 |
+
print(f"Invalid RGB format {color}, using white")
|
| 180 |
+
final_color = (255, 255, 255)
|
| 181 |
+
except (ValueError, IndexError) as e:
|
| 182 |
+
print(f"Error parsing RGB {color}: {e}, using white")
|
| 183 |
+
final_color = (255, 255, 255)
|
| 184 |
+
elif color.startswith('#'):
|
| 185 |
+
# Parse hex color
|
| 186 |
print(f"Converting hex color {color}")
|
| 187 |
hex_color = color.lstrip('#')
|
| 188 |
if len(hex_color) == 6:
|
|
|
|
| 193 |
print(f"Invalid hex color {color}: {e}, using white")
|
| 194 |
final_color = (255, 255, 255)
|
| 195 |
elif len(hex_color) == 3:
|
|
|
|
| 196 |
try:
|
| 197 |
final_color = tuple(int(hex_color[i]*2, 16) for i in range(3))
|
| 198 |
print(f"Successfully converted short hex {color} to RGB: {final_color}")
|
|
|
|
| 203 |
print(f"Invalid hex color length {color}, using white")
|
| 204 |
final_color = (255, 255, 255)
|
| 205 |
else:
|
| 206 |
+
# Try to use as named color
|
| 207 |
+
print(f"Attempting to use named color: {color}")
|
| 208 |
final_color = color # Let PIL try to handle it
|
| 209 |
elif isinstance(color, (tuple, list)) and len(color) >= 3:
|
| 210 |
+
final_color = tuple(int(round(x)) for x in color[:3])
|
| 211 |
print(f"Using tuple/list color: {final_color}")
|
| 212 |
else:
|
| 213 |
print(f"Unrecognized color format: {color}, using white")
|
|
|
|
| 247 |
else:
|
| 248 |
print("Skipping outline (disabled)")
|
| 249 |
|
| 250 |
+
# Draw main text with the properly parsed color
|
| 251 |
try:
|
| 252 |
draw.text((x, y), txt, fill=final_color, font=font, anchor="mm")
|
| 253 |
+
print(f"SUCCESS: Main text drawn with color {final_color} at ({x}, {y})")
|
| 254 |
except TypeError:
|
| 255 |
# Fallback for older PIL versions
|
| 256 |
try:
|
|
|
|
| 260 |
fallback_x = x - text_w//2
|
| 261 |
fallback_y = y - text_h//2
|
| 262 |
draw.text((fallback_x, fallback_y), txt, fill=final_color, font=font)
|
| 263 |
+
print(f"SUCCESS: Fallback text drawn with color {final_color} at ({fallback_x}, {fallback_y})")
|
| 264 |
+
except Exception as e:
|
| 265 |
+
print(f"Fallback failed: {e}, trying basic draw")
|
| 266 |
draw.text((x, y), txt, fill=final_color, font=font)
|
| 267 |
+
print(f"SUCCESS: Basic text drawn with color {final_color} at ({x}, {y})")
|
| 268 |
except Exception as e:
|
| 269 |
+
print(f"ERROR drawing main text with final_color: {e}")
|
| 270 |
+
# Last resort - try with white
|
| 271 |
try:
|
| 272 |
+
draw.text((x, y), txt, fill=(255, 255, 255), font=font, anchor="mm")
|
| 273 |
+
print(f"FALLBACK: Drew text in white")
|
| 274 |
except Exception as e2:
|
| 275 |
+
print(f"Even white text failed: {e2}")
|
| 276 |
|
| 277 |
text_drawn = True
|
| 278 |
+
print(f"Completed drawing line {i+1}")
|
| 279 |
|
| 280 |
if not text_drawn:
|
| 281 |
print("No text was drawn")
|
| 282 |
return img, "No text to add (all lines were empty)"
|
| 283 |
|
| 284 |
+
print("=== Text drawing completed successfully ===")
|
| 285 |
return image, f"Text added! Color: {original_color} -> {final_color}, Outline: {add_outline}"
|
| 286 |
|
| 287 |
except Exception as e:
|