How to find average color of an image with ImageMagick?

后端 未结 2 1624
逝去的感伤
逝去的感伤 2020-12-16 18:46

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

相关标签:
2条回答
  • 2020-12-16 19:25

    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)
    
    0 讨论(0)
  • 2020-12-16 19:27

    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+

    0 讨论(0)
提交回复
热议问题