image_compare 1.0.0.pre.dev → 1.0.1
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 +4 -4
- data/README.md +82 -16
- data/lib/image_compare/matcher.rb +1 -1
- data/lib/image_compare/modes/base.rb +1 -1
- data/lib/image_compare/modes.rb +3 -3
- data/lib/image_compare/rectangle.rb +3 -1
- data/lib/image_compare/version.rb +1 -1
- data/lib/image_compare.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9172a89fc7bd3779f58bf4291d045db2e9de6ce8e1d17d7eed9be6970c6faed9
|
4
|
+
data.tar.gz: f3d1a90a07c4d728ea51c2effd424d9a32b4567b53b59119a996637defebc4b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8a0fcbd0668b614592b65972d1326a7a2f2853da2a133150fd6d5d0e8153a46f7d986033cec071d6df6b6cdd6c5b0c7950cd76b07494dc6bd8e240ee4d928b3
|
7
|
+
data.tar.gz: 8c81cf6accfeb98d780ff3ed724834b5816c0d245186086d9b075e77f56a8f29f9d52d20fa4d01210bc5a8ddd36dfb7e7694ffc9e8024fc809e4a10b31437a42
|
data/README.md
CHANGED
@@ -33,15 +33,23 @@ ImageCompare supports different ways (_modes_) of comparing images.
|
|
33
33
|
|
34
34
|
Source images used in examples:
|
35
35
|
|
36
|
-
<img src='
|
37
|
-
<img src='
|
36
|
+
<img alt='a.png' src='spec/fixtures/a.png' />
|
37
|
+
<img alt='b.png' src='spec/fixtures/b.png' />
|
38
|
+
|
39
|
+
### Color
|
40
|
+
|
41
|
+
Compare pixels, resulting score is a ratio of unequal pixels (with respect to provided tolerance).
|
42
|
+
|
43
|
+
Resulting diff contains version of the first image with different pixels highlighted in red and red bounding box.
|
44
|
+
|
45
|
+
<img alt='color_diff' src='spec/fixtures/color_diff.png' />
|
38
46
|
|
39
47
|
### Base (RGB) mode
|
40
48
|
|
41
49
|
Compare pixels by values, resulting score is a ratio of unequal pixels.
|
42
50
|
Resulting diff represents per-channel difference.
|
43
51
|
|
44
|
-
<img src='
|
52
|
+
<img alt='rgb_diff.png' src='spec/fixtures/rgb_diff.png' width='480' />
|
45
53
|
|
46
54
|
### Grayscale mode
|
47
55
|
|
@@ -49,24 +57,29 @@ Compare pixels as grayscale (by brightness and alpha), resulting score is a rati
|
|
49
57
|
|
50
58
|
Resulting diff contains grayscale version of the first image with different pixels highlighted in red and red bounding box.
|
51
59
|
|
52
|
-
<img src='
|
60
|
+
<img alt='grayscale_diff.png' src='spec/fixtures/grayscale_diff.png' width='480' />
|
53
61
|
|
54
62
|
### Delta
|
55
63
|
|
56
64
|
Compare pixels using [Delta E](https://en.wikipedia.org/wiki/Color_difference) distance.
|
57
65
|
Resulting diff contains grayscale version of the first image with different pixels highlighted in red (with respect to diff score).
|
58
66
|
|
59
|
-
<img src='
|
67
|
+
<img alt='delta_diff.png' src='spec/fixtures/delta_diff.png' width='480' />
|
60
68
|
|
61
69
|
## Usage
|
62
70
|
|
63
71
|
```ruby
|
64
|
-
#
|
65
|
-
# and base (
|
72
|
+
# Create new matcher with default threshold equals to 0
|
73
|
+
# and base (Color) mode
|
66
74
|
cmp = ImageCompare::Matcher.new
|
75
|
+
cmp.mode #=> ImageCompare::Modes::Color
|
76
|
+
|
77
|
+
# Create new matcher with default threshold equals to 0
|
78
|
+
# and base (RGB) mode
|
79
|
+
cmp = ImageCompare::Matcher.new mode: :rgb
|
67
80
|
cmp.mode #=> ImageCompare::Modes::RGB
|
68
81
|
|
69
|
-
#
|
82
|
+
# Create matcher with specific threshold
|
70
83
|
cmp = ImageCompare::Matcher.new threshold: 0.05
|
71
84
|
cmp.threshold #=> 0.05
|
72
85
|
|
@@ -74,38 +87,91 @@ cmp.threshold #=> 0.05
|
|
74
87
|
cmp = ImageCompare::Matcher.new lower_threshold: 0.01
|
75
88
|
cmp.lower_threshold #=> 0.01
|
76
89
|
|
77
|
-
#
|
90
|
+
# Create zero-tolerance grayscale matcher
|
78
91
|
cmp = ImageCompare::Matcher.new mode: :grayscale, tolerance: 0
|
79
92
|
cmp.mode #=> ImageCompare::Modes::Grayscale
|
80
93
|
|
81
|
-
res = cmp.compare(
|
94
|
+
res = cmp.compare('path/image1.png', 'path/image2.png')
|
82
95
|
res #=> ImageCompare::Result
|
83
96
|
res.match? #=> true
|
84
97
|
res.score #=> 0.0
|
85
98
|
|
86
99
|
# Return diff image object
|
87
100
|
res.difference_image #=> ImageCompare::Image
|
88
|
-
res.difference_image.save(
|
101
|
+
res.difference_image.save(result)
|
89
102
|
|
90
103
|
# without explicit matcher
|
91
|
-
res = ImageCompare.compare(
|
104
|
+
res = ImageCompare.compare('path/image1.png', 'path/image2.png', options)
|
105
|
+
res.match? #=> true
|
106
|
+
res.score #=> 0.0
|
92
107
|
|
93
108
|
# equals to
|
94
|
-
res = ImageCompare::Matcher.new(options).compare(
|
109
|
+
res = ImageCompare::Matcher.new(options).compare('my_images_path/image1.png', 'my_images_path/image2.png')
|
110
|
+
res.match? #=> true
|
111
|
+
res.score #=> 0.0
|
95
112
|
```
|
96
113
|
|
97
114
|
## Excluding rectangle
|
98
115
|
|
99
|
-
<img src='
|
100
|
-
<img src='
|
116
|
+
<img alt='a.png' src='spec/fixtures/a.png' />
|
117
|
+
<img alt='a1.png' src='spec/fixtures/a1.png' />
|
101
118
|
|
102
119
|
You can exclude rectangle from comparing by passing `:exclude_rect` to `compare`.
|
103
120
|
E.g., if `path_1` and `path_2` contain images above
|
104
121
|
```ruby
|
105
|
-
ImageCompare.compare(
|
122
|
+
ImageCompare.compare('path/image1.png', 'path/image2.png', exclude_rect: [200, 150, 275, 200]).match? # => true
|
123
|
+
|
124
|
+
# or
|
125
|
+
|
126
|
+
cmp = ImageCompare::Matcher.new exclude_rect: [200, 150, 275, 200]
|
127
|
+
res = cmp.compare('path/image1.png', 'path/image2.png')
|
128
|
+
res #=> ImageCompare::Result
|
129
|
+
res.match? #=> true
|
130
|
+
res.score #=> 0.0
|
131
|
+
|
132
|
+
# Return diff image object
|
133
|
+
res.difference_image #=> ImageCompare::Image
|
134
|
+
res.difference_image.save('path/diff.png')
|
106
135
|
```
|
107
136
|
`[200, 150, 275, 200]` is array of two vertices of rectangle -- (200, 150) is left-top vertex and (275, 200) is right-bottom.
|
108
137
|
|
138
|
+
### Cucumber + Capybara example
|
139
|
+
`support/env.rb`:
|
140
|
+
```ruby
|
141
|
+
require 'image_compare'
|
142
|
+
```
|
143
|
+
|
144
|
+
`support/capybara_extensions.rb`:
|
145
|
+
```ruby
|
146
|
+
# frozen_string_literal: true
|
147
|
+
|
148
|
+
Capybara::Node::Element.class_eval do
|
149
|
+
def rect_area
|
150
|
+
rect_area = self.evaluate_script('this.getBoundingClientRect()')
|
151
|
+
|
152
|
+
[
|
153
|
+
rect_area['left'].to_f.round,
|
154
|
+
rect_area['top'].to_f.round,
|
155
|
+
rect_area['right'].to_f.round,
|
156
|
+
rect_area['bottom'].to_f.round
|
157
|
+
]
|
158
|
+
rescue StandardError => e
|
159
|
+
raise "Error getting element rect area: #{e.inspect}!"
|
160
|
+
end
|
161
|
+
end
|
162
|
+
```
|
163
|
+
|
164
|
+
`steps/my_step.rb`:
|
165
|
+
```ruby
|
166
|
+
my_element = page.find('#my_element').rect_area
|
167
|
+
|
168
|
+
cmp = ImageCompare::Matcher.new exclude_rect: my_element.rect_area
|
169
|
+
res = cmp.compare('path/image1.png', 'path/image2.png')
|
170
|
+
res.difference_image.save('path/diff.png')
|
171
|
+
|
172
|
+
expect(res.match?).to eq(true)
|
173
|
+
```
|
174
|
+
|
109
175
|
## Including rectangle
|
110
176
|
|
111
177
|
You can set bounds of comparing by passing `:include_rect` to `compare` with array similar to previous example
|
@@ -16,7 +16,7 @@ module ImageCompare
|
|
16
16
|
attr_reader :mode
|
17
17
|
|
18
18
|
def initialize(**options)
|
19
|
-
mode_type = options.delete(:mode) || :
|
19
|
+
mode_type = options.delete(:mode) || :color
|
20
20
|
raise ArgumentError, "Undefined mode: #{mode_type}" unless MODES.key?(mode_type)
|
21
21
|
@mode = Modes.const_get(MODES[mode_type]).new(**options)
|
22
22
|
end
|
@@ -22,8 +22,8 @@ module ImageCompare
|
|
22
22
|
@bounds = Rectangle.new(*include_rect.bounds)
|
23
23
|
|
24
24
|
b.compare_each_pixel(a, area: include_rect) do |b_pixel, a_pixel, x, y|
|
25
|
-
next if pixels_equal?(b_pixel, a_pixel)
|
26
25
|
next if !exclude_rect.nil? && exclude_rect.contains_point?(x, y)
|
26
|
+
next if pixels_equal?(b_pixel, a_pixel)
|
27
27
|
update_result(b_pixel, a_pixel, x, y)
|
28
28
|
end
|
29
29
|
|
data/lib/image_compare/modes.rb
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
module ImageCompare
|
4
4
|
module Modes
|
5
|
-
require 'image_compare/modes/rgb'
|
6
|
-
require 'image_compare/modes/grayscale'
|
7
|
-
require 'image_compare/modes/delta'
|
8
5
|
require 'image_compare/modes/color'
|
6
|
+
require 'image_compare/modes/delta'
|
7
|
+
require 'image_compare/modes/grayscale'
|
8
|
+
require 'image_compare/modes/rgb'
|
9
9
|
end
|
10
10
|
end
|
@@ -27,7 +27,9 @@ module ImageCompare
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def contains_point?(x, y)
|
30
|
-
x
|
30
|
+
(x >= left && y >= top && x <= right && y <= bot) ||
|
31
|
+
(x <= right && y <= top && x >= left && y >= bot) ||
|
32
|
+
(x.between?(right, left) && y.between?(bot, top))
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
data/lib/image_compare.rb
CHANGED
@@ -6,8 +6,8 @@ module ImageCompare
|
|
6
6
|
class SizesMismatchError < StandardError
|
7
7
|
end
|
8
8
|
|
9
|
-
require 'image_compare/matcher'
|
10
9
|
require 'image_compare/color_methods'
|
10
|
+
require 'image_compare/matcher'
|
11
11
|
|
12
12
|
def self.compare(path_a, path_b, **options)
|
13
13
|
Matcher.new(**options).compare(path_a, path_b)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: image_compare
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- cristianofmc
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2023-03-
|
12
|
+
date: 2023-03-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: chunky_png
|
@@ -99,9 +99,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
99
|
version: 3.2.0
|
100
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
101
|
requirements:
|
102
|
-
- - "
|
102
|
+
- - ">="
|
103
103
|
- !ruby/object:Gem::Version
|
104
|
-
version:
|
104
|
+
version: '0'
|
105
105
|
requirements: []
|
106
106
|
rubygems_version: 3.4.1
|
107
107
|
signing_key:
|