title: Detecting a horizontal line in an image
body: <p><strong>Problem:</strong>
  I'm working with a dataset that contains many images that look something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/Rio8gm.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/Rio8gm.jpg" alt="img2"></a>
  <a href="https://i.stack.imgur.com/k4D6Jm.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/k4D6Jm.jpg" alt="img1"></a></p>
  
  <p>Now I need all these images to be oriented horizontally or vertically, such that the color palette is either at the bottom or the right side of the image. This can be done by simply rotating the image, but the tricky part is figuring out which images should be rotated and which shouldn't.</p>
  
  <p><strong>What I have tried:</strong></p>
  
  <p>I thought that the best way to do this, is by detecting the white line that separates the the color palette from the image. I decided to rotate all images that have the palette at the bottom such that they have it at the right side.</p>
  
  <pre><code># yes I am mixing between PIL and opencv (I like the PIL resizing more)
  # resize image to be 128 by 128 pixels
  img = img.resize((128, 128), PIL.Image.BILINEAR)
  img = np.array(img)
  
  # perform edge detection, not sure if these are the best parameters for Canny
  edges = cv2.Canny(img, 30, 50, 3, apertureSize=3)
  
  has_line = 0
  
  # take numpy slice of the area where the white line usually is 
  # (not always exactly in the same spot which probably has to do with the way I resize my image) 
  for line in edges[75:80]:
  
      # check if most of one of the lines contains white pixels
      counts = np.bincount(line)
      if np.argmax(counts) == 255:
          has_line = True
  
  # rotate if we found such a line
  if has_line == True:
      s = np.rot90(s)
  
  </code></pre>
  
  <p><strong>An example of it working correctly:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/FpRsY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/FpRsY.png" alt="enter image description here"></a></p>
  
  <p><strong>An example of it working incorrectly:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/2WRsY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/2WRsY.png" alt="enter image description here"></a></p>
  
  <p>This works maybe on 98% of images but there are some cases where it will rotate images that shouldn't be rotated or not rotate images that should be rotated. Maybe there is an easier way to do this, or maybe a more elaborate way that is more consistent? I could do it manually but I'm dealing with a lot of images. Thanks for any help and/or comments.</p>
  
  <p><strong>Here are some images where my code fails for testing purposes:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/TKbuEm.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/TKbuEm.jpg" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/oHwr6m.png" rel="noreferrer"><img src="https://i.stack.imgur.com/oHwr6m.png" alt="enter image description here"></a></p>