Last active
September 11, 2024 14:31
-
-
Save miladfa7/076bb33d98faf2601690042045d37302 to your computer and use it in GitHub Desktop.
Rotate text (Put rotated text) on image with OpenCV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
improt numpy as np | |
def draw_rotated_text(image: np.ndarray, main_text: str) -> np.ndarray: | |
main_font_face = cv2.FONT_HERSHEY_SIMPLEX | |
main_font_scale = 0.4 | |
main_thickness = 1 | |
main_text_size, _ = cv2.getTextSize(main_text, main_font_face, main_font_scale, main_thickness) | |
main_baseline = main_text_size[1] + main_thickness | |
# Define the position and rotation of the main text | |
main_x = 465 | |
main_y = 770 | |
main_rotation = 15 | |
main_rotation_matrix = cv2.getRotationMatrix2D((main_x, main_y), main_rotation, 1) | |
# Create a black image with the same size as the input image | |
text_img = np.zeros_like(frame) | |
# Add the main text to the text image with rotation | |
cv2.putText(text_img, main_text, (main_x, main_y), main_font_face, 1.5, (0, 0, 0), 2, cv2.LINE_AA) | |
rotated_text_img = cv2.warpAffine(text_img, main_rotation_matrix, (image.shape[1], image.shape[0])) | |
# Overlay the rotated main text on the input image | |
image = cv2.add(image, rotated_text_img) | |
return image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment