title: Compute the hash of only the core image data of a TIFF
body: <p>Using Python, how can I compute the md5 hash of a TIFF image, excluding all metadata?</p>
  
  <p>With a JPG, I do something like this:</p>
  
  <pre><code>def jpeg(fh):
    hash = hashlib.md5()
    assert fh.read(2) == "\xff\xd8"
    while True:
        marker,length = struct.unpack("&gt;2H", fh.read(4))
        assert marker &amp; 0xff00 == 0xff00
        if marker == 0xFFDA: # Start of stream
            hash.update(fh.read())
            break
        else:
            fh.seek(length-2, os.SEEK_CUR)
    print "Hash: %r" % hash.hexdigest()
  
  &gt;&gt; jpeg(file("test.jpg"))
  &gt;&gt; Hash: 'debb4956941795d6ef48717e4c9cc433'
  </code></pre>
  
  <p>Not sure how to extend this to TIFF images.</p>
  
  <p>It seems trickier with TIFFs because the location of the metadata within the image can change (it's not always at the beginning or end).</p>