13

How do I get the RGB values of the average color of an image, where each value is 0-255? Such as "255,255,255"

I run this command to shrink the image down and it returns the 'rgba' value with the alpha channel and sometimes it gives text color names:

convert cat.png -resize 1x1\! -format "%[pixel:u]\n" info:

Output:

rgba(155,51,127,0.266087)
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
jaredcohe
  • 378
  • 1
  • 3
  • 11

3 Answers3

32

There are two aspects to my answer:

  1. Resize the original image to a 1-pixel-image. This pixel then will have the "average" color as ImageMagick's convert sees it.
  2. Output the result as the special .txt format supported by convert. This text format enumerates all pixels of an image, giving first its coordinates ($row,$column:), then its RGB or CMYK values in different formats.

Here is a command which covers both aspects in one:

convert cat.png -resize 1x1 out.txt
cat out.txt

To get the output directly in the terminal window, you could use:

convert cat.png -resize 1x1 txt:-

Example output:

convert p4.png -resize 1x1 txt:-
  # ImageMagick pixel enumeration: 1,1,255,srgb
  0,0: (189,185,184)  #BDB9B8  srgb(189,185,184)
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • 1
    Much simpler and more detailed than the accepted answer. – Stephen Talley Jun 15 '17 at 01:53
  • 3
    To get the hex code in a single shot, try `convert p4.png -resize 1x1 txt:- | grep -Po "#[[:xdigit:]]{6}"`, and further pipe that to `tr A-F a-f` if you prefer lowercase. – tremby Jul 11 '19 at 22:45
10

You can do the following to parse out just the comma-separated RGB values. It also will not return text color names.

convert cat.png -resize 1x1\! \
    -format "%[fx:int(255*r+.5)],%[fx:int(255*g+.5)],%[fx:int(255*b+.5)]" info:-

Output format should look like:

155,51,127

This should work in ImageMagick 6.3.9.1+

Aidan Feldman
  • 5,205
  • 36
  • 46
Shawn Aukstak
  • 796
  • 7
  • 8
  • 1
    It's easier to use the special output format `.txt` :-) – Kurt Pfeifle Sep 03 '14 at 13:58
  • Building on this answer, I wanted to get the hex equivalent of the RGB triplet. I'm still noobish at command-line processing, so this might not be optimal, but hopefully it'll save someone time: `convert cat.png -scale 1x1\! -format '%[fx:int(255*r+.5)],%[fx:int(255*g+.5)],%[fx:int(255*b+.5)]' info:- | sed 's/,/\n/g' | xargs -L 1 printf "%x"` – Illya Moskvin Aug 03 '16 at 06:15
  • Beware of ImageMagick's bad default handling of gamma issues; this may be especially apparent when scaling down to 1x1. A page with good info: http://www.ericbrasseur.org/gamma.html#ImageMagick – jwd Dec 04 '19 at 23:19
1

The C# way, using the NuGet Magick.Net package(s) and borrowing from the command line examples.

ImageMagick.IMagickColor<ushort> color;
using (ImageMagick.MagickImage image = new ImageMagick.MagickImage(file))
{
    // Get average color
    image.Resize(1, 1);
    color = image.GetPixels().First().ToColor();
}
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69