title: Trim whitespace using PIL
body: <p>Is there a simple solution to trim whitespace on the image in PIL?</p>
  <p>ImageMagick has easy support for it in the following way:</p>
  <pre><code>convert test.jpeg -fuzz 7% -trim test_trimmed.jpeg
  </code></pre>
  <p>I found a solution for PIL:</p>
  <pre><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)
  </code></pre>
  <p>But this solution has disadvantages:</p>
  <ol>
  <li>I need to define <code>border</code> color, it is not a big deal for me, my images has a white background</li>
  <li>And the most disadvantage, This PIL solution doesn't support ImageMagick's <code>-fuzz</code> key. To add some fuzzy cropping. as I can have some jpeg compression artifacts and unneeded huge shadows.</li>
  </ol>
  <p>Maybe PIL has some built-in functions for it?
  Or there is some fast solution?</p>