image_compare 1.0.1 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -1
- data/README.md +54 -32
- data/lib/image_compare/color_methods.rb +2 -2
- data/lib/image_compare/image.rb +1 -1
- data/lib/image_compare/matcher.rb +14 -12
- data/lib/image_compare/modes/base.rb +1 -1
- data/lib/image_compare/modes/color.rb +6 -6
- data/lib/image_compare/modes/delta.rb +1 -1
- data/lib/image_compare/modes/grayscale.rb +1 -1
- data/lib/image_compare/modes/rgb.rb +1 -1
- data/lib/image_compare/modes.rb +4 -4
- data/lib/image_compare/rectangle.rb +2 -2
- data/lib/image_compare/version.rb +1 -1
- data/lib/image_compare.rb +3 -3
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: efc745c05f8c87808311a102dc55c92e030de6ce85a6512aed95161a817258ce
|
4
|
+
data.tar.gz: a243eda35bc9edf3313a8d34c7ad71ae2a066f3553faada189a58ea9db04c1e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6009fd283629fc28bc28e5ed4a4e74dcf39946e23d16dd0bb03a35ca98fb069f8a5a3279d5b46cd3373550d80cccc7a5fda0c360277062a092c220ba4415827e
|
7
|
+
data.tar.gz: 3ea374f5b4c3e184ee34f9c0ff3aa36c8a400e3923bafa34bd90ca66b79c2da080f606bf31fb31d717496bf045f091eba9d6a7df96eb0283517587615f019233
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
3
|
## main
|
4
|
-
- Add Color mode to generate "normal" diff images ([@gsguma][], [@cristianofmc][])
|
4
|
+
- v1.0.0: Add Color mode to generate "normal" diff images ([@gsguma][], [@cristianofmc][])
|
5
|
+
- v1.0.1: Fix all code smells and integration tests ([@gsguma][])
|
6
|
+
- v1.0.2: Fix gem documentation uri ([@gsguma][])
|
5
7
|
|
6
8
|
[@gsguma]: https://github.com/gsguma
|
7
9
|
[@cristianofmc]: https://github.com/cristianofmc
|
data/README.md
CHANGED
@@ -11,7 +11,7 @@ This is an utility library for image regression testing.
|
|
11
11
|
Add this line to your application's Gemfile:
|
12
12
|
|
13
13
|
```ruby
|
14
|
-
gem
|
14
|
+
gem "image_compare"
|
15
15
|
```
|
16
16
|
|
17
17
|
Or adding to your project:
|
@@ -20,7 +20,7 @@ Or adding to your project:
|
|
20
20
|
# my-cool-gem.gemspec
|
21
21
|
Gem::Specification.new do |spec|
|
22
22
|
# ...
|
23
|
-
spec.add_dependency
|
23
|
+
spec.add_dependency "image_compare"
|
24
24
|
# ...
|
25
25
|
end
|
26
26
|
```
|
@@ -33,38 +33,47 @@ ImageCompare supports different ways (_modes_) of comparing images.
|
|
33
33
|
|
34
34
|
Source images used in examples:
|
35
35
|
|
36
|
-
|
37
|
-
<img alt='b.png' src='spec/fixtures/b.png' />
|
36
|
+
a1.png
|
38
37
|
|
39
|
-
|
38
|
+
<img alt="a.png" src="spec/fixtures/a.png" />
|
39
|
+
|
40
|
+
b.png
|
41
|
+
|
42
|
+
<img alt="b.png" src="spec/fixtures/b.png" />
|
43
|
+
|
44
|
+
a1.png
|
45
|
+
|
46
|
+
<img alt="a1.png" src="spec/fixtures/a1.png" />
|
47
|
+
|
48
|
+
### Base (Color) mode (a.png X a1.png)
|
40
49
|
|
41
50
|
Compare pixels, resulting score is a ratio of unequal pixels (with respect to provided tolerance).
|
42
51
|
|
43
52
|
Resulting diff contains version of the first image with different pixels highlighted in red and red bounding box.
|
44
53
|
|
45
|
-
<img alt=
|
54
|
+
<img alt="color_diff" src="spec/fixtures/color_diff.png" />
|
46
55
|
|
47
|
-
###
|
56
|
+
### RGB mode (a.png X b.png)
|
48
57
|
|
49
58
|
Compare pixels by values, resulting score is a ratio of unequal pixels.
|
50
59
|
Resulting diff represents per-channel difference.
|
51
60
|
|
52
|
-
<img alt=
|
61
|
+
<img alt="rgb_diff.png" src="spec/fixtures/rgb_diff.png" />
|
53
62
|
|
54
|
-
### Grayscale mode
|
63
|
+
### Grayscale mode (a.png X a1.png)
|
55
64
|
|
56
65
|
Compare pixels as grayscale (by brightness and alpha), resulting score is a ratio of unequal pixels (with respect to provided tolerance).
|
57
66
|
|
58
67
|
Resulting diff contains grayscale version of the first image with different pixels highlighted in red and red bounding box.
|
59
68
|
|
60
|
-
<img alt=
|
69
|
+
<img alt="grayscale_diff.png" src="spec/fixtures/grayscale_diff.png" />
|
61
70
|
|
62
|
-
### Delta
|
71
|
+
### Delta (a.png X a1.png)
|
63
72
|
|
64
73
|
Compare pixels using [Delta E](https://en.wikipedia.org/wiki/Color_difference) distance.
|
65
74
|
Resulting diff contains grayscale version of the first image with different pixels highlighted in red (with respect to diff score).
|
66
75
|
|
67
|
-
<img alt=
|
76
|
+
<img alt="delta_diff.png" src="spec/fixtures/delta_diff.png" />
|
68
77
|
|
69
78
|
## Usage
|
70
79
|
|
@@ -91,7 +100,7 @@ cmp.lower_threshold #=> 0.01
|
|
91
100
|
cmp = ImageCompare::Matcher.new mode: :grayscale, tolerance: 0
|
92
101
|
cmp.mode #=> ImageCompare::Modes::Grayscale
|
93
102
|
|
94
|
-
res = cmp.compare(
|
103
|
+
res = cmp.compare("path/image1.png", "path/image2.png")
|
95
104
|
res #=> ImageCompare::Result
|
96
105
|
res.match? #=> true
|
97
106
|
res.score #=> 0.0
|
@@ -101,44 +110,43 @@ res.difference_image #=> ImageCompare::Image
|
|
101
110
|
res.difference_image.save(result)
|
102
111
|
|
103
112
|
# without explicit matcher
|
104
|
-
res = ImageCompare.compare(
|
113
|
+
res = ImageCompare.compare("path/image1.png", "path/image2.png", options)
|
105
114
|
res.match? #=> true
|
106
115
|
res.score #=> 0.0
|
107
116
|
|
108
117
|
# equals to
|
109
|
-
res = ImageCompare::Matcher.new(options).compare(
|
118
|
+
res = ImageCompare::Matcher.new(options).compare("my_images_path/image1.png", "my_images_path/image2.png")
|
110
119
|
res.match? #=> true
|
111
120
|
res.score #=> 0.0
|
112
121
|
```
|
113
122
|
|
114
|
-
## Excluding rectangle
|
123
|
+
## Excluding rectangle (a.png X a1.png)
|
115
124
|
|
116
|
-
<img alt=
|
117
|
-
<img alt='a1.png' src='spec/fixtures/a1.png' />
|
125
|
+
<img alt="a1.png" src="spec/fixtures/rgb_exclude_rect.png" />
|
118
126
|
|
119
127
|
You can exclude rectangle from comparing by passing `:exclude_rect` to `compare`.
|
120
128
|
E.g., if `path_1` and `path_2` contain images above
|
121
129
|
```ruby
|
122
|
-
ImageCompare.compare(
|
130
|
+
ImageCompare.compare("path/image1.png", "path/image2.png", mode: :rgb, exclude_rect: [200, 150, 275, 200]).match? # => true
|
123
131
|
|
124
132
|
# or
|
125
133
|
|
126
|
-
cmp = ImageCompare::Matcher.new exclude_rect: [200, 150, 275, 200]
|
127
|
-
res = cmp.compare(
|
134
|
+
cmp = ImageCompare::Matcher.new mode: :rgb, exclude_rect: [200, 150, 275, 200]
|
135
|
+
res = cmp.compare("path/image1.png", "path/image2.png")
|
128
136
|
res #=> ImageCompare::Result
|
129
137
|
res.match? #=> true
|
130
138
|
res.score #=> 0.0
|
131
139
|
|
132
140
|
# Return diff image object
|
133
141
|
res.difference_image #=> ImageCompare::Image
|
134
|
-
res.difference_image.save(
|
142
|
+
res.difference_image.save("path/diff.png")
|
135
143
|
```
|
136
144
|
`[200, 150, 275, 200]` is array of two vertices of rectangle -- (200, 150) is left-top vertex and (275, 200) is right-bottom.
|
137
145
|
|
138
146
|
### Cucumber + Capybara example
|
139
147
|
`support/env.rb`:
|
140
148
|
```ruby
|
141
|
-
require
|
149
|
+
require "image_compare"
|
142
150
|
```
|
143
151
|
|
144
152
|
`support/capybara_extensions.rb`:
|
@@ -147,15 +155,15 @@ require 'image_compare'
|
|
147
155
|
|
148
156
|
Capybara::Node::Element.class_eval do
|
149
157
|
def rect_area
|
150
|
-
rect_area = self.evaluate_script(
|
158
|
+
rect_area = self.evaluate_script("this.getBoundingClientRect()") # rubocop:disable Style/RedundantSelf
|
151
159
|
|
152
160
|
[
|
153
|
-
rect_area[
|
154
|
-
rect_area[
|
155
|
-
rect_area[
|
156
|
-
rect_area[
|
161
|
+
rect_area["left"].to_f.round,
|
162
|
+
rect_area["top"].to_f.round,
|
163
|
+
rect_area["right"].to_f.round,
|
164
|
+
rect_area["bottom"].to_f.round
|
157
165
|
]
|
158
|
-
rescue StandardError => e
|
166
|
+
rescue StandardError => e # rubocop:disable Style/RescueStandardError
|
159
167
|
raise "Error getting element rect area: #{e.inspect}!"
|
160
168
|
end
|
161
169
|
end
|
@@ -163,11 +171,11 @@ end
|
|
163
171
|
|
164
172
|
`steps/my_step.rb`:
|
165
173
|
```ruby
|
166
|
-
my_element = page.find(
|
174
|
+
my_element = page.find("#my_element").rect_area
|
167
175
|
|
168
176
|
cmp = ImageCompare::Matcher.new exclude_rect: my_element.rect_area
|
169
|
-
res = cmp.compare(
|
170
|
-
res.difference_image.save(
|
177
|
+
res = cmp.compare("path/image1.png", "path/image2.png")
|
178
|
+
res.difference_image.save("path/diff.png")
|
171
179
|
|
172
180
|
expect(res.match?).to eq(true)
|
173
181
|
```
|
@@ -180,6 +188,20 @@ You can set bounds of comparing by passing `:include_rect` to `compare` with arr
|
|
180
188
|
|
181
189
|
Bug reports and pull requests are welcome on GitHub at https://github.com/instantink/image_compare.
|
182
190
|
|
191
|
+
### Running tests and code inspections
|
192
|
+
|
193
|
+
- `rake rubocop`
|
194
|
+
- `rake rubocop:md`
|
195
|
+
- `rake spec`
|
196
|
+
- `ruby examples/performance.rb` Create the "gemfile.local" file with the content below to run the performance tests:
|
197
|
+
```ruby
|
198
|
+
# frozen_string_literal: true
|
199
|
+
|
200
|
+
source "https://rubygems.org"
|
201
|
+
gem "benchmark-ips", "~> 2.7", ">= 2.7.2"
|
202
|
+
```
|
203
|
+
|
204
|
+
|
183
205
|
## License
|
184
206
|
|
185
207
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/lib/image_compare/image.rb
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
|
3
3
|
module ImageCompare
|
4
4
|
class Matcher
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
5
|
+
require "image_compare/image"
|
6
|
+
require "image_compare/result"
|
7
|
+
require "image_compare/modes"
|
8
8
|
|
9
9
|
MODES = {
|
10
|
-
rgb:
|
11
|
-
delta:
|
12
|
-
grayscale:
|
13
|
-
color:
|
10
|
+
rgb: "RGB",
|
11
|
+
delta: "Delta",
|
12
|
+
grayscale: "Grayscale",
|
13
|
+
color: "Color"
|
14
14
|
}.freeze
|
15
15
|
|
16
16
|
attr_reader :mode
|
@@ -26,25 +26,27 @@ module ImageCompare
|
|
26
26
|
b = Image.from_file(b) unless b.is_a?(Image)
|
27
27
|
|
28
28
|
unless a.sizes_match?(b)
|
29
|
-
raise
|
30
|
-
|
29
|
+
raise(
|
30
|
+
SizesMismatchError,
|
31
|
+
"Size mismatch: first image size: #{a.width}x#{a.height}, second image size: #{b.width}x#{b.height}"
|
32
|
+
)
|
31
33
|
end
|
32
34
|
|
33
35
|
image_area = Rectangle.new(0, 0, a.width - 1, a.height - 1)
|
34
36
|
|
35
37
|
unless mode.exclude_rect.nil?
|
36
38
|
unless image_area.contains?(mode.exclude_rect)
|
37
|
-
raise ArgumentError,
|
39
|
+
raise ArgumentError, "Bounds must be in image"
|
38
40
|
end
|
39
41
|
end
|
40
42
|
|
41
43
|
unless mode.include_rect.nil?
|
42
44
|
unless image_area.contains?(mode.include_rect)
|
43
|
-
raise ArgumentError,
|
45
|
+
raise ArgumentError, "Bounds must be in image"
|
44
46
|
end
|
45
47
|
unless mode.exclude_rect.nil?
|
46
48
|
unless mode.include_rect.contains?(mode.exclude_rect)
|
47
|
-
raise ArgumentError,
|
49
|
+
raise ArgumentError, "Included area must contain excluded"
|
48
50
|
end
|
49
51
|
end
|
50
52
|
end
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module ImageCompare
|
4
4
|
module Modes
|
5
|
-
require
|
5
|
+
require "image_compare/modes/base"
|
6
6
|
|
7
7
|
class Color < Base
|
8
8
|
include ColorMethods
|
@@ -33,20 +33,20 @@ module ImageCompare
|
|
33
33
|
left: bounds.bounds[0],
|
34
34
|
top: bounds.bounds[1],
|
35
35
|
right: bounds.bounds[2],
|
36
|
-
bot:
|
36
|
+
bot: bounds.bounds[3]
|
37
37
|
}
|
38
38
|
|
39
39
|
exclude_area = {
|
40
40
|
left: exclude_rect.bounds[0],
|
41
41
|
top: exclude_rect.bounds[1],
|
42
42
|
right: exclude_rect.bounds[2],
|
43
|
-
bot:
|
43
|
+
bot: exclude_rect.bounds[3]
|
44
44
|
}
|
45
45
|
|
46
46
|
diff_area[:left] <= exclude_area[:left] &&
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
diff_area[:top] <= exclude_area[:top] &&
|
48
|
+
diff_area[:right] >= exclude_area[:right] &&
|
49
|
+
diff_area[:bot] >= exclude_area[:bot]
|
50
50
|
end
|
51
51
|
|
52
52
|
def pixels_equal?(a, b)
|
data/lib/image_compare/modes.rb
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
module ImageCompare
|
4
4
|
module Modes
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
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
|
@@ -28,8 +28,8 @@ module ImageCompare
|
|
28
28
|
|
29
29
|
def contains_point?(x, y)
|
30
30
|
(x >= left && y >= top && x <= right && y <= bot) ||
|
31
|
-
|
32
|
-
|
31
|
+
(x <= right && y <= top && x >= left && y >= bot) ||
|
32
|
+
(x.between?(right, left) && y.between?(bot, top))
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
data/lib/image_compare.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "image_compare/version"
|
4
4
|
|
5
5
|
module ImageCompare
|
6
6
|
class SizesMismatchError < StandardError
|
7
7
|
end
|
8
8
|
|
9
|
-
require
|
10
|
-
require
|
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.3
|
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-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: chunky_png
|
@@ -85,7 +85,7 @@ licenses:
|
|
85
85
|
metadata:
|
86
86
|
bug_tracker_uri: https://github.com/instantink/image_compare/issues
|
87
87
|
changelog_uri: https://github.com/instantink/image_compare/blob/master/CHANGELOG.md
|
88
|
-
documentation_uri: https://
|
88
|
+
documentation_uri: https://www.rubydoc.info/gems/image_compare/1.0.3
|
89
89
|
homepage_uri: https://github.com/instantink/image_compare
|
90
90
|
source_code_uri: https://github.com/instantink/image_compare
|
91
91
|
post_install_message:
|