Skip to content

Commit

Permalink
Make PyWavelets and scipy optional installs
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Oct 20, 2023
1 parent 0624854 commit b7b99e4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
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]

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

* 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
],
extras_require={
"whash": "PyWavelets", # for whash
'phash': "scipy", # for phash
},
test_suite='tests',
tests_require=['pytest>=3'],
)

0 comments on commit b7b99e4

Please sign in to comment.