Skip to content

Instantly share code, notes, and snippets.

@devishot
Last active August 29, 2015 13:57
Show Gist options
  • Save devishot/9374587 to your computer and use it in GitHub Desktop.
Save devishot/9374587 to your computer and use it in GitHub Desktop.
example of how to convert image to "-format %c histogram:info:" (http://www.imagemagick.org/Usage/files/#histogram) by gem "mini_magick". It used below to get dominant color of image by this solution for ImageMagick http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=12033
require "mini_magick"
module MiniMagick
class Image
def get_dominant_color
color = run_command("convert", path, "-format", "%c\n", "-colors", 1, "-depth", 8, "histogram:info:").split(' ');
# color = " 1764000: (208,195,161) #D0C3A1 srgb(208,195,161)\n\n"
{
hex: color[2],
rgb: color[1][1..-2].split(',')
}
end
end
end
i = MiniMagick::Image.open("/home/a.jpg")
p i.get_dominant_color()[:hex]
p i.get_dominant_color()[:rgb]
@MohHeader
Copy link

Thanks, it is really useful,
I just updated my usage to get Hex code using Regex,

I think Regex will be much accurate,
hex: color[/#[A-Fa-f0-9]{6}|#[A-Fa-f0-9]{3}/]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment