prizm 0.0.1 → 0.0.2
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/lib/prizm.rb +10 -13
- data/lib/prizm/version.rb +1 -1
- data/test/test.rb +4 -5
- metadata +3 -3
data/lib/prizm.rb
CHANGED
@@ -3,34 +3,31 @@ require 'RMagick'
|
|
3
3
|
|
4
4
|
module Prizm
|
5
5
|
class Extractor
|
6
|
-
def
|
7
|
-
|
8
|
-
|
6
|
+
def initialize(img_location)
|
7
|
+
@image = Magick::ImageList.new(img_location)
|
8
|
+
end
|
9
|
+
|
10
|
+
def get_colors(nc=6, remove_bg=true)
|
11
|
+
image = remove_bg ? remove_bg(nc) : reduce_size(100, 100)
|
9
12
|
q = image.quantize(nc)
|
10
13
|
colors = q.color_histogram.sort {|a, b| b[1] <=> a[1]}.map { |color, count| color }
|
11
|
-
rescue Magick::ImageMagickError => e
|
12
|
-
puts e.message
|
13
|
-
puts e.backtrace
|
14
|
-
nil
|
15
|
-
end
|
16
14
|
end
|
17
15
|
|
18
16
|
def to_hex(pixel)
|
19
17
|
pixel.to_color(Magick::AllCompliance, false, 8)
|
20
18
|
end
|
21
19
|
|
22
|
-
def reduce_size(
|
23
|
-
image.change_geometry!("#{width}x#{height}") { |cols, rows, img| img.resize!(cols, rows) }
|
20
|
+
def reduce_size(width, height)
|
21
|
+
@image.change_geometry!("#{width}x#{height}") { |cols, rows, img| img.resize!(cols, rows) }
|
24
22
|
end
|
25
23
|
|
26
|
-
def remove_bg(
|
27
|
-
image = reduce_size
|
24
|
+
def remove_bg(nc, fuzz=20)
|
25
|
+
image = reduce_size(100, 100)
|
28
26
|
image = image.border(1, 1, image.pixel_color(0,0))
|
29
27
|
image = image.quantize(nc+4)
|
30
28
|
image.fuzz = "#{fuzz}%"
|
31
29
|
a = image.color_floodfill(0, 0, image.pixel_color(0,0))
|
32
30
|
a = a.matte_floodfill(0,0)
|
33
|
-
a
|
34
31
|
end
|
35
32
|
end
|
36
33
|
end
|
data/lib/prizm/version.rb
CHANGED
data/test/test.rb
CHANGED
@@ -3,18 +3,17 @@ require 'test/unit'
|
|
3
3
|
|
4
4
|
class TestExtractor < Test::Unit::TestCase
|
5
5
|
def test_functional
|
6
|
-
colors = Prizm::Extractor.new
|
6
|
+
colors = Prizm::Extractor.new("#{File.dirname(__FILE__)}/rainbow.jpg").get_colors(6, false)
|
7
7
|
assert_equal(6, colors.size)
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_to_hex
|
11
|
-
extr = Prizm::Extractor.new
|
12
|
-
colors = extr.get_colors(
|
11
|
+
extr = Prizm::Extractor.new("#{File.dirname(__FILE__)}/rainbow.jpg")
|
12
|
+
colors = extr.get_colors(6, false)
|
13
13
|
assert_equal('#322D31', extr.to_hex(colors[0]))
|
14
14
|
end
|
15
15
|
|
16
16
|
def test_error
|
17
|
-
|
18
|
-
assert_equal(nil, c)
|
17
|
+
assert_raise(Magick::ImageMagickError) { Prizm::Extractor.new("blahblah.jpg").get_colors }
|
19
18
|
end
|
20
19
|
end
|
metadata
CHANGED