phpregexgrepzshgraphicsmagick

How to get image red green blue standard deviation values from gm identify -verbose?


I am trying to get the values given for Standard Deviation in the Red, Green, and Blue channels that you can see in gm identity -verbose but they're not listed in the -format options.

How can I get these from the commandline?

A workaround to list the values using PHP:

    $raw = `gm identify -verbose {$file}|grep -E -e '^ +(Standard Deviation): *[^b]*$'`;
    preg_match_all("/(?<=Standard Deviation:)(?: +)([\d\.]+)/", $raw, $matches);
    $ret = [$matches[1][0], $matches[1][1], $matches[1][2]];

this is the output from gm identify -verbose. Example we need the values from properties under Channel Statistics:

Image: /.../.../img.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Geometry: 1064x1600
  Class: DirectClass
  Type: true color
  Depth: 8 bits-per-pixel component
  Channel Depths:
    Red:      8 bits
    Green:    8 bits
    Blue:     8 bits
  Channel Statistics:
    Red:
      Minimum:                     0.00 (0.0000)
      Maximum:                   255.00 (1.0000)
      Mean:                      132.32 (0.5189)
      Standard Deviation:         45.92 (0.1801)
    Green:
      Minimum:                     0.00 (0.0000)
      Maximum:                   255.00 (1.0000)
      Mean:                      104.17 (0.4085)
      Standard Deviation:         55.13 (0.2162)
    Blue:
      Minimum:                     0.00 (0.0000)
      Maximum:                   255.00 (1.0000)
      Mean:                      103.61 (0.4063)
      Standard Deviation:         55.71 (0.2185)
  Filesize: 452.0Ki
  Interlace: No
  Orientation: Unknown
  Background Color: white
  Border Color: #DFDFDF
  Matte Color: #BDBDBD
  Page geometry: 1064x1600+0+0
  Compose: Over
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  JPEG-Quality: 95
  JPEG-Colorspace: 2
  JPEG-Colorspace-Name: RGB
  JPEG-Sampling-factors: 2x2,1x1,1x1
  Signature: 136912e901ae9314fd683868418cae1f5d838c6891ddd8c13ce28057fb39365a
  Tainted: False
  User Time: 0.010u
  Elapsed Time: 0m:0.014459s
  Pixels Per Second: 112.3Mi

Solution

  • The solution that worked for me on macos was based on the answer from @thefourthbird. I couldn't install ggrep but had success with pcre2grep from port (macports).

    gm identify -verbose img.jpg|\
    pcre2grep -o --allow-lookaround-bsk "\b(?:Minimum|Maximum|Mean|Standard Deviation): +\d+\.\d+ \(\K\d+\.\d+"
    

    the --allow-lookaround-bsk option in pcre2grep enabled the \K symbol which was needed to discard the content before each value.

    The result is the extracted values I can use from the example in the question:

    0.0000
    1.0000
    0.5189
    0.1801
    0.0000
    1.0000
    0.4085
    0.2162
    0.0000
    1.0000
    0.4063
    0.2185
    

    Note: the number of values varies.