if direction is LEFT: ## vector direction start from the left pointint to the right
m = cv2.getRotationMatrix2D((cx, cy), (-angle)/pi*180., 1.0) ## getRotationMatrix2D takes DEGREE not radian
m_inv = cv2.getRotationMatrix2D((cx, cy), angle/pi*180., 1.0) ## the matrix rotate the image back
else:
m = cv2.getRotationMatrix2D((cx, cy), (pi-angle)/pi*180., 1.0)
m_inv = cv2.getRotationMatrix2D((cx, cy), (angle-pi)/pi*180., 1.0)
rotated = cv2.warpAffine(img, m, (w,h), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
rotate_back = cv2.warpAffine(rotated, m_inv, (w,h), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)