Created
September 15, 2017 10:09
-
-
Save osulyanov/76f54779b48f737dc1e4a0dd18cd40ef to your computer and use it in GitHub Desktop.
Get average color of image. In this example `Image` model with `file` file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
after_commit :set_average_color | |
def set_average_color | |
return unless self == section.images.first || section.bg_color.blank? | |
section.update_attribute :bg_color, get_average_color | |
end | |
def get_average_color | |
img = Magick::Image.read(file.path).first | |
pix = img.scale(1, 1) | |
pix.to_color(pix.pixel_color(0, 0)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment