title: Variation in MD5sum for StringIO object vs saved file?
body: <p>I have a piece of python code, as follows:</p>
  
  <pre><code>def make_portrait(self, image_name):
  
      im = Image.open(str(image_name))
  
      output = StringIO.StringIO()
      im.save(output, 'JPEG', quality=70)
      self.portrait_md5 = hashlib.md5(output.getvalue()).hexdigest()
      output.close()
      self.portrait_asset = self.portrait_md5 + '.jpg'
      self.portrait_xml = '&lt;net.rptools.maptool.model.Asset&gt;\n  &lt;id&gt;\n    &lt;id&gt;' + self.portrait_md5 + \
                          '&lt;/id&gt;\n  &lt;/id&gt;\n  &lt;name&gt;' + self.name + \
                          '&lt;/name&gt;\n  &lt;extension&gt;jpg&lt;/extension&gt;\n  &lt;image/&gt;\n&lt;/net.rptools.maptool.model.Asset&gt;\n'
      self.portrait = im
  </code></pre>
  
  <p>Its job is to read in a given image file, convert the image to JPG format at 70% quality, then calculate an MD5 checksum for the converted data.  Elsewhere in the program, the converted image data gets written out to an actual file on the disk, with an accompanying file containing metadata about it (such as the MD5 sum).</p>
  
  <p>The code runs fine, but the check sums don't match the final output.  Suppose I feed it an image, it does the conversion, and comes up with "ce5d1126ba52ba1618c402a13bee1c0c" as the check sum.  If I later go examine the final file that gets saved to disk, md5sum says it has the check sum "bb5ae2fdfea6294267b7ffdc226e21fa" instead.</p>
  
  <p>This is causing problems because the program that's supposed to use the file this code is generating checks the MD5 sum of the actual file and compares it to the MD5 sum reported in the metadata.  If the two don't match, it chokes.</p>
  
  <p>I'm very new to Python.  How do I make it generate an MD5 sum that will match that of the image file that gets saved to disk?</p>
  
  <p>Happy to provide additional details if needed.</p>