Class: ImageComparator
- Inherits:
-
Object
- Object
- ImageComparator
- 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)
-
- (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.
-
- (Boolean) equal
This method return a boolean to indicate if 2 png images are equal from a pixel point of view.
-
- (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.
-
- (ImageComparator) initialize(path_image1, path_image2)
constructor
Class constructor.
-
- (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.
Constructor Details
- (ImageComparator) initialize(path_image1, path_image2)
Class constructor
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
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
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
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.
133 134 135 |
# File 'image_comparator.rb', line 133 def set_areas(areas) @areas=areas end |