Class: DealWithify::DealWithIt
- Inherits:
-
Object
- Object
- DealWithify::DealWithIt
- Defined in:
- lib/dealwithify.rb
Instance Method Summary collapse
- #centered_glasses ⇒ Object
- #glasses_midpoint ⇒ Object
-
#initialize(left_eye, right_eye, angle, img, rotate, flip) ⇒ DealWithIt
constructor
A new instance of DealWithIt.
- #scaled_and_rotated_glasses ⇒ Object
- #scaled_and_rotated_glasses_midpoint ⇒ Object
- #shifted_glasses(y_shift) ⇒ Object
- #shrunk_glasses_dimensions ⇒ Object
Constructor Details
#initialize(left_eye, right_eye, angle, img, rotate, flip) ⇒ DealWithIt
Returns a new instance of DealWithIt.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/dealwithify.rb', line 5 def initialize(left_eye, right_eye, angle, img, rotate, flip) @img = img @img.format = 'gif' @rotate = rotate @flip = flip @img_width = @img.columns @img_height = @img.rows @saved_glasses = nil left_eye_x = left_eye['x'] * @img_width / 100.0 left_eye_y = left_eye['y'] * @img_height / 100.0 right_eye_x = right_eye['x'] * @img_width / 100.0 right_eye_y = right_eye['y'] * @img_height / 100.0 @eye_distance = Math.sqrt((left_eye_x - right_eye_x)**2 + (left_eye_y - right_eye_y)**2) eye_midpoint_x = left_eye_x + (right_eye_x - left_eye_x)/2.0 eye_midpoint_y = left_eye_y + (right_eye_y - left_eye_y)/2.0 @eye_midpoint = [eye_midpoint_x, eye_midpoint_y] @angle = angle end |
Instance Method Details
#centered_glasses ⇒ Object
65 66 67 |
# File 'lib/dealwithify.rb', line 65 def centered_glasses align_images(self.scaled_and_rotated_glasses, @img, self.scaled_and_rotated_glasses_midpoint, @eye_midpoint) end |
#glasses_midpoint ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/dealwithify.rb', line 45 def glasses_midpoint gwidth, gheight = self.shrunk_glasses_dimensions scale = gwidth / GLASSES.columns.to_f if @flip [FLIP_GLASSES_MID_X * scale, FLIP_GLASSES_MID_Y * scale] else [ORIG_GLASSES_MID_X * scale, ORIG_GLASSES_MID_Y * scale] end end |
#scaled_and_rotated_glasses ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/dealwithify.rb', line 33 def scaled_and_rotated_glasses return @saved_glasses if @saved_glasses new_glasses = GLASSES.resize(*self.shrunk_glasses_dimensions, Magick::TriangleFilter, 0.5) new_glasses.background_color = 'none' new_glasses.format = 'png' new_glasses.flop! if @flip new_glasses.rotate!(@angle) if @rotate @saved_glasses = new_glasses end |
#scaled_and_rotated_glasses_midpoint ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/dealwithify.rb', line 55 def scaled_and_rotated_glasses_midpoint midx, midy = self.glasses_midpoint if @rotate # midx += 50 * Math.cos(@angle * Math::PI/180) # This pushes the glasses up the nose a bit. midy += (50 * Math.sin(@angle * Math::PI/180)).abs end [midx, midy] end |
#shifted_glasses(y_shift) ⇒ Object
69 70 71 72 73 |
# File 'lib/dealwithify.rb', line 69 def shifted_glasses(y_shift) glasses_mid_x, glasses_mid_y = self.scaled_and_rotated_glasses_midpoint glasses_mid_y -= y_shift align_images(self.scaled_and_rotated_glasses, @img, [glasses_mid_x, glasses_mid_y], @eye_midpoint) end |
#shrunk_glasses_dimensions ⇒ Object
26 27 28 29 30 31 |
# File 'lib/dealwithify.rb', line 26 def shrunk_glasses_dimensions new_width = @eye_distance * GLASSES_WIDTH_MULTIPLIER scale = new_width / GLASSES.columns.to_f new_height = scale * GLASSES.rows.to_f [new_width, new_height] end |