Class: Wraith::CropImages

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/wraith/crop.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Constructor Details

#initialize(config) ⇒ CropImages

Returns a new instance of CropImages.



11
12
13
# File 'lib/wraith/crop.rb', line 11

def initialize(config)
  @wraith = Wraith::Wraith.new(config)
end

Instance Attribute Details

#wraithObject (readonly)

Returns the value of attribute wraith.



9
10
11
# File 'lib/wraith/crop.rb', line 9

def wraith
  @wraith
end

Instance Method Details

#crop_if_necessary(base, compare) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wraith/crop.rb', line 23

def crop_if_necessary(base, compare)
    base_width     = image_dimensions(base)[0]
    base_height    = image_dimensions(base)[1]
    compare_width  = image_dimensions(compare)[0]
    compare_height = image_dimensions(compare)[1]

    if base_height == compare_height and base_width == compare_width
      logger.debug "Both images are exactly #{base_width}x#{base_height} - no cropping required. (#{base}, #{compare})"
      return true
    end

    max_width  = [base_width, compare_width].max
    max_height = [base_height, compare_height].max

    logger.debug "Cropping both images to #{max_width}x#{max_height}. (#{base}, #{compare})"
    [base, compare].each do |image_to_crop|
      run_crop_task(image_to_crop, max_height, max_width)
    end
end

#crop_imagesObject



15
16
17
18
19
20
21
# File 'lib/wraith/crop.rb', line 15

def crop_images
  files = Dir.glob("#{wraith.directory}/*/*.png").sort

  Parallel.each(files.each_slice(2), :in_processes => Parallel.processor_count) do |base, compare|
    crop_if_necessary base, compare
  end
end

#image_dimensions(image) ⇒ Object



47
48
49
# File 'lib/wraith/crop.rb', line 47

def image_dimensions(image)
  ImageSize.new(File.open(image, "rb").read).size
end

#run_crop_task(crop, height, width) ⇒ Object



43
44
45
# File 'lib/wraith/crop.rb', line 43

def run_crop_task(crop, height, width)
  `convert #{crop.shellescape} -background none -extent #{width}x#{height} #{crop.shellescape}`
end