phash-rb 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2ed6783979b46e09e95389897750228c77636def24aca69d4317bdefe2084d87
4
+ data.tar.gz: fd24447815072b2c2327c08611db9ff92f26a334064a610c93a5074db3a45b61
5
+ SHA512:
6
+ metadata.gz: 82b859cbc9d5bd00dc452e7c62e33e0b74a6015be8276a9954300c2391727604dd6726a96bcbe276e04e23a3176c36cb3e78e1167934e595f2cf094127d13551
7
+ data.tar.gz: bce7b097bb1c3f370fe2622fb375064b086f4e264347f61d5da6ed03a2e54aba72f66a99341a496e7a298c8e1f61231cd6adfe6705c01e5c015dbec95fcdd31b
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Tomasz Ratajczak, Krzysztof Hasiński
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,71 @@
1
+ # Phash
2
+
3
+ Phashion replacement without native dependencies. Compatible with pHash 0.9.6.
4
+
5
+ ## Installation
6
+
7
+
8
+ ```bash
9
+ bundle add phash-rb
10
+ ```
11
+
12
+ If bundler is not being used to manage dependencies, install the gem by executing:
13
+
14
+ ```bash
15
+ gem install phash-rb
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ If you're using Phashion replace Phashion::Image with Phash::Image.
21
+
22
+ Public interface is the same as Phashion::Image.
23
+
24
+ ```ruby
25
+ require 'phash'
26
+
27
+ img1 = Phash::Image.new(filename1)
28
+ img2 = Phash::Image.new(filename2)
29
+ img1.duplicate?(img2) # true
30
+ ```
31
+
32
+ Optionally, you can set the minimum Hamming distance in the second argument, an options Hash:
33
+
34
+ ```ruby
35
+ img1.duplicate?(img2, threshold: 5) # true
36
+
37
+ img1.duplicate?(img2, threshold: 0) # false
38
+ ```
39
+
40
+ We also support the fingerprint method for storing Phash fingerprints in the database.
41
+
42
+ ```ruby
43
+ require 'phash'
44
+
45
+ Phash::Image.new(filename1).fingerprint # 3714852948054213970
46
+ ```
47
+
48
+ Fingerprint is also available in a command `phash`:
49
+
50
+ ```bash
51
+ $ phash filename1
52
+ 3714852948054213970
53
+ ```
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/khasinski/phash-rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/phash-rb/blob/main/CODE_OF_CONDUCT.md).
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
68
+
69
+ ## Code of Conduct
70
+
71
+ Everyone interacting in the Phash project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/phash-rb/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ task default: :test
data/exe/phash ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative "../lib/phash"
4
+
5
+ filename = ARGV[0]
6
+
7
+ begin
8
+ puts Phash.fingerprint(filename)
9
+ rescue => e
10
+ puts "Failed to compute pHash for #{filename}."
11
+ puts e
12
+ exit 1
13
+ end
@@ -0,0 +1,19 @@
1
+ module Phash
2
+ class Image
3
+ def initialize(image)
4
+ @image = image
5
+ end
6
+
7
+ def fingerprint
8
+ @fingerprint ||= Phash.fingerprint(@image)
9
+ end
10
+
11
+ def duplicate?(other_image, threshold: 10)
12
+ distance_from(other_image) < threshold
13
+ end
14
+
15
+ def distance_from(other_image)
16
+ (fingerprint ^ other_image.fingerprint).to_s(2).count('1')
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phash
4
+ VERSION = "0.1.0"
5
+ end
data/lib/phash.rb ADDED
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "phash/version"
4
+ require_relative "phash/image"
5
+ require "vips"
6
+ require "matrix"
7
+
8
+ module Phash
9
+ CIMG_PI = 3.14159265358979323846
10
+ CIMG_V = CIMG_PI / 2 / 32
11
+ CIMG_SCALE = 2**8 + 1
12
+
13
+ def self.fingerprint(file)
14
+ img = Vips::Image.new_from_file(file)
15
+ #Y = (66*R + 129*G + 25*B + 128)/256 + 16
16
+ img = img * [66.0 / 256, 129.0 / 256, 25.0 / 256]
17
+ r, g, b = img.bandsplit
18
+ img = r + g + b + 16.5
19
+
20
+ mask = Vips::Image.new_from_array([[1.0] * 7] * 7)
21
+ img = img.colourspace("grey16")
22
+ img = img.conv(mask)
23
+ mat = sample(img)
24
+ dct = ph_dct_matrix
25
+ dct_t = dct.transpose
26
+
27
+ out = dct * mat * dct_t
28
+
29
+ sub = out.minor(1..8, 1..8).to_a.flatten
30
+ median = sub.sort[31..32].sum / 2
31
+
32
+ sub.reverse.map {|i| i > median ? 1 : 0 }.join.to_i(2)
33
+ end
34
+
35
+ private
36
+
37
+ def self.ph_dct_matrix
38
+ v = 1 / Math.sqrt(32)
39
+ c1 = Math.sqrt(2.0 / 32)
40
+
41
+ Matrix.build(32, 32) do |y, x|
42
+ (y < 1 ? v : c1 * Math.cos(CIMG_V * y * (2*x + 1))).round(6)
43
+ end
44
+ end
45
+
46
+ def self.sample(img)
47
+ w, h = img.width / 32.0, img.height / 32.0
48
+ src = img.to_a
49
+ Matrix.build(32, 32) do |y, x|
50
+ src[y*h][x*w][0] / CIMG_SCALE
51
+ end
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phash-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomasz Ratajczak
8
+ - Krzysztof Hasiński
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2025-01-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ruby-vips
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '2.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '2.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: matrix
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description: Ruby implementation of pHash library, uses VIPS for image processing
43
+ email:
44
+ - twratajczak@gmail.com
45
+ - krzysztof.hasinski@gmail.com
46
+ executables:
47
+ - phash
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - CODE_OF_CONDUCT.md
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - exe/phash
56
+ - lib/phash.rb
57
+ - lib/phash/image.rb
58
+ - lib/phash/version.rb
59
+ homepage: http://github.com/khasinski/phash-rb
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: http://github.com/khasinski/phash-rb
64
+ source_code_uri: http://github.com/khasinski/phash-rb
65
+ changelog_uri: http://github.com/khasinski/phash-rb
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 3.0.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.5.22
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Ruby implementation of pHash library
85
+ test_files: []