Skip to content

Instantly share code, notes, and snippets.

@osulyanov
Created September 15, 2017 10:09
Show Gist options
  • Save osulyanov/76f54779b48f737dc1e4a0dd18cd40ef to your computer and use it in GitHub Desktop.
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.
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