remove_white_border 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENSE +19 -0
- data/README.markdown +31 -0
- data/Rakefile +2 -0
- data/bin/remove_white_border +13 -0
- data/lib/remove_white_border.rb +124 -0
- data/lib/remove_white_border/version.rb +3 -0
- data/remove_white_border.gemspec +23 -0
- metadata +91 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011 Alex McHale (alexmchale@gmail.com)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
remove_white_border
|
2
|
+
===================
|
3
|
+
|
4
|
+
Description
|
5
|
+
-----------
|
6
|
+
|
7
|
+
This is a command line tool for removing the white border created on images by
|
8
|
+
a scanner.
|
9
|
+
|
10
|
+
Installation
|
11
|
+
------------
|
12
|
+
|
13
|
+
gem install remove_white_border
|
14
|
+
|
15
|
+
Runtime Dependencies
|
16
|
+
--------------------
|
17
|
+
|
18
|
+
* RMagick: Gem used for image manipulation
|
19
|
+
* ImageMagick: Used by RMagick
|
20
|
+
|
21
|
+
Usage
|
22
|
+
-----
|
23
|
+
|
24
|
+
remove_white_border MyImage.png
|
25
|
+
|
26
|
+
remove_white_border MyImage.png MyNewImage.png
|
27
|
+
|
28
|
+
License
|
29
|
+
-------
|
30
|
+
|
31
|
+
See LICENSE file.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "remove_white_border"
|
4
|
+
|
5
|
+
SRC = ARGV.shift
|
6
|
+
DST = ARGV.shift
|
7
|
+
|
8
|
+
if !SRC && !DST
|
9
|
+
puts "usage: remove_white_border IMAGE (replaces the existing file)"
|
10
|
+
puts " remove_white_border INPUT OUTPUT (creates the output file)"
|
11
|
+
else
|
12
|
+
RemoveWhiteBorder::Remover.new(SRC).write(DST)
|
13
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require "rmagick"
|
2
|
+
|
3
|
+
module RemoveWhiteBorder
|
4
|
+
|
5
|
+
class Remover
|
6
|
+
|
7
|
+
WHITE = 60000
|
8
|
+
|
9
|
+
def initialize(filename)
|
10
|
+
@filename = filename
|
11
|
+
@image = Magick::Image.read(filename).first
|
12
|
+
@width = @image.columns
|
13
|
+
@height = @image.rows
|
14
|
+
end
|
15
|
+
|
16
|
+
def white?(x, y)
|
17
|
+
if x && y
|
18
|
+
p = @image.pixel_color(x, y)
|
19
|
+
%w( red green blue ).all? { |c| p.send(c) >= WHITE }
|
20
|
+
elsif x
|
21
|
+
(0 ... @height).all? { |y| white? x, y }
|
22
|
+
elsif y
|
23
|
+
(0 ... @width).all? { |x| white? x, y }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Determines and x and y that should be good to use for scanning the image.
|
28
|
+
def calculate_center(x = nil, y = nil, width = nil, height = nil)
|
29
|
+
x ||= @width / 2
|
30
|
+
y ||= @height / 2
|
31
|
+
width ||= @width
|
32
|
+
height ||= @height
|
33
|
+
|
34
|
+
return @coords if @coords
|
35
|
+
return (@coords = [x, y]) if !white?(x, y)
|
36
|
+
return if width <= 2 || height <= 2
|
37
|
+
|
38
|
+
if coords = calculate_center(x - width/2, y - height/2, width/2, height/2)
|
39
|
+
return coords
|
40
|
+
end
|
41
|
+
|
42
|
+
if coords = calculate_center(x - width/2, y + height/2, width/2, height/2)
|
43
|
+
return coords
|
44
|
+
end
|
45
|
+
|
46
|
+
if coords = calculate_center(x + width/2, y - height/2, width/2, height/2)
|
47
|
+
return coords
|
48
|
+
end
|
49
|
+
|
50
|
+
if coords = calculate_center(x + width/2, y + height/2, width/2, height/2)
|
51
|
+
return coords
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def nonwhite_top
|
56
|
+
# Find a good x to use.
|
57
|
+
x0 = calculate_center[0]
|
58
|
+
|
59
|
+
# Start with a quick scan down to find the first y that is not white.
|
60
|
+
y = 0
|
61
|
+
y += 1 while (y < @height - 1) && white?(x0, y)
|
62
|
+
|
63
|
+
# Now move back up until we find a row that is all white.
|
64
|
+
y -= 1 while (y > 0) && !white?(nil, y)
|
65
|
+
|
66
|
+
return y
|
67
|
+
end
|
68
|
+
|
69
|
+
def nonwhite_bottom
|
70
|
+
# Find a good x to use.
|
71
|
+
x0 = calculate_center[0]
|
72
|
+
|
73
|
+
# Start with a quick scan up to find the first y that is not white.
|
74
|
+
y = @height - 1
|
75
|
+
y -= 1 while (y > 0) && white?(x0, y)
|
76
|
+
|
77
|
+
# Now move back down until we find a row that is all white.
|
78
|
+
y += 1 while (y < @height - 1) && !white?(nil, y)
|
79
|
+
|
80
|
+
return y
|
81
|
+
end
|
82
|
+
|
83
|
+
def nonwhite_left
|
84
|
+
# Find a good y to use.
|
85
|
+
y0 = calculate_center[1]
|
86
|
+
|
87
|
+
# Start with a quick scan right to find the first x that is not white.
|
88
|
+
x = 0
|
89
|
+
x += 1 while (x < @width - 1) && white?(x, y0)
|
90
|
+
|
91
|
+
# Now move back left until we find a column that is all white.
|
92
|
+
x -= 1 while (x > 0) && !white?(x, nil)
|
93
|
+
|
94
|
+
return x
|
95
|
+
end
|
96
|
+
|
97
|
+
def nonwhite_right
|
98
|
+
# Find a good y to use.
|
99
|
+
y0 = calculate_center[1]
|
100
|
+
|
101
|
+
# Start with a quick scan left to find the first x that is not white.
|
102
|
+
x = @width - 1
|
103
|
+
x -= 1 while (x > 0) && white?(x, y0)
|
104
|
+
|
105
|
+
# Now move back right until we find a column that is all white.
|
106
|
+
x += 1 while (x < @width - 1) && !white?(x, nil)
|
107
|
+
|
108
|
+
return x
|
109
|
+
end
|
110
|
+
|
111
|
+
def write(output = nil)
|
112
|
+
output ||= @filename
|
113
|
+
|
114
|
+
x = nonwhite_left
|
115
|
+
y = nonwhite_top
|
116
|
+
width = nonwhite_right - x
|
117
|
+
height = nonwhite_bottom - y
|
118
|
+
|
119
|
+
@image.crop(x, y, width, height).write(output)
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "remove_white_border/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "remove_white_border"
|
7
|
+
s.version = RemoveWhiteBorder::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Alex McHale"]
|
10
|
+
s.email = ["alexmchale@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/alexmchale/remove_white_border"
|
12
|
+
s.summary = %q{This is a command line tool for removing a white border around an image.}
|
13
|
+
s.description = %q{This is a command line tool for removing the white border around an image, often created by scanners.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "remove_white_border"
|
16
|
+
|
17
|
+
s.add_dependency "rmagick", "~> 2.13.1"
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: remove_white_border
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Alex McHale
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-05-21 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rmagick
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 57
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 13
|
33
|
+
- 1
|
34
|
+
version: 2.13.1
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description: This is a command line tool for removing the white border around an image, often created by scanners.
|
38
|
+
email:
|
39
|
+
- alexmchale@gmail.com
|
40
|
+
executables:
|
41
|
+
- remove_white_border
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- LICENSE
|
50
|
+
- README.markdown
|
51
|
+
- Rakefile
|
52
|
+
- bin/remove_white_border
|
53
|
+
- lib/remove_white_border.rb
|
54
|
+
- lib/remove_white_border/version.rb
|
55
|
+
- remove_white_border.gemspec
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: https://github.com/alexmchale/remove_white_border
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project: remove_white_border
|
86
|
+
rubygems_version: 1.4.2
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: This is a command line tool for removing a white border around an image.
|
90
|
+
test_files: []
|
91
|
+
|