image_hash 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/.rspec +3 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/Rakefile +6 -0
- data/image_hash.gemspec +27 -0
- data/lib/image_hash.rb +80 -0
- data/lib/image_hash/version.rb +3 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 829b739c1d457b27adc2ee184e6710395337bcd09bc410c9c639cb0840e92ca4
|
4
|
+
data.tar.gz: 59c137349c9e38fbb5523f68d7c7c5d1d5bb52b10658490f9e6d7d5d0ae6674a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1ab3c4104284087f016c564053a61b4b332c752787bfabd31996c1e253985319a115845b26db12c796cddb12648b6facdd23fad2a3344c4d5d8103b53c49d126
|
7
|
+
data.tar.gz: 3f18bdda87c9b612527584f0bfd660819688f9923878f8ad0e89dbef6385f4c321b38f9ab9bb1a56736009f97661c6ea92d511d256b36dde094c8153c4b0753e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Tim Morgan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# ImageHash
|
2
|
+
|
3
|
+
This is a very simple Ruby library for calculating a perceptual hash of any image using ImageMagick.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'image_hash'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install image_hash
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Create a new ImageHash instance, passing in the path to your image. Use the `#hash` method
|
24
|
+
to get a compact hexadecimal hash representing the image:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
ImageHash.new('image1.jpg').hash # => fce000101834b692
|
28
|
+
ImageHash.new('image2.jpg').hash # => fce000101834b693
|
29
|
+
```
|
30
|
+
|
31
|
+
If you would rather, you can get the binary hash with `#binary_hash`:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
ImageHash.new('image1.jpg').binary_hash # => 1111110011100000000000000001000000011000001101001011011010010010
|
35
|
+
ImageHash.new('image2.jpg').binary_hash # => 1111110011100000000000000001000000011000001101001011011010010011
|
36
|
+
```
|
37
|
+
|
38
|
+
Easily compare two hashes using the `.distance` method:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
ImageHash.distance(
|
42
|
+
ImageHash.new('image1.jpg').hash,
|
43
|
+
ImageHash.new('image2.jpg').hash
|
44
|
+
) # => 1
|
45
|
+
```
|
46
|
+
|
47
|
+
## License
|
48
|
+
|
49
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/image_hash.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require 'image_hash/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'image_hash'
|
8
|
+
spec.version = ImageHash::VERSION
|
9
|
+
spec.authors = ['Tim Morgan']
|
10
|
+
spec.email = ['tim@timmorgan.org']
|
11
|
+
|
12
|
+
spec.summary = 'a simple perceptual hash library for images'
|
13
|
+
spec.description = 'generate a perceptual hash of any image using ImageMagick and a very simple algorithm'
|
14
|
+
spec.homepage = 'https://github.com/seven1m/image_hash_ruby'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
18
|
+
`git ls-files`.split("\n").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_dependency 'mini_magick'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
27
|
+
end
|
data/lib/image_hash.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'image_hash/version'
|
2
|
+
require 'mini_magick'
|
3
|
+
|
4
|
+
class ImageHash
|
5
|
+
def initialize(path)
|
6
|
+
@path = path
|
7
|
+
end
|
8
|
+
|
9
|
+
def binary_hash
|
10
|
+
binary.join
|
11
|
+
end
|
12
|
+
|
13
|
+
def hash
|
14
|
+
self.class.binary_to_hex(binary_hash)
|
15
|
+
end
|
16
|
+
|
17
|
+
def binary
|
18
|
+
scale_down
|
19
|
+
desaturate
|
20
|
+
pixels.map { |v| v > average_value ? 1 : 0 }
|
21
|
+
end
|
22
|
+
|
23
|
+
class << self
|
24
|
+
def hex_to_binary(hex)
|
25
|
+
nums = hex.each_char.each_slice(2)
|
26
|
+
bin_values = nums.map { |s| s.join.to_i(16).to_s(2).rjust(8, '0') }
|
27
|
+
bin_values.join
|
28
|
+
end
|
29
|
+
|
30
|
+
def binary_to_hex(binary)
|
31
|
+
bits = binary.each_char.map(&:to_i).each_slice(8)
|
32
|
+
hex_values = bits.map { |s| s.join.to_i(2).to_s(16).rjust(2, '0') }
|
33
|
+
hex_values.join
|
34
|
+
end
|
35
|
+
|
36
|
+
def binary_distance(s1, s2)
|
37
|
+
diffs = s1.each_char.zip(s2.each_char).map do |c1, c2|
|
38
|
+
c1 == c2 ? 0 : 1
|
39
|
+
end
|
40
|
+
diffs.inject(&:+)
|
41
|
+
end
|
42
|
+
|
43
|
+
def distance(s1, s2)
|
44
|
+
binary_distance(hex_to_binary(s1), hex_to_binary(s2))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def scale_down
|
51
|
+
image.resize('8x8!')
|
52
|
+
end
|
53
|
+
|
54
|
+
def desaturate
|
55
|
+
image.format('jpg', 0, colorspace: 'Gray')
|
56
|
+
end
|
57
|
+
|
58
|
+
def image
|
59
|
+
@image ||= MiniMagick::Image.open(@path)
|
60
|
+
end
|
61
|
+
|
62
|
+
def pixels
|
63
|
+
@pixels ||= image.get_pixels.flat_map do |row|
|
64
|
+
row.map { |r, g, b| rgb_to_value(r, g, b) }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def average_value
|
69
|
+
@average_value ||= pixels.inject(&:+) / pixels.size
|
70
|
+
end
|
71
|
+
|
72
|
+
def rgb_to_value(r, g, b)
|
73
|
+
r /= 255.0
|
74
|
+
g /= 255.0
|
75
|
+
b /= 255.0
|
76
|
+
max = [r, g, b].max
|
77
|
+
min = [r, g, b].min
|
78
|
+
(max + min) / 2.0
|
79
|
+
end
|
80
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: image_hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tim Morgan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mini_magick
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: generate a perceptual hash of any image using ImageMagick and a very
|
70
|
+
simple algorithm
|
71
|
+
email:
|
72
|
+
- tim@timmorgan.org
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- image_hash.gemspec
|
84
|
+
- lib/image_hash.rb
|
85
|
+
- lib/image_hash/version.rb
|
86
|
+
homepage: https://github.com/seven1m/image_hash_ruby
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.7.6
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: a simple perceptual hash library for images
|
110
|
+
test_files: []
|