Class: ImageComparator

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

Constant Summary

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

- (ImageComparator) initialize(path_image1, path_image2)

Class constructor

Parameters:

  • path_image1 (String)

    The path to the first image to compare.

  • path_image2 (String)

    The path to the second image to compare.



23
24
25
26
27
28
29
# File 'image_comparator.rb', line 23

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

Instance Method Details

- (String) calculate_diff_image(path_res = './diff.png')

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



48
49
50
51
52
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
# File 'image_comparator.rb', line 48

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

- (Boolean) equal

This method return a boolean to indicate if 2 png images are equal 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 equal, otherwise false.



36
37
38
# File 'image_comparator.rb', line 36

def equal()
  equal_pixel_perfect()
end

- (Boolean) equal_and_generate_diff(path_res = './diff.png')

This method returns a boolean to indicate if the 2 images are equal, 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 equal, otherwise false. In case of being false a png image with the differences is generated.



97
98
99
100
101
102
103
# File 'image_comparator.rb', line 97

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

- (Object) set_areas(areas)

This method defines the areas that will be used on the comparison methods, ie: equal, calculate_diff_image and equal_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). This 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



133
134
135
# File 'image_comparator.rb', line 133

def set_areas(areas)
  @areas=areas
end