Created
August 17, 2014 23:26
-
-
Save tribela/b6d49f612ad1e6d88636 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
from PIL import Image | |
def dhash(image, hash_size=8): | |
image = image.convert('L').resize( | |
(hash_size + 1, hash_size), | |
Image.ANTIALIAS) | |
diff = [] | |
for row in xrange(hash_size): | |
for col in xrange(hash_size): | |
pixel_left = image.getpixel((col, row)) | |
pixel_right = image.getpixel((col + 1, row)) | |
diff.append(pixel_left > pixel_right) | |
decimal_value = 0 | |
hex_string = [] | |
for index, value in enumerate(diff): | |
if value: | |
decimal_value += 2**(index % 8) | |
if index % 8 == 7: | |
hex_string.append(hex(decimal_value)[2:].rjust(2, '0')) | |
decimal_value = 0 | |
image.close() | |
return ''.join(hex_string) | |
if __name__ == '__main__': | |
basedir = sys.argv[1] | |
images = [os.path.join(base, name) | |
for base, dirs, names in os.walk(basedir) | |
for name in names | |
if os.path.splitext(name)[1].lower() in ('.jpg', '.png')] | |
hashmap = {} | |
for filename in images: | |
image = Image.open(filename) | |
dh = dhash(image) | |
space = hashmap.get(dh, []) | |
space.append(filename) | |
hashmap[dh] = space | |
image.close() | |
print('\n\n'.join( | |
'\n'.join(names) for names in hashmap.values() | |
if len(names) > 1 | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment