Class: ImageComparator
- Inherits:
-
Object
- Object
- ImageComparator
- 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
-
#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.
-
#compare ⇒ Boolean
This method return a boolean to indicate if 2 png images are compare from a pixel point of view.
-
#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.
-
#define_images_to_compare(path_image1, path_image2) ⇒ Object
It defines the images to be compared.
-
#initialize ⇒ ImageComparator
constructor
Class constructor.
-
#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.
Constructor Details
#initialize ⇒ ImageComparator
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
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 |
#compare ⇒ Boolean
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
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
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.
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.
138 139 140 |
# File 'lib/image_comparator.rb', line 138 def set_areas(areas) @areas=areas end |