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

Make PyWavelets and scipy optional installs #195

Closed
Closed
Changes from 2 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
9 changes: 8 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -30,11 +30,16 @@ black & gray fractions (without position information).
Installation
============

Based on PIL/Pillow Image, numpy and scipy.fftpack (for pHash)
Based on PIL/Pillow Image, numpy, scipy.fftpack (for pHash) and PyWavelets (for wHash)
Easy installation through `pypi`_::

pip install imagehash

scipy and PyWavelets are optional. If you don't intend on using them, you can install ImageHash using:

pip install imagehash --no-dependencies
pip install numpy pillow

Basic usage
===========
::
@@ -162,6 +167,8 @@ and show how you can create a reverse image search using hashes generated by thi
Changelog
----------

* Unreleased: Make PyWavelets and scipy optional if ImageHash is installed with `--no-dependencies` installs by @Avasam

* 4.3: typing annotations by @Avasam @SpangleLabs and @nh2

* 4.2: Cropping-Resistant image hashing added by @joshcoales
18 changes: 15 additions & 3 deletions imagehash/__init__.py
Original file line number Diff line number Diff line change
@@ -266,10 +266,14 @@ def phash(image, hash_size=8, highfreq_factor=4):

@image must be a PIL instance.
"""
try:
import scipy.fftpack
except (ModuleNotFoundError) as e:
raise ModuleNotFoundError(f"{e.msg}. Did you forget to install scipy?") from e

if hash_size < 2:
raise ValueError('Hash size must be greater than or equal to 2')

import scipy.fftpack
img_size = hash_size * highfreq_factor
image = image.convert('L').resize((img_size, img_size), ANTIALIAS)
pixels = numpy.asarray(image)
@@ -289,7 +293,11 @@ def phash_simple(image, hash_size=8, highfreq_factor=4):

@image must be a PIL instance.
"""
import scipy.fftpack
try:
import scipy.fftpack
except (ModuleNotFoundError) as e:
raise ModuleNotFoundError(f"{e.msg}. Did you forget to install scipy?") from e

img_size = hash_size * highfreq_factor
image = image.convert('L').resize((img_size, img_size), ANTIALIAS)
pixels = numpy.asarray(image)
@@ -357,7 +365,11 @@ def whash(image, hash_size=8, image_scale=None, mode='haar', remove_max_haar_ll=
'db4' - Daubechies wavelets
@remove_max_haar_ll - remove the lowest low level (LL) frequency using Haar wavelet.
"""
import pywt
try:
import pywt
except (ModuleNotFoundError) as e:
raise ModuleNotFoundError(f"{e.msg}. Did you forget to install PyWavelets?") from e

if image_scale is not None:
assert image_scale & (image_scale - 1) == 0, 'image_scale is not power of 2'
else:
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@
'scipy', # for phash
'pillow', # or PIL
'PyWavelets', # for whash
],
'pillow', # or PIL
test_suite='tests',
tests_require=['pytest>=3'],
)