Skip to content

Commit

Permalink
fixed up doc strings and layout
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesBuchner committed Jun 13, 2015
1 parent 8f45896 commit fc59b24
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013, Johannes Buchner
Copyright (c) 2013-2015, Johannes Buchner
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Expand Down
77 changes: 41 additions & 36 deletions imagehash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
>>> for r in range(1, 30, 5):
... rothash = imagehash.average_hash(Image.open('test.png').rotate(r))
... print('Rotation by %d: %d Hamming difference' % (r, hash - rothash))
...
...
Rotation by 1: 2 Hamming difference
Rotation by 6: 11 Hamming difference
Rotation by 11: 13 Hamming difference
Expand All @@ -35,19 +35,23 @@
import scipy.fftpack

def _binary_array_to_hex(arr):
"""
internal function to make a hex string out of a binary array
"""
h = 0
s = []
for i,v in enumerate(arr.flatten()):
if v: h += 2**(i % 8)
for i, v in enumerate(arr.flatten()):
if v:
h += 2**(i % 8)
if (i % 8) == 7:
s.append(hex(h)[2:].rjust(2, '0'))
h = 0
return "".join(s)

"""
Hash encapsulation. Can be used for dictionary keys and comparisons.
"""
class ImageHash(object):
"""
Hash encapsulation. Can be used for dictionary keys and comparisons.
"""
def __init__(self, binary_array):
self.hash = binary_array

Expand Down Expand Up @@ -76,12 +80,13 @@ def __ne__(self, other):

def __hash__(self):
# this returns a 8 bit integer, intentionally shortening the information
return sum([2**(i % 8) for i,v in enumerate(self.hash.flatten()) if v])
"""
Convert a stored hash (hex, as retrieved from str(Imagehash))
back to a Imagehash object.
"""
return sum([2**(i % 8) for i, v in enumerate(self.hash.flatten()) if v])

def hex_to_hash(hexstr):
"""
Convert a stored hash (hex, as retrieved from str(Imagehash))
back to a Imagehash object.
"""
l = []
if len(hexstr) != 16:
raise ValueError('The hex string has the wrong length')
Expand All @@ -92,29 +97,29 @@ def hex_to_hash(hexstr):
return ImageHash(numpy.array(l))


"""
Average Hash computation
def average_hash(image, hash_size=8):
"""
Average Hash computation
Implementation follows http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
Implementation follows http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
@image must be a PIL instance.
"""
def average_hash(image, hash_size=8):
@image must be a PIL instance.
"""
image = image.convert("L").resize((hash_size, hash_size), Image.ANTIALIAS)
pixels = numpy.array(image.getdata()).reshape((hash_size, hash_size))
avg = pixels.mean()
diff = pixels > avg
# make a hash
return ImageHash(diff)

"""
Perceptual Hash computation.
def phash(image, hash_size=8):
"""
Perceptual Hash computation.
Implementation follows http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
Implementation follows http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
@image must be a PIL instance.
"""
def phash(image, hash_size=8):
@image must be a PIL instance.
"""
image = image.convert("L").resize((hash_size, hash_size), Image.ANTIALIAS)
pixels = numpy.array(image.getdata(), dtype=numpy.float).reshape((hash_size, hash_size))
dct = scipy.fftpack.dct(scipy.fftpack.dct(pixels, axis=0), axis=1)
Expand All @@ -123,14 +128,14 @@ def phash(image, hash_size=8):
diff = dctlowfreq > med
return ImageHash(diff)

"""
Perceptual Hash computation.
def phash_simple(image, hash_size=8):
"""
Perceptual Hash computation.
Implementation follows http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
Implementation follows http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
@image must be a PIL instance.
"""
def phash_simple(image, hash_size=8):
@image must be a PIL instance.
"""
image = image.convert("L").resize((hash_size, hash_size), Image.ANTIALIAS)
pixels = numpy.array(image.getdata(), dtype=numpy.float).reshape((hash_size, hash_size))
dct = scipy.fftpack.dct(pixels)
Expand All @@ -139,18 +144,18 @@ def phash_simple(image, hash_size=8):
diff = dctlowfreq > avg
return ImageHash(diff)

"""
Difference Hash computation.
def dhash(image, hash_size=8):
"""
Difference Hash computation.
following http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html
following http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html
@image must be a PIL instance.
"""
def dhash(image, hash_size=8):
@image must be a PIL instance.
"""
image = image.convert("L").resize((hash_size + 1, hash_size), Image.ANTIALIAS)
pixels = numpy.array(image.getdata(), dtype=numpy.float).reshape((hash_size + 1, hash_size))
# compute differences
diff = pixels[1:,:] > pixels[:-1,:]
diff = pixels[1:, :] > pixels[:-1, :]
return ImageHash(diff)


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='ImageHash',
version='0.3',
version='1.0',
author='Johannes Buchner',
author_email='[email protected]',
packages=['imagehash'],
Expand Down

0 comments on commit fc59b24

Please sign in to comment.