summary refs log tree commit diff stats
path: root/opencv/code/a2.py
diff options
context:
space:
mode:
Diffstat (limited to 'opencv/code/a2.py')
-rw-r--r--opencv/code/a2.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/opencv/code/a2.py b/opencv/code/a2.py
new file mode 100644
index 0000000..f0bfc14
--- /dev/null
+++ b/opencv/code/a2.py
@@ -0,0 +1,26 @@
+import cv2
+import numpy as np
+
+image = cv2.imread("1i.png")
+
+angle = float(input("Enter angle in degrees: "))
+
+height, width = image.shape[:2]
+
+rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1)
+
+# Determine the new dimensions of the rotated image
+cos_theta = np.abs(rotation_matrix[0, 0])
+sin_theta = np.abs(rotation_matrix[0, 1])
+new_width = int(height * sin_theta + width * cos_theta)
+new_height = int(height * cos_theta + width * sin_theta)
+
+# Adjust the rotation matrix to take into account translation to keep the entire image within bounds
+rotation_matrix[0, 2] += (new_width - width) / 2
+rotation_matrix[1, 2] += (new_height - height) / 2
+
+rotated_image = cv2.warpAffine(image, rotation_matrix, (new_width, new_height))
+
+cv2.imwrite(f"2.({angle}).png", rotated_image)
+cv2.waitKey(0)
+cv2.destroyAllWindows()