Class: Wraith::CompareImages

Inherits:
Object
  • Object
show all
Defined in:
lib/wraith/compare_images.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ CompareImages

Returns a new instance of CompareImages.



9
10
11
# File 'lib/wraith/compare_images.rb', line 9

def initialize(config)
  @wraith = Wraith::Wraith.new(config)
end

Instance Attribute Details

#wraithObject (readonly)

Returns the value of attribute wraith.



7
8
9
# File 'lib/wraith/compare_images.rb', line 7

def wraith
  @wraith
end

Instance Method Details

#compare_imagesObject



13
14
15
16
17
18
19
20
21
# File 'lib/wraith/compare_images.rb', line 13

def compare_images
  files = Dir.glob("#{wraith.directory}/*/*.png").sort
  Parallel.each(files.each_slice(2), in_processes: Parallel.processor_count) do |base, compare|
    diff = base.gsub(/([a-z0-9]+).png$/, 'diff.png')
    info = base.gsub(/([a-z0-9]+).png$/, 'data.txt')
    compare_task(base, compare, diff, info)
    puts 'Saved diff'
  end
end

#compare_task(base, compare, output, info) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/wraith/compare_images.rb', line 29

def compare_task(base, compare, output, info)
  cmdline = "compare -dissimilarity-threshold 1 -fuzz #{wraith.fuzz} -metric AE -highlight-color blue #{base} #{compare} #{output}"
  px_value = Open3.popen3(cmdline) { |_stdin, _stdout, stderr, _wait_thr| stderr.read }.to_f
  begin
    img_size = ImageSize.path(output).size.inject(:*)
    percentage(img_size, px_value, info)
  rescue
    File.open(info, 'w') { |file| file.write('invalid') } unless File.exist?(output)
  end
end

#percentage(img_size, px_value, info) ⇒ Object



23
24
25
26
27
# File 'lib/wraith/compare_images.rb', line 23

def percentage(img_size, px_value, info)
  pixel_count = (px_value / img_size) * 100
  rounded = pixel_count.round(2)
  File.open(info, 'w') { |file| file.write(rounded) }
end