Rotate an image with text
I have an image as shown I need to convert this image to correct orientation. How can achieve this? This is the code i wrote,
image = cv2.imread("borderRemoved.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bitwise_not(gray)
thresh = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
coords = np.column_stack(np.where(thresh > 0))
angle = cv2.minAreaRect(coords)[-1]
if angle < -45:
angle = -(90 + angle)
else:
angle = -angle
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(image, M, (w, h),flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
cv2.imwrite("Rotated.png",rotated)
Thanks in advance.
how will you find out, which way to rotate it ?
That's the solution i needed.
maybe a simple cv2.rotate() ?
No its not possible. Which angle should i take to rotate this image? I need a general solution for rotating an image
yea, that was my question, before.