prizm 0.0.1
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 +5 -0
- data/Gemfile +4 -0
- data/README.md +5 -0
- data/Rakefile +1 -0
- data/lib/prizm.rb +36 -0
- data/lib/prizm/version.rb +3 -0
- data/prizm.gemspec +25 -0
- data/test/rainbow.jpg +0 -0
- data/test/test.rb +20 -0
- metadata +88 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/prizm.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'RMagick'
|
3
|
+
|
4
|
+
module Prizm
|
5
|
+
class Extractor
|
6
|
+
def get_colors(img_location, nc=6, remove_bg=true)
|
7
|
+
begin
|
8
|
+
image = remove_bg ? remove_bg(img_location, nc) : reduce_size(Magick::ImageList.new(img_location), 100, 100)
|
9
|
+
q = image.quantize(nc)
|
10
|
+
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
|
+
end
|
17
|
+
|
18
|
+
def to_hex(pixel)
|
19
|
+
pixel.to_color(Magick::AllCompliance, false, 8)
|
20
|
+
end
|
21
|
+
|
22
|
+
def reduce_size(image, width, height)
|
23
|
+
image.change_geometry!("#{width}x#{height}") { |cols, rows, img| img.resize!(cols, rows) }
|
24
|
+
end
|
25
|
+
|
26
|
+
def remove_bg(img_location, nc, fuzz=20)
|
27
|
+
image = reduce_size Magick::ImageList.new(img_location), 100, 100
|
28
|
+
image = image.border(1, 1, image.pixel_color(0,0))
|
29
|
+
image = image.quantize(nc+4)
|
30
|
+
image.fuzz = "#{fuzz}%"
|
31
|
+
a = image.color_floodfill(0, 0, image.pixel_color(0,0))
|
32
|
+
a = a.matte_floodfill(0,0)
|
33
|
+
a
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/prizm.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "prizm/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "prizm"
|
7
|
+
s.version = Prizm::VERSION
|
8
|
+
s.authors = ["Quan Nguyen"]
|
9
|
+
s.email = ["mquannie@gmail.com <mailto:mquannie@gmail.com>"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Prizm is a ruby gem that extracts colors from an input image}
|
12
|
+
s.description = %q{Prizm uses rmagick to extract colors from images}
|
13
|
+
|
14
|
+
s.rubyforge_project = "prizm"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "rmagick"
|
22
|
+
# specify any dependencies here; for example:
|
23
|
+
# s.add_development_dependency "rspec"
|
24
|
+
# s.add_runtime_dependency "rest-client"
|
25
|
+
end
|
data/test/rainbow.jpg
ADDED
Binary file
|
data/test/test.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/../lib/prizm"
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TestExtractor < Test::Unit::TestCase
|
5
|
+
def test_functional
|
6
|
+
colors = Prizm::Extractor.new.get_colors("#{File.dirname(__FILE__)}/rainbow.jpg", 6, false)
|
7
|
+
assert_equal(6, colors.size)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_to_hex
|
11
|
+
extr = Prizm::Extractor.new
|
12
|
+
colors = extr.get_colors("#{File.dirname(__FILE__)}/rainbow.jpg", 6, false)
|
13
|
+
assert_equal('#322D31', extr.to_hex(colors[0]))
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_error
|
17
|
+
c = Prizm::Extractor.new.get_colors("blahblah.jpg")
|
18
|
+
assert_equal(nil, c)
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prizm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Quan Nguyen
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-12-22 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rmagick
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Prizm uses rmagick to extract colors from images
|
35
|
+
email:
|
36
|
+
- mquannie@gmail.com <mailto:mquannie@gmail.com>
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- README.md
|
47
|
+
- Rakefile
|
48
|
+
- lib/prizm.rb
|
49
|
+
- lib/prizm/version.rb
|
50
|
+
- prizm.gemspec
|
51
|
+
- test/rainbow.jpg
|
52
|
+
- test/test.rb
|
53
|
+
homepage: ""
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project: prizm
|
82
|
+
rubygems_version: 1.8.6
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Prizm is a ruby gem that extracts colors from an input image
|
86
|
+
test_files:
|
87
|
+
- test/rainbow.jpg
|
88
|
+
- test/test.rb
|