Permalink
Browse files

First import of the code.

  • Loading branch information...
1 parent 402e091 commit 9eca5edb48240ddc69114598314032677757523f @alexmchale committed May 21, 2011
Showing with 143 additions and 7 deletions.
  1. +13 −0 bin/remove_white_border
  2. +122 −1 lib/remove_white_border.rb
  3. +1 −1 lib/remove_white_border/version.rb
  4. +7 −5 remove_white_border.gemspec
View
@@ -0,0 +1,13 @@
+#!/usr/bin/env ruby
+
+require "remove_white_border"
+
+SRC = ARGV.shift
+DST = ARGV.shift
+
+if !SRC && !DST
+ puts "usage: remove_white_border INPUT"
+ puts " remove_white_border INPUT OUTPUT"
+else
+ RemoveWhiteBorder::Remover.new(SRC).write(DST)
+end
View
@@ -1,3 +1,124 @@
+require "rmagick"
+
module RemoveWhiteBorder
- # Your code goes here...
+
+ class Remover
+
+ WHITE = 60000
+
+ def initialize(filename)
+ @filename = filename
+ @image = Magick::Image.read(filename).first
+ @width = @image.columns
+ @height = @image.rows
+ end
+
+ 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
+
+ # Determines and x and y that should be good to use for scanning the image.
+ 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
+
+ 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
+
+ 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
+
+ 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
+
+ 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
+
+ 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
+
+ end
+
end
@@ -1,3 +1,3 @@
module RemoveWhiteBorder
- VERSION = "0.0.1"
+ VERSION = "1.0.0"
end
@@ -6,14 +6,16 @@ Gem::Specification.new do |s|
s.name = "remove_white_border"
s.version = RemoveWhiteBorder::VERSION
s.platform = Gem::Platform::RUBY
- s.authors = ["TODO: Write your name"]
- s.email = ["TODO: Write your email address"]
- s.homepage = ""
- s.summary = %q{TODO: Write a gem summary}
- s.description = %q{TODO: Write a gem description}
+ s.authors = ["Alex McHale"]
+ s.email = ["alexmchale@gmail.com"]
+ s.homepage = "https://github.com/alexmchale/remove_white_border"
+ s.summary = %q{This is a command line tool for removing a white border around an image.}
+ s.description = %q{This is a command line tool for removing the white border around an image, often created by scanners.}
s.rubyforge_project = "remove_white_border"
+ s.add_dependency "rmagick", "~> 2.13.1"
+
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }

0 comments on commit 9eca5ed

Please sign in to comment.