Spaces:
Runtime error
Runtime error
Commit
·
25db2bc
1
Parent(s):
9429a8b
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
def create_text_image(text, font_size, font_color, background_color):
|
5 |
+
# Create a blank image with white background
|
6 |
+
image = np.ones((100, 300, 3), dtype=np.uint8) * 255
|
7 |
+
|
8 |
+
# Set the font and text properties
|
9 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
10 |
+
font_scale = font_size
|
11 |
+
font_thickness = 2
|
12 |
+
|
13 |
+
# Get the size of the text
|
14 |
+
(text_width, text_height), _ = cv2.getTextSize(text, font, font_scale, font_thickness)
|
15 |
+
|
16 |
+
# Calculate the position to center the text
|
17 |
+
x = (image.shape[1] - text_width) // 2
|
18 |
+
y = (image.shape[0] + text_height) // 2
|
19 |
+
|
20 |
+
# Draw the text on the image
|
21 |
+
cv2.putText(image, text, (x, y), font, font_scale, font_color, font_thickness, cv2.LINE_AA)
|
22 |
+
|
23 |
+
# Change the background color
|
24 |
+
image[np.where((image == [255, 255, 255]).all(axis=2))] = background_color
|
25 |
+
|
26 |
+
return image
|
27 |
+
|
28 |
+
# Example usage
|
29 |
+
text = "Hello, World!"
|
30 |
+
font_size = 2.0
|
31 |
+
font_color = (0, 0, 255) # Red color
|
32 |
+
background_color = (255, 255, 0) # Yellow color
|
33 |
+
|
34 |
+
image = create_text_image(text, font_size, font_color, background_color)
|
35 |
+
|
36 |
+
# Display the image
|
37 |
+
cv2.imshow("Text Image", image)
|
38 |
+
cv2.waitKey(0)
|
39 |
+
cv2.destroyAllWindows()
|