diff --git a/imagehash/__init__.py b/imagehash/__init__.py index 80b924a..5fc39f8 100644 --- a/imagehash/__init__.py +++ b/imagehash/__init__.py @@ -32,8 +32,17 @@ from PIL import Image import numpy -import scipy.fftpack +""" +Try to import the scipy.fftpack module. If it is not available pHash will not work, but one can still use aHash and dHash. +""" +try: + import scipy.fftpack + havefftpack = True +except ImportError: + havefftpack = False + + def binary_array_to_hex(arr): h = 0 s = [] @@ -116,6 +125,9 @@ def average_hash(image, hash_size=8): @image must be a PIL instance. """ def phash(image, hash_size=32): + if (havefftpack == False): + import scipy.fftpack + 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)