Skip to content

Commit

Permalink
Closes #17: phash should first work on a bigger image
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesBuchner committed Jun 16, 2015
1 parent fc59b24 commit 56932f0
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions imagehash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,32 +112,34 @@ def average_hash(image, hash_size=8):
# make a hash
return ImageHash(diff)

def phash(image, hash_size=8):
def phash(image, hash_size=8, highfreq_factor=4):
"""
Perceptual Hash computation.
Implementation follows http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
@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))
img_size = hash_size * highfreq_factor
image = image.convert("L").resize((img_size, img_size), Image.ANTIALIAS)
pixels = numpy.array(image.getdata(), dtype=numpy.float).reshape((img_size, img_size))
dct = scipy.fftpack.dct(scipy.fftpack.dct(pixels, axis=0), axis=1)
dctlowfreq = dct[:hash_size, :hash_size]
med = numpy.median(dctlowfreq)
diff = dctlowfreq > med
return ImageHash(diff)

def phash_simple(image, hash_size=8):
def phash_simple(image, hash_size=8, highfreq_factor=4):
"""
Perceptual Hash computation.
Implementation follows http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
@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))
img_size = hash_size * highfreq_factor
image = image.convert("L").resize((img_size, img_size), Image.ANTIALIAS)
pixels = numpy.array(image.getdata(), dtype=numpy.float).reshape((img_size, img_size))
dct = scipy.fftpack.dct(pixels)
dctlowfreq = dct[:hash_size, 1:hash_size+1]
avg = dctlowfreq.mean()
Expand Down

0 comments on commit 56932f0

Please sign in to comment.