title: How to Close an Image?
body: <p>I'm trying to take an image file, do some stuff to it and save the changes back to the original file.  The problem I'm having is in overwriting the original image; there doesn't seem to be a reliable way to release the handle on <code>filename</code>.</p>
  
  <p>I need this content to be saved back to the same file because external processes depend on that filename to stay the same.</p>
  
  <pre><code>def do_post_processing(filename):
      image = Image.open(str(filename))
      try:
          new_image = optimalimage.trim(image)
      except ValueError as ex:
          # The image is a blank placeholder image.
          new_image = image.copy()
      new_image = optimalimage.rescale(new_image)
      new_image.save('tmp.tif')
      del image
  
      os.remove(str(filename))
      os.rename('tmp.tif', str(filename))
  </code></pre>
  
  <p><code>del image</code> was working until I added the exception handler where I made a copy of the image.  I've also tried accessing a <code>close()</code> attribute of Image and <code>with image</code>, no success.</p>