[Python] Is there a way i can detect the image orientation and rotate the image to the right...

Discussão em 'Python' iniciado por Stack, Agosto 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am making a script that repairs scanned documents and i now need a way to detect the image orientation and rotate the image so its rotation is correct.

    Right now my script is unreliable and isn't that precise.

    right now I look for a line and it rotates the first line it sees correctly but this barely works except for a few images

    img_before = cv2.imread('rotated_377.jpg')

    img_gray = cv2.cvtColor(img_before, cv2.COLOR_BGR2GRAY)
    img_edges = cv2.Canny(img_gray, 100, 100, apertureSize=3)
    lines = cv2.HoughLinesP(img_edges, 1, math.pi / 180.0, 100, minLineLength=100, maxLineGap=5)

    angles = []

    for x1,y1,x2,y2 in lines[0]:
    angle = math.degrees(math.atan2(y2 - y1, x2 - x1))
    angles.append(angle)

    median_angle = np.median(angles)
    img_rotated = ndimage.rotate(img_before, median_angle)

    print("Angle is {}".format(median_angle))
    cv2.imwrite('rotated.jpg', img_rotated)


    I want to make a script that gets an image like this one(don't mind the image its for testing purposes) [​IMG]

    and rotates it in the right way so I get a correctly orientated image.

    Continue reading...

Compartilhe esta Página