Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elcuervo/ph
https://github.com/elcuervo/ph
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/elcuervo/ph
- Owner: elcuervo
- License: mit
- Created: 2021-04-02T23:13:26.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-05-28T15:29:10.000Z (over 3 years ago)
- Last Synced: 2024-11-28T23:28:28.361Z (about 2 months ago)
- Language: Ruby
- Size: 141 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PH
_Perceptual Hashing_## What is it?
Perceptual hashing is the technique to generate a fingerprint of an image.
- https://en.wikipedia.org/wiki/Perceptual_hashing
- http://hackerfactor.com/blog/index.php%3F/archives/432-Looks-Like-It.html## Install
```bash
gem install ph
```## Usage
`PH` Generates a perceptual hash from an array of pixels.
The way you get that pixel data is up to you. Different techniques can shield different values.
Hashes exist on a similar space but different reads could yield different (subtle) hashes.### Vips
```ruby
size = 64
img = Vips::Image.new_from_file(file)
width, height = img.width, img.height
w_scale, v_scale = size.fdiv(width), size.fdiv(height)img = img
.resize(w_scale, vscale: v_scale)
.colourspace(:grey16)pixels_2d = img.to_a.map(&:flatten)
PH.hash(pixels_2d)
# => "859091ce633aaebb"
```### RMagick
```ruby
img = Magick::ImageList.new(file)
size = 64
img = img
.scale(size, size)
.dispatch(0, 0, size, size, "I")
.each_slice(size)
.to_aPH.new(img).vector
# => [1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1]
```### Distance
Since hashes exist on a metric space you can measure how far a hash is from another.
You can use the [hamming gem](https://github.com/elcuervo/hamming) for calculations if needed```ruby
Hamming.distance(hash_a, hash_b)# You can also transform hashes based on your storage:
Hamming.vector_to_hash(hash)
Hamming.hash_to_vector(vector)
```