In order to correct the perspective (also known as perspective transformation), you will need to create the transformation matrix by making use of the cv2.getPerspectiveTransform() function, where a 3 x 3 matrix is constructed. This function needs four pairs of points (coordinates of a quadrangle in both the source and output image) and calculates a perspective transformation matrix from these points. Then, the M matrix is passed to cv2.warpPerspective(), where the source image is transformed by applying the specified matrix with a specified size, as shown in the following code:
pts_1 = np.float32([[450, 65], [517, 65], [431, 164], [552, 164]])
pts_2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
M = cv2.getPerspectiveTransform(pts_1, pts_2)
dst_image = cv2.warpPerspective(image, M, (300, 300))