#!/opt/local/bin/python2.7
from collections import defaultdict
import imghdr
import os
import sys

import pHash

# Find Duplicates Simple
# Attempt to find images by looking for exact hash collisions.
# This method is not very sophisticated and does not work all
# that well.

def main():

    img_map = defaultdict(list)

    # Make sure at least one command line argument was provided
    if len(sys.argv) < 2:
        print 'Usage: find_duplicate_simple <path>'
        return -1;

    # Print out the path we're walking
    path = sys.argv[1]
    print 'Step 1: Walk path: {:s}\n'.format(path)

    # Walk the root path
    for root, dirs, files in os.walk(path):
        for file in files:
            img_path = os.path.join(root, file)
            # Check the make sure the file is an actual image
            if not imghdr.what(img_path):
                continue
            img_hash = pHash.imagehash(img_path)
            print '{:s} -> {:016X}'.format(img_path, img_hash)
            img_map[img_hash].append(img_path)


    print '\n\nStep 2: find duplicates\n'

    # Now look through the image hash dictionary for matches
    for img_hash, files in img_map.items():
        if len(files) > 2:
            print '{:016X}'.format(img_hash)
            for file_path in files:
                print '\t{:s}'.format(file_path)


if __name__ == '__main__':
    sys.exit(main())