Class: Kontrast::ImageHelper
- Inherits:
-
Object
- Object
- Kontrast::ImageHelper
- Defined in:
- lib/kontrast/image_helper.rb
Instance Method Summary collapse
-
#compare(output_dir, output_file_name) ⇒ Object
Uses the compare_channel function to highlight the differences between two images Docs: www.rubydoc.info/github/gemhome/rmagick/Magick%2FImage%3Acompare_channel.
- #crop(width) ⇒ Object
-
#initialize(img1_path, img2_path) ⇒ ImageHelper
constructor
A new instance of ImageHelper.
- #load_image(path) ⇒ Object
- #reload_images ⇒ Object
Constructor Details
#initialize(img1_path, img2_path) ⇒ ImageHelper
Returns a new instance of ImageHelper.
7 8 9 10 11 12 |
# File 'lib/kontrast/image_helper.rb', line 7 def initialize(img1_path, img2_path) @img1_path, @img2_path = img1_path, img2_path @img1 = load_image(@img1_path) @img2 = load_image(@img2_path) @path = Kontrast.path end |
Instance Method Details
#compare(output_dir, output_file_name) ⇒ Object
Uses the compare_channel function to highlight the differences between two images Docs: www.rubydoc.info/github/gemhome/rmagick/Magick%2FImage%3Acompare_channel
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/kontrast/image_helper.rb', line 40 def compare(output_dir, output_file_name) begin distortion_metric = Magick.const_get(Kontrast.configuration.distortion_metric) diff = @img1.compare_channel(@img2, distortion_metric) do || .highlight_color = Kontrast.configuration.highlight_color .lowlight_color = Kontrast.configuration.lowlight_color end output_path = "#{Kontrast.path}/#{output_dir}" FileUtils.mkdir_p(output_path) # Just in case diff.first.write(File.join(output_path, output_file_name)) # diff is an array, the last (second) value is the diff value, # a float between 0 and 1, 0 being the same image, 1 being an # entirely different image return diff.last rescue Magick::ImageMagickError => e puts "Error comparing images: #{e.}" # 1 means that both images are different return 1 end end |
#crop(width) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/kontrast/image_helper.rb', line 23 def crop(width) # Let's not do anything if the images are already the same size return if @img1.rows == @img2.rows # Get max height of both images max_height = [@img1.rows, @img2.rows].max # Crop Workers.map([@img1, @img2]) do |image| image.extent(width, max_height).write(image.filename) end reload_images end |
#load_image(path) ⇒ Object
19 20 21 |
# File 'lib/kontrast/image_helper.rb', line 19 def load_image(path) return Magick::Image.read(path).first end |
#reload_images ⇒ Object
14 15 16 17 |
# File 'lib/kontrast/image_helper.rb', line 14 def reload_images @img1 = load_image(@img1_path) @img2 = load_image(@img2_path) end |