pixel_matcher 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9e709cf7a1171cf3646e11ee33fdd847c90d9eb087b61bcfedaa760d7b3fbf46
4
- data.tar.gz: 1cecb8fa4d4af025d8c0cef94f6f6598d1a8feefeb93a22fb60538705be50ff5
3
+ metadata.gz: 9878d36f90fea82db8e1d5411b36b5d72d20d8353de7829ac7d90774ceb49396
4
+ data.tar.gz: f7bfb4d812e76a101ff2405af772c2d6706ff899c05d606984cb71c75440c181
5
5
  SHA512:
6
- metadata.gz: 2322258ac93036c4e42731e2ed58fe62fbdef5bc98e2c60bcd0c27bd43025f66be3ceb05615847c3c93aa98cb19fa49933427d49a04784046ba7997b87deed2f
7
- data.tar.gz: c9a92b95861ad934c8e0f90d0c5fbe70806ceab5c3b740a093cd820e8abf04b2b18ecfac2f3f29e7858e355b3b207a1c8d31af32b5b51d1fc911bb9ff9c055ed
6
+ metadata.gz: 12546f60584e1c87744457e5eb8961cd5ceca0b27fefff265f1014c22cc9aee4c8559ce6023aabde4fdc30593774da2fecb981e3428939e9a34f6797dffe1f68
7
+ data.tar.gz: a2a76e6ffa2e0acb99434fbd336be3b70b4488ac9cf2903aba1af358bce04091209b2dc17d3eb6c3385483b25bacea0c047c09af31d30261785f8a1136b2c993
data/.gitignore CHANGED
@@ -9,3 +9,5 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+
13
+ /vendor/bundle
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pixel_matcher (0.0.5)
4
+ pixel_matcher (0.0.6)
5
5
  rmagick (~> 4.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,8 +1,15 @@
1
1
  # PixelMatcher
2
+ ![](https://ruby-gem-downloads-badge.herokuapp.com/pixel_matcher)
3
+ [![Gem Version](https://badge.fury.io/rb/pixel_matcher.svg)](https://badge.fury.io/rb/pixel_matcher)
4
+ [![Build Status](https://travis-ci.org/ksk001100/pixel_matcher.svg?branch=master)](https://travis-ci.org/ksk001100/pixel_matcher)
5
+ ![](https://img.shields.io/github/issues/ksk001100/pixel_matcher.svg)
6
+ ![](https://img.shields.io/github/forks/ksk001100/pixel_matcher.svg)
7
+ ![](https://img.shields.io/github/license/ksk001100/pixel_matcher.svg)
8
+
2
9
 
3
10
  Library to compare images and generate difference image files
4
11
 
5
- ![](images/screen_shot.jpeg)
12
+ ![](images/generate.png)
6
13
 
7
14
  ## Installation
8
15
 
@@ -28,18 +35,21 @@ require 'pixel_matcher'
28
35
  # from rmagick image
29
36
  require 'rmagick'
30
37
  diff = PixelMatcher::DiffImage.new(Magick::Image.read('img1.png').first, Magick::Image.read('img2.png').first)
31
- diff.export_diff('diff.png')
32
- diff.export_gray_scale('gray_scale.png')
38
+ diff.export('diff.png') # or mode: :only
39
+ diff.export('gray_scale.png', mode: :gray_scale)
40
+ diff.export('compare.png', mode: :compare)
33
41
 
34
42
  # from file path
35
43
  diff = PixelMatcher::DiffImage.from_path('img1.png', 'img2.png')
36
- diff.export_diff('diff.png')
37
- diff.export_gray_scale('gray_scale.png')
44
+ diff.export('diff.png') # or mode: :only
45
+ diff.export('gray_scale.png', mode: :gray_scale)
46
+ diff.export('compare.png', mode: :compare)
38
47
 
39
48
  # from blob
40
49
  diff = PixelMatcher::DiffImage.from_blob(File.read('img1.png'), File.read('img2.png'))
41
- diff.export_diff('diff.png')
42
- diff.export_gray_scale('gray_scale.png')
50
+ diff.export('diff.png') # or mode: :only
51
+ diff.export('gray_scale.png', mode: :gray_scale)
52
+ diff.export('compare.png', mode: :compare)
43
53
  ```
44
54
 
45
55
 
Binary file
data/lib/pixel_matcher.rb CHANGED
@@ -2,5 +2,15 @@ require "pixel_matcher/version"
2
2
  require 'pixel_matcher/diff_image'
3
3
 
4
4
  module PixelMatcher
5
- class SizeMismatchError < StandardError; end
5
+ class SizeMismatchError < StandardError
6
+ def initialize(msg = 'Image size mismatch...')
7
+ super
8
+ end
9
+ end
10
+
11
+ class ModeMismatchError < StandardError
12
+ def initialize(msg = 'Undefined mode...')
13
+ super
14
+ end
15
+ end
6
16
  end
@@ -2,30 +2,50 @@ require 'rmagick'
2
2
 
3
3
  module PixelMatcher
4
4
  class DiffImage < Magick::Image
5
- def initialize(img1, img2)
6
- self.class.image_size_validate(img1, img2)
7
- @img1 = img1
8
- @img2 = img2
9
- super(@img1.columns, @img1.rows) { self.background_color = 'none' }
5
+ def initialize(original, changed)
6
+ self.class.image_size_validate!(original, changed)
7
+
8
+ @original = original
9
+ @changed = changed
10
+ super(@original.columns, @original.rows) { self.background_color = 'none' }
10
11
  store_diff
11
12
  end
12
13
 
13
14
  def self.from_path(path1, path2)
14
- img1 = Magick::Image.read(path1).first
15
- img2 = Magick::Image.read(path2).first
16
- new(img1, img2)
15
+ original = Magick::Image.read(path1).first
16
+ changed = Magick::Image.read(path2).first
17
+ new(original, changed)
17
18
  end
18
19
 
19
20
  def self.from_blob(bin1, bin2)
20
- img1 = Magick::Image.from_blob(bin1).first
21
- img2 = Magick::Image.from_blob(bin2).first
22
- new(img1, img2)
21
+ original = Magick::Image.from_blob(bin1).first
22
+ changed = Magick::Image.from_blob(bin2).first
23
+ new(original, changed)
24
+ end
25
+
26
+ def export(output_path, mode: :only)
27
+ case mode
28
+ when :only then export_diff(output_path)
29
+ when :gray_scale then export_gray_scale(output_path)
30
+ when :compare then export_compare(output_path)
31
+ else self.class.mode_validate!
32
+ end
23
33
  end
24
34
 
25
- def self.image_size_validate(img1, img2)
26
- return if [img1.columns, img1.rows] == [img2.columns, img2.rows]
35
+ private
36
+
37
+ def store_diff
38
+ @original.columns.times do |i|
39
+ @original.rows.times do |j|
40
+ next if eq_pixel?(i, j)
41
+
42
+ store_pixels(i, j, 1, 1, [@original.pixel_color(i, j)])
43
+ end
44
+ end
45
+ end
27
46
 
28
- raise PixelMatcher::SizeMismatchError, 'Image size mismatch'
47
+ def eq_pixel?(column, row)
48
+ @original.pixel_color(column, row) == @changed.pixel_color(column, row)
29
49
  end
30
50
 
31
51
  def export_diff(output_path)
@@ -34,26 +54,29 @@ module PixelMatcher
34
54
  end
35
55
 
36
56
  def export_gray_scale(output_path)
37
- gray_scale = @img1.clone.quantize(256, Magick::GRAYColorspace)
38
- gray_scale.composite!(self, Magick::CenterGravity, 0, 0, Magick::OverCompositeOp);
57
+ gray_scale = @original.clone.quantize(256, Magick::GRAYColorspace)
58
+ gray_scale.composite!(self, Magick::CenterGravity, 0, 0, Magick::OverCompositeOp)
39
59
  gray_scale.write(output_path)
40
60
  self
41
61
  end
42
62
 
43
- private
63
+ def export_compare(output_path)
64
+ @changed.compare_channel(@original, Magick::MeanSquaredErrorMetric)
65
+ .first
66
+ .write(output_path)
67
+ self
68
+ end
44
69
 
45
- def store_diff
46
- @img1.columns.times do |i|
47
- @img1.rows.times do |j|
48
- next if eq_pixel?(i, j)
70
+ class << self
71
+ def image_size_validate!(original, changed)
72
+ return if [original.columns, original.rows] == [changed.columns, changed.rows]
49
73
 
50
- store_pixels(i, j, 1, 1, [@img1.pixel_color(i, j)])
51
- end
74
+ raise PixelMatcher::SizeMismatchError
52
75
  end
53
- end
54
76
 
55
- def eq_pixel?(column, row)
56
- @img1.pixel_color(column, row) == @img2.pixel_color(column, row)
77
+ def mode_validate!
78
+ raise PixelMatcher::ModeMismatchError
79
+ end
57
80
  end
58
81
  end
59
82
  end
@@ -1,3 +1,3 @@
1
1
  module PixelMatcher
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pixel_matcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - ksk001100
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-02 00:00:00.000000000 Z
11
+ date: 2020-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rmagick
@@ -114,7 +114,7 @@ files:
114
114
  - bin/console
115
115
  - bin/setup
116
116
  - exe/pixel_matcher
117
- - images/screen_shot.jpeg
117
+ - images/generate.png
118
118
  - lib/pixel_matcher.rb
119
119
  - lib/pixel_matcher/diff_image.rb
120
120
  - lib/pixel_matcher/version.rb
Binary file