Camalian
Ruby gem to extract color palettes from images and play with their saturation
Installation
Add this line to your application's Gemfile:
gem 'camalian', '~> 0.2.0'
And then execute:
$ bundle
Or install it yourself as:
$ gem install camalian
Usage
image = Camalian::load('file_path')
colors = image.prominent_colors(15)
colors = colors.sort_similar_colors
colors.light_colors(0, 40)
You can find a working example with detail explanation and reference code here on this link. Here we will build a functional color based image search engine in Ruby on Rails.
NOTE: Since its a compute intensive operation so for production use its suggested to use under a background job and not within a request/response cycle.
Quantization Algorithms
Currently following algorithms are implemented.
Histogram
Its a most common algorithm for color quantization and used different bucket technique to group the colors together. You can read more about this technique here. It can be accessed by Camalian::QUANTIZATION_HISTOGRAM
constant. This is used as default method as well.
K Means
This algorithm uses color distancing in RGB space to group the similar colors. You can learn more about this technique here. It can be accessed by Camalian::QUANTIZATION_K_MEANS
constant.
Median Cut
This algorithm uses color highest color range to determine the median and split colors to groups. The output consists of average color of such color groups. Since these algorithm don't use actual colors and instead average, so you will may not exact matching pixel in the image. This algorithm is nice to be used with image compression, where similarity and compression is important than having same pixel colors. You can learn more about this technique here . It can be accessed by Camalian::QUANTIZATION_MEDIAN_CUT
constant.
You can set default quantization method globally as:
Camalian.options[:quantization] = Camalian::QUANTIZATION_K_MEANS
or you can set at the time of extracting colors by.
image = Camalian::load('file_path')
colors = image.prominent_colors(15, quantization: Camalian::QUANTIZATION_K_MEANS)
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request