title: Compare source of multiple jpegs in python
body: <p>I have a folder with 78 jpeg images in it, of which only 48 are unique. I would like to iterate over the files in this folder, with Python, and have it print out a list of each file name so long as the image is not a duplicate. </p>
  
  <p>More specifically, it will print out an array that I can copy and paste into a PHP script for use.</p>
  
  <p>My idea is to, as I iterate over the files, save the md5 of the source of the file into an array. If the hash is already in the array, however, it will ignore the file and move on.</p>
  
  <p>Here is my script:</p>
  
  <pre><code>from os import listdir
  from os import path
  from hashlib import md5
  
  m = md5()
  hashes = []
  d = path.join("Z:\\mydir")
  files = listdir(d);
  i = 1;
  
  print " array("
  
  for file in files:
  
      src = "Z:\\mydir\\%s" % file
      src = open(src)
      src = src.read()
  
      m.update(src)
      hash = m.digest()
  
      if(hash not in hashes):
  
          hashes.append(hash)
  
          if(i == len(files)):
              print "\"%s\"" % file
          else:
              print "\"%s\"," % file
      i += 1
  
  print ");"
  </code></pre>
  
  <p>This is printing out every image though! </p>
  
  <p>I thought that the issue might be because of the metadata at the beginning of the jpeg file, so I have tried to read the last half of the source only:</p>
  
  <pre><code>halfsrclen = len(src) / 2
  
  m.update(src[halfsrclen:])
  hash = m.digest()
  </code></pre>
  
  <p>But this is still returning each file!</p>
  
  <p>I am probably doing something very wrong but I have no idea what :(</p>