Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continue even if scipy.fftpack is missing #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion imagehash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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)
Expand Down