Class: WebImageCompare

Inherits:
Object
  • Object
show all
Includes:
ChunkyPNG::Color
Defined in:
lib/web_image_compare.rb

Overview

Compare image class

Instance Method Summary collapse

Constructor Details

#initialize(pattern_file_path, img_to_compare_path) ⇒ WebImageCompare

initialize the object with the path for the two images

Parameters:

  • pattern_file_path (String)

    Path for the pattern image

  • img_to_compare_path (String)

    Path for the image that it is to compare



12
13
14
15
16
17
# File 'lib/web_image_compare.rb', line 12

def initialize(pattern_file_path, img_to_compare_path)
  @img_to_compare_path  = img_to_compare_path
  @pattern_img_path     = pattern_file_path
  @pattern_img          = ChunkyPNG::Image.from_file(pattern_file_path) if File.file?(pattern_file_path)
  @img_to_compare       = ChunkyPNG::Image.from_file(img_to_compare_path) if File.file?(img_to_compare_path)
end

Instance Method Details

#process_image(record_mode, elements = {}) ⇒ Float

Performs the actions to obtain the final image and then performs the image comparison

Parameters:

  • record_mode (Boolean)

    true - saves the pattern image, false - performs the image comparison

  • elements (Hash) (defaults to: {})

    Elements to hide or?and crop

Returns:

  • (Float)

    Percentual difference between the two images



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/web_image_compare.rb', line 25

def process_image(record_mode, elements = {})
  # hide the elements if necessary
  hide_all_elements(elements[:hide], @img_to_compare) if elements[:hide]

  # crop the element if necessary
  crop_element(elements[:crop]) if elements[:crop]

  diff_perc = nil
  if record_mode
    @img_to_compare.save(@pattern_img_path)
    return 0
  else
    diff_perc, result_img = @img_to_compare.compare_imgs(@pattern_img)
    @img_to_compare.save("#{@img_to_compare_path}")
    result_img.save("#{@img_to_compare_path}_result")
  end
  diff_perc
end