A more constrained and friendlier fork of ImageHash.
Project description
imha
A more constrained and friendlier fork of ImageHash.
imha is constrained to not use numpy and keep dependencies to a minimum. This keeps the package simpler and also easier to be installed on limited environments. imha also offers more customization for parametrizing the hash computation and a better engineered hash representation that can be easily converted to different types and representations.
Usage
>>> from PIL import Image
>>> import imha
>>> hash1 = imha.average_hash(Image.open("photo1.jpg"))
>>> hash1.bin()
'1111111111010111100100011000000110000001110010011111111111111111'
>>> hash1.hex()
'ffd7918181c9ffff'
>>> hash1.uint()
18435363585078722559
>>> bytes(hash1)
b'\xff\xd7\x91\x81\x81\xc9\xff\xff'
>>> int(hash1)
-11380488630829057
>>> len(hash1) # hash length in bits
64
>>> hash2 = imha.average_hash(Image.open("photo2.jpg"))
>>> hash2.hex()
'9f172786e71f1e00'
>>> hash1 == hash2
False
>>> hash1 - hash2 # hamming distance between hashes
33
Goals
- Generate the same hash values as ImageHash.
- Require only the pillow package.
- Be compatible with currently supported Python versions.
API Reference
average_hash
def average_hash(image, size=(8, 8))
Compute Average Hash.
Computes hash with width*height bits.
Arguments:
image: Source image.size: Tuple with width and height to resize the image to. (Default value =(8, 8))
dhash
def dhash(image, size=(9, 8), *, skip_corners=False)
Compute Difference Hash by row.
Computes row hash with (width-1)*height bits. Enabling skip_corners reduces
the hash length by 4 bits. This means a 64-bits hash can be generated with
size=(9, 8) or a 16-bit hash be generated with either size=(5, 4) or
size=(6, 4), skip_corners=True for example.
Arguments:
image: Source image.size: Tuple with width and height to resize the image to. (Default value =(9, 8))skip_corners: Ignore the four corners. (Default value =False)
dhash_vertical
def dhash_vertical(image, size=(8, 9), *, skip_corners=False)
Compute Difference Hash by column.
Computes col hash with width*(height-1) bits. Enabling skip_corners reduces
the hash length by 4 bits. This means a 64-bits hash can be generated with
size=(8, 9) or a 16-bit hash be generated with either size=(4, 5) or
size=(5, 5), skip_corners=True for example.
Arguments:
image: Source image.size: Tuple with width and height to resize the image to. (Default value =(8, 9))skip_corners: Ignore the four corners. (Default value =False)
Hash Objects
class Hash()
Represents a hash object.
The hash value can be converted to various types and representations using the built-in functions and provided methods. The hamming distance between hashes can be computed by subtracting one hash from another.
__init__
def __init__(value, len_)
Create hash object.
Arguments:
value: The hash as an unsigned integer value.len_: The hash length in bits.
bin
def bin()
Return the hash binary representation.
hex
def hex()
Return the hash hexadecimal representation.
uint
def uint()
Return the hash as an unsigned integer value.
__len__
def __len__()
Return len(self), the hash length in bits.
__sub__
def __sub__(other)
Return self - other, the hamming distance between hashes.
__eq__
def __eq__(other)
Return self == other.
__hash__
def __hash__()
Return hash(self).
__bytes__
def __bytes__()
Return bytes(self), the hash as bytes.
__int__
def __int__()
Return int(self), the hash as a signed integer value.
__index__
def __index__()
Return integer for bin(self), hex(self) and oct(self).
__repr__
def __repr__()
Return repr(self).
__str__
def __str__()
Return str(self).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file imha-0.1.0.tar.gz.
File metadata
- Download URL: imha-0.1.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a8c74b3ada940eb2dbf7d847d5d4a2968c55a6cc3fcbf6e189031d69722443a
|
|
| MD5 |
4b295c7ba36c305af61637e00e44f038
|
|
| BLAKE2b-256 |
0ed98a1753ba8fbf65067da14a491e6e64d0e2d6294f8551b2b5c155ca4c1a4b
|
File details
Details for the file imha-0.1.0-py3-none-any.whl.
File metadata
- Download URL: imha-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8872e74f901d6fcfd54e1c719b2a30a57d76ee167471b2b3ded811cb674ad83
|
|
| MD5 |
b61896dda1f2c30c813cc1bf0cd24162
|
|
| BLAKE2b-256 |
dc9a6e4e93b7afb4fa9ac9d94a6273923ad5d20bd79e338e4168fd36b996f0de
|