dhash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d1da8f5898b57a4d35fb9e2d9fee1ed4f4f88a07
4
+ data.tar.gz: 225f39c75d943aa9f383d53f1f27213b47658acb
5
+ SHA512:
6
+ metadata.gz: 6119723956596fbadf1a6ac346247086e6211155986eab810f927df8e43b2efac12d5a96dc564ae9ff4dc467c062a38fdbe30eb6c4bd2ec20f9a962f0a454918
7
+ data.tar.gz: 8282ea0cc4da23512bb2a33610000eb729a33545a901b44fc0987f9b5ed0e8b2e31fda98d0741625f59f187203c5a2399aef73fe9abb4c04bd6e7590b2e14988
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dhash.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alex MacCaw
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # Dhash
2
+
3
+ Dhash's (or difference hashes) are a way of calculating similarity between images. If two images have a similar dhash, then the likelihood is that they both are depicting the same image (albeit slightly cropped compressed etc).
4
+
5
+ For more information, check out the links below.
6
+
7
+ - http://hackerlabs.org/blog/2012/07/30/organizing-photos-with-duplicate-and-similarity-checking/
8
+ - http://www.hackerfactor.com/blog/?/archives/529-Kind-of-Like-That.html
9
+ - http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
10
+ - http://blog.iconfinder.com/detecting-duplicate-images-using-python/
11
+
12
+ ## Installation
13
+
14
+ gem 'dhash'
15
+
16
+ ## Usage
17
+
18
+ hash1 = Dhash.calculate('face-high.jpg')
19
+ hash2 = Dhash.calculate('face-low.jpg')
20
+
21
+ if Dhash.hamming(hash1, hash2) < 10
22
+ puts "Images are very similar"
23
+ else
24
+ puts "No match"
25
+ end
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ task :default => :test
data/bin/dhash ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'dhash'
4
+
5
+ unless path = ARGV[0]
6
+ puts "Usage: dhash [path]"
7
+ exit
8
+ end
9
+
10
+ puts Dhash.calculate(File.expand_path(path))
data/bin/hamming ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'dhash'
4
+
5
+ num1 = ARGV[0]
6
+ num2 = ARGV[1]
7
+
8
+ unless num1 && num2
9
+ puts "Usage: hamming [num1] [num2]"
10
+ exit
11
+ end
12
+
13
+ puts Dhash.hamming(num1.to_i, num2.to_i)
data/dhash.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dhash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dhash"
8
+ spec.version = Dhash::VERSION
9
+ spec.authors = ["Alex MacCaw"]
10
+ spec.email = ["info@eribium.org"]
11
+ spec.summary = %q{Calculate Dhash on images}
12
+ spec.homepage = "https://github.com/maccman/dhash"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_dependency 'rmagick'
23
+ end
data/lib/dhash.rb ADDED
@@ -0,0 +1,26 @@
1
+ require "dhash/version"
2
+ require "rmagick"
3
+
4
+ module Dhash extend self
5
+ def hamming(a, b)
6
+ (a^b).to_s(2).count('1')
7
+ end
8
+
9
+ def calculate(file, hash_size = 8)
10
+ image = Magick::Image.read(file).first
11
+ image = image.quantize(256, Magick::Rec601LumaColorspace, Magick::NoDitherMethod, 8)
12
+ image = image.resize!(hash_size + 1, hash_size)
13
+
14
+ difference = []
15
+
16
+ hash_size.times do |row|
17
+ hash_size.times do |col|
18
+ pixel_left = image.pixel_color(col, row).intensity
19
+ pixel_right = image.pixel_color(col + 1, row).intensity
20
+ difference << (pixel_left > pixel_right)
21
+ end
22
+ end
23
+
24
+ difference.map {|d| d ? 1 : 0 }.join('').to_i(2)
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Dhash
2
+ VERSION = "0.0.1"
3
+ end
Binary file
Binary file
Binary file
@@ -0,0 +1,26 @@
1
+ require 'minitest/autorun'
2
+ require 'dhash'
3
+ require 'pp'
4
+
5
+ describe Dhash do
6
+ it 'should have similar hashes for low/high of same image' do
7
+ hash1 = Dhash.calculate(File.expand_path('../images/face-high.jpg', __FILE__))
8
+ hash2 = Dhash.calculate(File.expand_path('../images/face-low.jpg', __FILE__))
9
+
10
+ assert Dhash.hamming(hash1, hash2) < 3
11
+ end
12
+
13
+ it 'should have similar hashes for similar images' do
14
+ hash1 = Dhash.calculate(File.expand_path('../images/face-high.jpg', __FILE__))
15
+ hash2 = Dhash.calculate(File.expand_path('../images/face-with-nose.jpg', __FILE__))
16
+
17
+ assert Dhash.hamming(hash1, hash2) < 5
18
+ end
19
+
20
+ it 'should have identical hashes for identical images' do
21
+ hash1 = Dhash.calculate(File.expand_path('../images/face-high.jpg', __FILE__))
22
+ hash2 = Dhash.calculate(File.expand_path('../images/face-high.jpg', __FILE__))
23
+
24
+ assert Dhash.hamming(hash1, hash2) == 0
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dhash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex MacCaw
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rmagick
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - info@eribium.org
58
+ executables:
59
+ - dhash
60
+ - hamming
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/dhash
70
+ - bin/hamming
71
+ - dhash.gemspec
72
+ - lib/dhash.rb
73
+ - lib/dhash/version.rb
74
+ - test/images/face-high.jpg
75
+ - test/images/face-low.jpg
76
+ - test/images/face-with-nose.jpg
77
+ - test/test_dhash.rb
78
+ homepage: https://github.com/maccman/dhash
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.2.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Calculate Dhash on images
102
+ test_files:
103
+ - test/images/face-high.jpg
104
+ - test/images/face-low.jpg
105
+ - test/images/face-with-nose.jpg
106
+ - test/test_dhash.rb