title: Automatically cropping an image with python/PIL
body: <p>Can anyone help me figure out what's happening in my image auto-cropping script? I have a png image with a large transparent area/space.  I would like to be able to automatically crop that space out and leave the essentials.  Original image has a squared canvas, optimally it would be rectangular, encapsulating just the molecule.</p>
  
  <p>here's the original image:
  <img src="https://i.stack.imgur.com/AsypD.png" alt="Original Image"></p>
  
  <p>Doing some googling i came across PIL/python code that was reported to work, however in my hands, running the code below over-crops the image.</p>
  
  <pre><code>import Image
  import sys
  
  image=Image.open('L_2d.png')
  image.load()
  
  imageSize = image.size
  imageBox = image.getbbox()
  
  imageComponents = image.split()
  
  rgbImage = Image.new("RGB", imageSize, (0,0,0))
  rgbImage.paste(image, mask=imageComponents[3])
  croppedBox = rgbImage.getbbox()
  print imageBox
  print croppedBox
  if imageBox != croppedBox:
      cropped=image.crop(croppedBox)
      print 'L_2d.png:', "Size:", imageSize, "New Size:",croppedBox
      cropped.save('L_2d_cropped.png')
  </code></pre>
  
  <p>the output is this:<img src="https://i.stack.imgur.com/6Y5r6.png" alt="script&#39;s output"></p>
  
  <p>Can anyone more familiar with image-processing/PLI can help me figure out the issue?</p>