colorfulness 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.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/Gemfile +3 -0
- data/README.md +19 -0
- data/Rakefile +2 -0
- data/bin/colorfulness +12 -0
- data/colorfulness.gemspec +20 -0
- data/lib/colorfulness/calculator.rb +53 -0
- data/lib/colorfulness.rb +1 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 61edbcd378454f2eebe6c3ee9055205060b35ad6
|
4
|
+
data.tar.gz: ae0a4ab32b49c0f02705e84811aa2062c76ebc50
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3dfb82ba53fe752bc7970525e92e9dc03b3c8fd783e676ccfad4634f2936d84bc53e86ac13b153c2f301f4a2dfdf08357f67ff029c5397095b1dc00e6d1dfcf2
|
7
|
+
data.tar.gz: 38fda323aacfd78123f92066cc8e5c5a1bf2efbc63243dc7399f9880caf964a55eaba5bda79ab7b774d13fa1a0210252a8ae7781dfdb042057d7b487f071d00a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# Colorfulness
|
2
|
+
|
3
|
+
A Ruby library for calculating the colorfulness of an image, using a technique
|
4
|
+
described in ["Measuring colourfulness in natural images" (Hasler and
|
5
|
+
Susstrunk, 2003)][1] (See section 7: 'A More Efficient Metric').
|
6
|
+
|
7
|
+
It uses ChunkyPNG to read the pixels of an image, and expects to be passed
|
8
|
+
a ChunkyPNG `Image`.
|
9
|
+
|
10
|
+
For example:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
image = ChunkyPNG::Image.from_file(path)
|
14
|
+
calculator = Colorfulness::Calculator.new(image)
|
15
|
+
puts image.colorfulness
|
16
|
+
# 0.3207628365648987
|
17
|
+
```
|
18
|
+
|
19
|
+
[1]: http://infoscience.epfl.ch/record/33994/files/HaslerS03.pdf
|
data/Rakefile
ADDED
data/bin/colorfulness
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "colorfulness"
|
5
|
+
s.version = "0.0.1"
|
6
|
+
s.authors = ["Tom Taylor"]
|
7
|
+
s.email = ["tom@tomtaylor.co.uk"]
|
8
|
+
s.homepage = "https://github.com/tomtaylor/colorfulness"
|
9
|
+
s.summary = "Ruby library to calculate the colorfulness of a ChunkyPNG Image"
|
10
|
+
|
11
|
+
s.files = `git ls-files`.split($\)
|
12
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
13
|
+
s.require_paths = ["lib"]
|
14
|
+
s.executables = ["colorfulness"]
|
15
|
+
|
16
|
+
s.add_dependency "chunky_png", '~> 1.2.9'
|
17
|
+
s.add_dependency "oily_png", '~> 1.1.0'
|
18
|
+
|
19
|
+
s.add_development_dependency "rake"
|
20
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'chunky_png'
|
2
|
+
require 'oily_png'
|
3
|
+
|
4
|
+
module Colorfulness
|
5
|
+
class Calculator
|
6
|
+
|
7
|
+
def initialize(image)
|
8
|
+
@image = image
|
9
|
+
end
|
10
|
+
|
11
|
+
# Technique from https://gist.github.com/zabela/8539116, a MATLAB
|
12
|
+
# implementation of the algorithm described in "Measuring colourfulness in
|
13
|
+
# natural images" (Hasler and Susstrunk, 2003).
|
14
|
+
def colorfulness
|
15
|
+
rg = []
|
16
|
+
yb = []
|
17
|
+
|
18
|
+
(0...@image.width).each do |x|
|
19
|
+
(0...@image.height).each do |y|
|
20
|
+
pixel = @image.get_pixel(x, y)
|
21
|
+
rgb = ChunkyPNG::Color.to_truecolor_bytes(pixel)
|
22
|
+
r, g, b = rgb.map { |c| c.to_f / ChunkyPNG::Color::MAX }
|
23
|
+
|
24
|
+
rg << (r - g).abs
|
25
|
+
yb << ((0.5 * (r + g)) - b).abs
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
rg_std = calculate_standard_deviation(rg)
|
30
|
+
rg_mean = calculate_mean(rg)
|
31
|
+
|
32
|
+
yb_std = calculate_standard_deviation(yb)
|
33
|
+
yb_mean = calculate_mean(yb)
|
34
|
+
|
35
|
+
rg_yb_std = Math.sqrt(rg_std**2 + yb_std**2)
|
36
|
+
rg_yb_mean = Math.sqrt(rg_mean**2 + yb_mean**2)
|
37
|
+
|
38
|
+
rg_yb_std + (rg_yb_mean * 0.3)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def calculate_standard_deviation(array)
|
44
|
+
mean = calculate_mean(array)
|
45
|
+
Math.sqrt(array.inject(0) { |sum,val| sum + (val - mean)**2 } / array.size)
|
46
|
+
end
|
47
|
+
|
48
|
+
def calculate_mean(array)
|
49
|
+
array.inject(:+).to_f / array.size
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
data/lib/colorfulness.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'colorfulness/calculator'
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: colorfulness
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tom Taylor
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: chunky_png
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.2.9
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.2.9
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: oily_png
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.1.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- tom@tomtaylor.co.uk
|
58
|
+
executables:
|
59
|
+
- colorfulness
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- bin/colorfulness
|
68
|
+
- colorfulness.gemspec
|
69
|
+
- lib/colorfulness.rb
|
70
|
+
- lib/colorfulness/calculator.rb
|
71
|
+
homepage: https://github.com/tomtaylor/colorfulness
|
72
|
+
licenses: []
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.2.2
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Ruby library to calculate the colorfulness of a ChunkyPNG Image
|
94
|
+
test_files: []
|