Class: RemoveWhiteBorder::Remover
- Inherits:
-
Object
- Object
- RemoveWhiteBorder::Remover
- Defined in:
- lib/remove_white_border.rb
Constant Summary collapse
- WHITE =
60000
Instance Method Summary collapse
-
#calculate_center(x = nil, y = nil, width = nil, height = nil) ⇒ Object
Determines and x and y that should be good to use for scanning the image.
-
#initialize(filename) ⇒ Remover
constructor
A new instance of Remover.
- #nonwhite_bottom ⇒ Object
- #nonwhite_left ⇒ Object
- #nonwhite_right ⇒ Object
- #nonwhite_top ⇒ Object
- #white?(x, y) ⇒ Boolean
- #write(output = nil) ⇒ Object
Constructor Details
#initialize(filename) ⇒ Remover
Returns a new instance of Remover.
9 10 11 12 13 14 |
# File 'lib/remove_white_border.rb', line 9 def initialize(filename) @filename = filename @image = Magick::Image.read(filename).first @width = @image.columns @height = @image.rows end |
Instance Method Details
#calculate_center(x = nil, y = nil, width = nil, height = nil) ⇒ Object
Determines and x and y that should be good to use for scanning the image.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/remove_white_border.rb', line 28 def calculate_center(x = nil, y = nil, width = nil, height = nil) x ||= @width / 2 y ||= @height / 2 width ||= @width height ||= @height return @coords if @coords return (@coords = [x, y]) if !white?(x, y) return if width <= 2 || height <= 2 if coords = calculate_center(x - width/2, y - height/2, width/2, height/2) return coords end if coords = calculate_center(x - width/2, y + height/2, width/2, height/2) return coords end if coords = calculate_center(x + width/2, y - height/2, width/2, height/2) return coords end if coords = calculate_center(x + width/2, y + height/2, width/2, height/2) return coords end end |
#nonwhite_bottom ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/remove_white_border.rb', line 69 def nonwhite_bottom # Find a good x to use. x0 = calculate_center[0] # Start with a quick scan up to find the first y that is not white. y = @height - 1 y -= 1 while (y > 0) && white?(x0, y) # Now move back down until we find a row that is all white. y += 1 while (y < @height - 1) && !white?(nil, y) return y end |
#nonwhite_left ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/remove_white_border.rb', line 83 def nonwhite_left # Find a good y to use. y0 = calculate_center[1] # Start with a quick scan right to find the first x that is not white. x = 0 x += 1 while (x < @width - 1) && white?(x, y0) # Now move back left until we find a column that is all white. x -= 1 while (x > 0) && !white?(x, nil) return x end |
#nonwhite_right ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/remove_white_border.rb', line 97 def nonwhite_right # Find a good y to use. y0 = calculate_center[1] # Start with a quick scan left to find the first x that is not white. x = @width - 1 x -= 1 while (x > 0) && white?(x, y0) # Now move back right until we find a column that is all white. x += 1 while (x < @width - 1) && !white?(x, nil) return x end |
#nonwhite_top ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/remove_white_border.rb', line 55 def nonwhite_top # Find a good x to use. x0 = calculate_center[0] # Start with a quick scan down to find the first y that is not white. y = 0 y += 1 while (y < @height - 1) && white?(x0, y) # Now move back up until we find a row that is all white. y -= 1 while (y > 0) && !white?(nil, y) return y end |
#white?(x, y) ⇒ Boolean
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/remove_white_border.rb', line 16 def white?(x, y) if x && y p = @image.pixel_color(x, y) %w( red green blue ).all? { |c| p.send(c) >= WHITE } elsif x (0 ... @height).all? { |y| white? x, y } elsif y (0 ... @width).all? { |x| white? x, y } end end |
#write(output = nil) ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/remove_white_border.rb', line 111 def write(output = nil) output ||= @filename x = nonwhite_left y = nonwhite_top width = nonwhite_right - x height = nonwhite_bottom - y @image.crop(x, y, width, height).write(output) end |