Spaces:
Runtime error
Runtime error
| import cv2 | |
| import numpy as np | |
| def create_text_image(text, font_size, font_color, background_color): | |
| # Create a blank image with white background | |
| image = np.ones((100, 300, 3), dtype=np.uint8) * 255 | |
| # Set the font and text properties | |
| font = cv2.FONT_HERSHEY_SIMPLEX | |
| font_scale = font_size | |
| font_thickness = 2 | |
| # Get the size of the text | |
| (text_width, text_height), _ = cv2.getTextSize(text, font, font_scale, font_thickness) | |
| # Calculate the position to center the text | |
| x = (image.shape[1] - text_width) // 2 | |
| y = (image.shape[0] + text_height) // 2 | |
| # Draw the text on the image | |
| cv2.putText(image, text, (x, y), font, font_scale, font_color, font_thickness, cv2.LINE_AA) | |
| # Change the background color | |
| image[np.where((image == [255, 255, 255]).all(axis=2))] = background_color | |
| return image | |
| # Example usage | |
| text = "Hello, World!" | |
| font_size = 2.0 | |
| font_color = (0, 0, 255) # Red color | |
| background_color = (255, 255, 0) # Yellow color | |
| image = create_text_image(text, font_size, font_color, background_color) | |
| # Display the image | |
| cv2.imshow("Text Image", image) | |
| cv2.waitKey(0) | |
| cv2.destroyAllWindows() |