image_compare 1.0.0.pre.dev → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a67b91873ee2ca3ec939803ef3ff7c7e116ce75e48d4643340aae67f48c59225
4
- data.tar.gz: 2795771468140989921e1ec9afffa2f9616f5363df7106e050ebcb36871659b0
3
+ metadata.gz: 9172a89fc7bd3779f58bf4291d045db2e9de6ce8e1d17d7eed9be6970c6faed9
4
+ data.tar.gz: f3d1a90a07c4d728ea51c2effd424d9a32b4567b53b59119a996637defebc4b7
5
5
  SHA512:
6
- metadata.gz: 39a0d7d0bc330345bc4a9624bbbd721f7206db5d5c0d5900bfaa78bb0eec90dd9c03598d683c7f165a78549a8d311edaa7e206b5773abe96672479b25a322851
7
- data.tar.gz: 3fc93372828b8354867e63ac3e80cd2998fdd025ef9e3601a239d952f7ec0bfbdff3cd148bd03c4feb1a2c9cddf0be73c39da65ad55276639f3ac392886af1fa
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='https://raw.githubusercontent.com/instantink/image_compare/master/spec/fixtures/a.png' width='300' />
37
- <img src='https://raw.githubusercontent.com/instantink/image_compare/master/spec/fixtures/b.png' width='300' />
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='https://raw.githubusercontent.com/instantink/image_compare/master/spec/fixtures/rgb_diff.png' width='300' />
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='https://raw.githubusercontent.com/instantink/image_compare/master/spec/fixtures/grayscale_diff.png' width='300' />
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='https://raw.githubusercontent.com/instantink/image_compare/master/spec/fixtures/delta_diff.png' width='300' />
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
- # create new matcher with default threshold equals to 0
65
- # and base (RGB) mode
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
- # create matcher with specific threshold
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
- # create zero-tolerance grayscale matcher
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(path_1, path_2)
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(new_path)
101
+ res.difference_image.save(result)
89
102
 
90
103
  # without explicit matcher
91
- res = ImageCompare.compare(path_1, path_2, options)
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(path_1, path_2)
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='https://raw.githubusercontent.com/instantink/image_compare/master/spec/fixtures/a.png' width='300' />
100
- <img src='https://raw.githubusercontent.com/instantink/image_compare/master/spec/fixtures/a1.png' width='300' />
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(path_1, path_2, exclude_rect: [200, 150, 275, 200]).match? # => true
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) || :rgb
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
 
@@ -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.between?(left, right) && y.between?(top, bot)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ImageCompare
4
- VERSION = '1.0.0-dev'
4
+ VERSION = '1.0.1'
5
5
  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.0.pre.dev
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-24 00:00:00.000000000 Z
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: 1.3.1
104
+ version: '0'
105
105
  requirements: []
106
106
  rubygems_version: 3.4.1
107
107
  signing_key: