Class: ImageComparator

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

Constant Summary collapse

LOG_FILE =
'./tmp/log_file.log'
LOG_ROTATE_PERIOCITY =
'monthly'
DEFAULT_TEMP_FOLDER =
'./tmp'
DEFAULT_FILE1_NAME =
'file1.png'
DEFAULT_FILE2_NAME =
'file2.png'

Instance Method Summary collapse

Constructor Details

#initializeImageComparator

Class constructor. It just initializes the logger.



19
20
21
22
23
# File 'lib/image_comparator.rb', line 19

def initialize()
  @logger = Logger.new(LOG_FILE, LOG_ROTATE_PERIOCITY)
  @logger.debug("in: initialize()")
  @areas = nil
end

Instance Method Details

#calculate_diff_image(path_res = './diff.png') ⇒ String

This method generates a png with the image differences and returns a fingerprint of the returned image.

If areas are defined, they will be used on the comparison. See #set_areas

Parameters:

  • path_res (String) (defaults to: './diff.png')

    (Optional, by default ./diff.png) It defines where the png file with the differences will be placed.

Returns:

  • (String)

    It generates a png with the image differences and returns a fingerprint of the returned image



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/image_comparator.rb', line 53

def calculate_diff_image(path_res='./diff.png')
  @logger.debug("in: calculate_diff_image (#{path_res})")
  begin
    p_image1 = @path_image1
    p_image2 = @path_image2

    unless @areas.nil?
      prepare_areas_on_files
      p_image1 = "#{DEFAULT_TEMP_FOLDER}/#{DEFAULT_FILE1_NAME}"
      p_image2 = "#{DEFAULT_TEMP_FOLDER}/#{DEFAULT_FILE2_NAME}"
    end

    images = [
        ChunkyPNG::Image.from_file(p_image1),
        ChunkyPNG::Image.from_file(p_image2)
    ]

    images.first.height.times do |y|
      images.first.row(y).each_with_index do |pixel, x|

        images.last[x,y] = rgb(
            r(pixel) + r(images.last[x,y]) - 2 * [r(pixel), r(images.last[x,y])].min,
            g(pixel) + g(images.last[x,y]) - 2 * [g(pixel), g(images.last[x,y])].min,
            b(pixel) + b(images.last[x,y]) - 2 * [b(pixel), b(images.last[x,y])].min
        )
      end
    end
    images.last.save(path_res)
    diff_fingerprint = Phashion::Image.new(path_res).fingerprint
    @logger.debug("out: calculate_diff_image (#{path_res}) - ret: #{diff_fingerprint}")
    diff_fingerprint
  rescue StandardError
    @logger.error 'There was a problem'
    raise
  ensure
    delete_temp_images
  end
end

#compareBoolean

This method return a boolean to indicate if 2 png images are compare from a pixel point of view.

If areas are defined, they will be used on the comparison. See #set_areas

Returns:

  • (Boolean)

    True if the 2 images are compare, otherwise false.



41
42
43
# File 'lib/image_comparator.rb', line 41

def compare()
  compare_pixel_perfect()
end

#compare_and_generate_diff(path_res = './diff.png') ⇒ Boolean

This method returns a boolean to indicate if the 2 images are compare, based on the threshold, and in case they are not, a png with the differences is generated.

If areas are defined, they will be used on the comparison. See #set_areas

Parameters:

  • path_res (String) (defaults to: './diff.png')

    (Optional, by default ./diff.png) It defines where the png file with the differences will be placed.

Returns:

  • (Boolean)

    True if the 2 images are compare, otherwise false. In case of being false a png image with the differences is generated.



102
103
104
105
106
107
108
# File 'lib/image_comparator.rb', line 102

def compare_and_generate_diff(path_res='./diff.png')
  @logger.debug("in:compare_and_generate_diff(#{path_res}")
  images_compares = compare()
  calculate_diff_image(path_res) unless images_compares
  @logger.debug("out:compare_and_generate_diff(#{path_res} - ret: #{images_compares}")
  images_compares
end

#define_images_to_compare(path_image1, path_image2) ⇒ Object

It defines the images to be compared.

Parameters:

  • path_image1 (String)

    The path to the first image to compare.

  • path_image2 (String)

    The path to the second image to compare.



30
31
32
33
# File 'lib/image_comparator.rb', line 30

def define_images_to_compare(path_image1, path_image2)
  @path_image1 = path_image1
  @path_image2 = path_image2
end

#set_areas(areas) ⇒ Object

This method defines the areas that will be used on the comparison methods, ie: compare, calculate_diff_image and compare_and_generate_diff methods.

The comparison methods will only focus the comparison on the areas flagged as included and will ignore the ones marked as exclude.

If the areas are defined, the comparison methods first create a temporal image for each image to compare with only the areas that will be included (if there is any) and then will remove from this image the excluded images (if any). These temporal images will be used on the comparison.

Examples:

Here you can see an example of areas.

origin_coord = {'x':828, 'y':293}
end_coord = {'x':1173, 'y':688}
area1 = {'origin': origin_coord, 'end': end_coord, 'exclude': false}
origin_coord2 = {'x':0, 'y':0}
end_coord2 = {'x':828, 'y':293}
area2 = {'origin': origin_coord2, 'end': end_coord2, 'exclude': true}
areas = [area1, area2]

Parameters:

  • areas (Array<Symbol>)

    This is an array of areas to include or exclude on the comparison methods. It has the following format:

    areas = [area1, …, areaN]

    areaX = ‘end’: point, ‘exclude’: boolean

    point = integer, ‘y’: integer



138
139
140
# File 'lib/image_comparator.rb', line 138

def set_areas(areas)
  @areas=areas
end