class FaceCropper::FaceBox

Attributes

height[R]
left[R]
top[R]
width[R]

Public Class Methods

new(top: , left: , height: , width: , margin: 0) click to toggle source
# File lib/face_cropper/face_box.rb, line 7
def initialize(top: , left: , height: , width: , margin: 0)
  @top    = top
  @left   = left
  @height = height
  @width  = width
  @margin = margin
end

Public Instance Methods

calculate_position(image_width: , image_height:) click to toggle source
# File lib/face_cropper/face_box.rb, line 28
def calculate_position(image_width: , image_height:)
  {
    width:  (@width  * image_width).to_i  + @margin,
    height: (@height * image.height).to_i + @margin,
    x:      [(@top   * image.height).to_i - @margin, 0].min,
    y:      [(@left  * image.width).to_i  - @margin, 0].min
  }
end
crop_face!(image_path) click to toggle source
# File lib/face_cropper/face_box.rb, line 15
def crop_face!(image_path)
  image = MiniMagick::Image.open(image_path)
  position = calculate_position(image_width: image.width, image_height: image.height)

  crop_params = "#{position[:width]}x#{position[:height]}+#{position[:y]}+#{position[:x]}"

  image.crop(crop_params)
  crop_file = "#{crop_params}_#{@image_key}"
  image.write(crop_file)

  crop_file
end