title: How to detect a specific border detail of an image and crop according to it using python and PIL?
body: <p>I'm currently trying to crop an image by detecting the 2 big black corners on the upper-right and lower-left pf the document: <a href="http://www.suiteexpert.fr/AideProd/SalaireExpert/Images/Editions/aemapercu.png" rel="nofollow noreferrer">http://www.suiteexpert.fr/AideProd/SalaireExpert/Images/Editions/aemapercu.png</a>
  I'm trying to find a simpler method than deep-learning OCR for better performance. </p>
  
  <p>I have tried a few different things such as detecting a change in color, by starting along the edges of the document and detecting a change from white to black. This seems to work but it first detects the very small vertical text on some of the image examples: <a href="https://www.movinmotion.com/wp-content/uploads/2018/03/AEM-V5-MM-.jpg" rel="nofollow noreferrer">https://www.movinmotion.com/wp-content/uploads/2018/03/AEM-V5-MM-.jpg</a></p>
  
  <p>This also leeds to other problems due to the fact that the documents I want to analyze could also be in different colors, and could also be scans or photographs. </p>
  
  <p>However, these documents will always have the same internal shape and structure (it's a standardized administrative document).</p>
  
  <p>I would want some of your feedback as to how I could detect the 2 big black corners on the upper right and lower left corners of the document, which would allow me process and normalize the image by rotating and cropping it.</p>
  
  <pre class="lang-py prettyprint-override"><code>from PIL import Image, ImageChops
  
  def trim(im, border):
       bg = Image.new(im.mode, im.size, border)
       diff = ImageChops.difference(im, bg)
       bbox = diff.getbbox()
       if bbox:
           return im.crop(bbox)
       else:
           # found no content
           raise ValueError("cannot trim; image was empty")
  </code></pre>
  
  <p>Thank you in advance for your help!</p>