title: Is there a way i can detect the image orientation and rotate the image to the right angle?
body: <p>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.</p>
  
  <p>Right now my script is unreliable and isn't that precise. </p>
  
  <p>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</p>
  
  <pre class="lang-py prettyprint-override"><code>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)
  </code></pre>
  
  <p>I want to make a script that gets an image like this one(don't mind the image its for testing purposes) 
  <a href="https://i.stack.imgur.com/i6rqL.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/i6rqL.jpg" alt="rotated image"></a></p>
  
  <p>and rotates it in the right way so I get a correctly orientated image.</p>