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
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -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 intend on using pHash or wHash, you can install the right dependencies using::

pip install imagehash[phash]
pip install imagehash[whash]

Avasam marked this conversation as resolved.
Show resolved Hide resolved
Basic usage
===========
::
Expand Down Expand Up @@ -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 installs by @Avasam
Avasam marked this conversation as resolved.
Show resolved Hide resolved

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

* 4.2: Cropping-Resistant image hashing added by @joshcoales
Expand Down
18 changes: 15 additions & 3 deletions imagehash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
long_description_content_type='text/x-rst',
install_requires=[
'numpy',
'scipy', # for phash
'pillow', # or PIL
'PyWavelets', # for whash
'pillow', # or PIL
Avasam marked this conversation as resolved.
Show resolved Hide resolved
],
extras_require={
"whash": "PyWavelets", # for whash
'phash': "scipy", # for phash
},
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to provide these. Users can just add to their own dependencies. But it's a nice to have. And helps some tools that look at packages co-dependencies (for security analysis for instance)

Avasam marked this conversation as resolved.
Show resolved Hide resolved
test_suite='tests',
tests_require=['pytest>=3'],
)