Name | dhash JSON |
Version |
1.4
JSON |
| download |
home_page | https://github.com/benhoyt/dhash |
Summary | Calculate difference hash (perceptual hash) for a given image, useful for detecting duplicates |
upload_time | 2022-10-23 07:59:59 |
maintainer | |
docs_url | None |
author | Ben Hoyt |
requires_python | |
license | MIT License |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
dhash
=====
.. image:: https://img.shields.io/pypi/v/dhash.svg
:target: https://pypi.org/project/dhash/
:alt: dhash on PyPI (Python Package Index)
.. image:: https://github.com/benhoyt/dhash/actions/workflows/ci.yml/badge.svg
:target: https://github.com/benhoyt/dhash/actions/workflows/ci.yml
:alt: GitHub Actions Tests
dhash is a Python library that generates a "difference hash" for a given image
-- a `perceptual hash`_ based on Neal Krawetz's dHash algorithm in `this
"Hacker Factor" blog entry`_.
The library is `on the Python Package Index (PyPI)`_ and works on both Python
3 and Python 2.7. To install it, fire up a command prompt, activate your
virtual environment if you're using one, and type:
::
pip install dhash
The algorithm to create a difference hash is very simple:
* Convert the image to grayscale
* Downsize it to a 9x9 thumbnail (size=8 means an 8+1 by 8+1 image)
* Produce a 64-bit "row hash": a 1 bit means the pixel intensity is increasing
in the x direction, 0 means it's decreasing
* Do the same to produce a 64-bit "column hash" in the y direction
* Combine the two values to produce the final 128-bit hash value
The library defaults to producing a size 8 dhash, but you can override this
easily by passing ``size=N`` as a keyword argument to most functions. For
example, you can produce a more accurate (but slower to work with) dhash of
512 bits by specifying ``size=16``.
I've found that dhash is great for detecting near duplicates (we
found dupes using a size 8 dhash with a maximum delta of 2
bits). But because of the simplicity of the algorithm, it's not great at
finding similar images or duplicate-but-cropped images -- you'd need a more
sophisticated image fingerprint if you want that. However, the dhash is good
for finding exact duplicates and near duplicates, for example, the same image
with slightly altered lighting, a few pixels of cropping, or very light
photoshopping.
To use the dhash library, you need either the `wand`_ ImageMagick binding or
the `Pillow (PIL)`_ library installed. Pick one and stick with it -- they will
produce slightly different dhash values due to differences in their grayscale
conversion and resizing algorithms.
If you have both libraries installed, dhash will use wand by default. To
override this and force use of Pillow/PIL, call ``dhash.force_pil()`` before
using the library.
To produce a dhash value using wand:
.. code:: python
import dhash
from wand.image import Image
with Image(filename='dhash-test.jpg') as image:
row, col = dhash.dhash_row_col(image)
print(dhash.format_hex(row, col))
To produce a dhash value using Pillow:
.. code:: python
import dhash
from PIL import Image
image = Image.open('dhash-test.jpg')
row, col = dhash.dhash_row_col(image)
print(dhash.format_hex(row, col))
If you have your own library to convert an image to grayscale and downsize it
to 9x9 (or 17x17 for size=16), you can pass ``dhash_row_col()`` a list of
integer pixel intensities (for example, from 0 to 255). For example:
.. code:: python
>>> import dhash
>>> row, col = dhash.dhash_row_col([0,0,1,1,1, 0,1,1,3,4, 0,1,6,6,7, 7,7,7,7,9, 8,7,7,8,9], size=4)
>>> format(row, '016b')
'0100101111010001'
>>> format(col, '016b')
'0101001111111001'
To produce the hash value as a 128-bit integer directly, use
``dhash_int(image, size=N)``. To format the hash value in various ways, use
the ``format_*`` functions:
.. code:: python
>>> row, col = (13962536140006260880, 9510476289765573406)
>>> dhash.format_bytes(row, col)
b'\xc1\xc4\xe4\xa4\x84\xa0\x80\x90\x83\xfb\xff\xcc\x00@\x83\x1e'
>>> dhash.format_hex(row, col)
'c1c4e4a484a0809083fbffcc0040831e'
To compute the number of bits different (hamming distance) between two
hashes, you can use the ``get_num_bits_different(hash1, hash2)`` helper
function:
.. code:: python
>>> import dhash
>>> dhash.get_num_bits_different(0x4bd1, 0x5bd2)
3
You can also use dhash to generate the difference hash for a specific image
from the command line:
::
$ python -m dhash dhash-test.jpg
c1c4e4a484a0809083fbffcc0040831e
$ python -m dhash --format=decimal dhash-test.jpg
13962536140006260880 9510476289765573406
# show the 8x8 row and column grids
$ python -m dhash --format=matrix dhash-test.jpg
* * . . . . . *
* * . . . * . .
* * * . . * . .
* . * . . * . .
* . . . . * . .
* . * . . . . .
* . . . . . . .
* . . * . . . .
* . . . . . * *
* * * * * . * *
* * * * * * * *
* * . . * * . .
. . . . . . . .
. * . . . . . .
* . . . . . * *
. . . * * * * .
# compute the bit delta between two images
$ python -m dhash dhash-test.jpg similar.jpg
1 bit differs out of 128 (0.8%)
Read the code in `dhash.py`_ for more details – it's pretty small!
dhash was written by `Ben Hoyt`_ and is licensed with a
permissive MIT license (see `LICENSE.txt`_).
.. _perceptual hash: https://en.wikipedia.org/wiki/Perceptual_hashing
.. _on the Python Package Index (PyPI): https://pypi.python.org/pypi/dhash
.. _this "Hacker Factor" blog entry: http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html
.. _wand: https://pypi.python.org/pypi/Wand
.. _Pillow (PIL): https://pypi.python.org/pypi/Pillow
.. _dhash.py: https://github.com/benhoyt/dhash/blob/master/dhash.py
.. _Ben Hoyt: http://benhoyt.com/
.. _LICENSE.txt: https://github.com/benhoyt/dhash/blob/master/LICENSE.txt
Raw data
{
"_id": null,
"home_page": "https://github.com/benhoyt/dhash",
"name": "dhash",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Ben Hoyt",
"author_email": "benhoyt@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/45/1b/68986e7d0e1fff5e154d96bf5c420f9933571f1d13f788b3d1b05f978267/dhash-1.4.tar.gz",
"platform": null,
"description": "dhash\n=====\n\n.. image:: https://img.shields.io/pypi/v/dhash.svg\n :target: https://pypi.org/project/dhash/\n :alt: dhash on PyPI (Python Package Index)\n\n.. image:: https://github.com/benhoyt/dhash/actions/workflows/ci.yml/badge.svg\n :target: https://github.com/benhoyt/dhash/actions/workflows/ci.yml\n :alt: GitHub Actions Tests\n\ndhash is a Python library that generates a \"difference hash\" for a given image\n-- a `perceptual hash`_ based on Neal Krawetz's dHash algorithm in `this\n\"Hacker Factor\" blog entry`_.\n\nThe library is `on the Python Package Index (PyPI)`_ and works on both Python\n3 and Python 2.7. To install it, fire up a command prompt, activate your\nvirtual environment if you're using one, and type:\n\n::\n\n pip install dhash\n\nThe algorithm to create a difference hash is very simple:\n\n* Convert the image to grayscale\n* Downsize it to a 9x9 thumbnail (size=8 means an 8+1 by 8+1 image)\n* Produce a 64-bit \"row hash\": a 1 bit means the pixel intensity is increasing\n in the x direction, 0 means it's decreasing\n* Do the same to produce a 64-bit \"column hash\" in the y direction\n* Combine the two values to produce the final 128-bit hash value\n\nThe library defaults to producing a size 8 dhash, but you can override this\neasily by passing ``size=N`` as a keyword argument to most functions. For\nexample, you can produce a more accurate (but slower to work with) dhash of\n512 bits by specifying ``size=16``.\n\nI've found that dhash is great for detecting near duplicates (we\nfound dupes using a size 8 dhash with a maximum delta of 2\nbits). But because of the simplicity of the algorithm, it's not great at\nfinding similar images or duplicate-but-cropped images -- you'd need a more\nsophisticated image fingerprint if you want that. However, the dhash is good\nfor finding exact duplicates and near duplicates, for example, the same image\nwith slightly altered lighting, a few pixels of cropping, or very light\nphotoshopping.\n\nTo use the dhash library, you need either the `wand`_ ImageMagick binding or\nthe `Pillow (PIL)`_ library installed. Pick one and stick with it -- they will\nproduce slightly different dhash values due to differences in their grayscale\nconversion and resizing algorithms.\n\nIf you have both libraries installed, dhash will use wand by default. To\noverride this and force use of Pillow/PIL, call ``dhash.force_pil()`` before\nusing the library.\n\nTo produce a dhash value using wand:\n\n.. code:: python\n\n import dhash\n from wand.image import Image\n\n with Image(filename='dhash-test.jpg') as image:\n row, col = dhash.dhash_row_col(image)\n print(dhash.format_hex(row, col))\n\nTo produce a dhash value using Pillow:\n\n.. code:: python\n\n import dhash\n from PIL import Image\n\n image = Image.open('dhash-test.jpg')\n row, col = dhash.dhash_row_col(image)\n print(dhash.format_hex(row, col))\n\nIf you have your own library to convert an image to grayscale and downsize it\nto 9x9 (or 17x17 for size=16), you can pass ``dhash_row_col()`` a list of\ninteger pixel intensities (for example, from 0 to 255). For example:\n\n.. code:: python\n\n >>> import dhash\n >>> row, col = dhash.dhash_row_col([0,0,1,1,1, 0,1,1,3,4, 0,1,6,6,7, 7,7,7,7,9, 8,7,7,8,9], size=4)\n >>> format(row, '016b')\n '0100101111010001'\n >>> format(col, '016b')\n '0101001111111001'\n\nTo produce the hash value as a 128-bit integer directly, use\n``dhash_int(image, size=N)``. To format the hash value in various ways, use\nthe ``format_*`` functions:\n\n.. code:: python\n\n >>> row, col = (13962536140006260880, 9510476289765573406)\n >>> dhash.format_bytes(row, col)\n b'\\xc1\\xc4\\xe4\\xa4\\x84\\xa0\\x80\\x90\\x83\\xfb\\xff\\xcc\\x00@\\x83\\x1e'\n >>> dhash.format_hex(row, col)\n 'c1c4e4a484a0809083fbffcc0040831e'\n\nTo compute the number of bits different (hamming distance) between two\nhashes, you can use the ``get_num_bits_different(hash1, hash2)`` helper\nfunction:\n\n.. code:: python\n\n >>> import dhash\n >>> dhash.get_num_bits_different(0x4bd1, 0x5bd2)\n 3\n\nYou can also use dhash to generate the difference hash for a specific image\nfrom the command line:\n\n::\n\n $ python -m dhash dhash-test.jpg\n c1c4e4a484a0809083fbffcc0040831e\n\n $ python -m dhash --format=decimal dhash-test.jpg\n 13962536140006260880 9510476289765573406\n\n # show the 8x8 row and column grids\n $ python -m dhash --format=matrix dhash-test.jpg\n * * . . . . . * \n * * . . . * . . \n * * * . . * . . \n * . * . . * . . \n * . . . . * . . \n * . * . . . . . \n * . . . . . . . \n * . . * . . . . \n\n * . . . . . * * \n * * * * * . * * \n * * * * * * * * \n * * . . * * . . \n . . . . . . . . \n . * . . . . . . \n * . . . . . * * \n . . . * * * * . \n\n # compute the bit delta between two images\n $ python -m dhash dhash-test.jpg similar.jpg\n 1 bit differs out of 128 (0.8%)\n\nRead the code in `dhash.py`_ for more details \u2013 it's pretty small!\n\ndhash was written by `Ben Hoyt`_ and is licensed with a\npermissive MIT license (see `LICENSE.txt`_).\n\n\n.. _perceptual hash: https://en.wikipedia.org/wiki/Perceptual_hashing\n.. _on the Python Package Index (PyPI): https://pypi.python.org/pypi/dhash\n.. _this \"Hacker Factor\" blog entry: http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html\n.. _wand: https://pypi.python.org/pypi/Wand\n.. _Pillow (PIL): https://pypi.python.org/pypi/Pillow\n.. _dhash.py: https://github.com/benhoyt/dhash/blob/master/dhash.py\n.. _Ben Hoyt: http://benhoyt.com/\n.. _LICENSE.txt: https://github.com/benhoyt/dhash/blob/master/LICENSE.txt\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "Calculate difference hash (perceptual hash) for a given image, useful for detecting duplicates",
"version": "1.4",
"project_urls": {
"Homepage": "https://github.com/benhoyt/dhash"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5a15ab8b7105573b2f8730ec22a9cdd9014d6aebd36fb8a4f0412123090d6eb1",
"md5": "56bba352664415fef9941d22d5a04f3d",
"sha256": "759e1f7bdc99c3fb271ab5672a0f843ae274a7ea6794423618797cc12c680296"
},
"downloads": -1,
"filename": "dhash-1.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "56bba352664415fef9941d22d5a04f3d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 7415,
"upload_time": "2022-10-23T07:59:56",
"upload_time_iso_8601": "2022-10-23T07:59:56.924797Z",
"url": "https://files.pythonhosted.org/packages/5a/15/ab8b7105573b2f8730ec22a9cdd9014d6aebd36fb8a4f0412123090d6eb1/dhash-1.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "451b68986e7d0e1fff5e154d96bf5c420f9933571f1d13f788b3d1b05f978267",
"md5": "841c75e51278f6922ab1dc3ab0a6a426",
"sha256": "e0fd71c86d93585b7f08af7baceb0106cf1016ca70223517003605855b485183"
},
"downloads": -1,
"filename": "dhash-1.4.tar.gz",
"has_sig": false,
"md5_digest": "841c75e51278f6922ab1dc3ab0a6a426",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 9008,
"upload_time": "2022-10-23T07:59:59",
"upload_time_iso_8601": "2022-10-23T07:59:59.272765Z",
"url": "https://files.pythonhosted.org/packages/45/1b/68986e7d0e1fff5e154d96bf5c420f9933571f1d13f788b3d1b05f978267/dhash-1.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-10-23 07:59:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "benhoyt",
"github_project": "dhash",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "dhash"
}