如何通过gm identify -verbose获取RGB通道的标准差数值
提取gm identify输出中RGB通道的标准差
我需要从gm identify -verbose的输出中获取Red、Green、Blue通道对应的Standard Deviation(标准差)数值,但该值无法通过-format选项直接指定,请问如何通过命令行实现?
纯命令行解决方案
可以通过awk结合gm identify过滤提取目标数值,命令如下:
gm identify -verbose 你的图片路径.jpg | awk '/Channel Statistics:/ {flag=1; next} /^ [A-Z]/ {if (flag) {channel=$1; flag=0}} /Standard Deviation:/ {print channel ": " $3}'
执行后会输出类似结果:
Red: 45.92 Green: 55.13 Blue: 55.71
PHP实现方案
如果需要在PHP中处理,可以通过执行shell命令结合正则提取数值:
$file = '你的图片路径.jpg'; // 过滤出包含Standard Deviation的行 $raw = `gm identify -verbose {$file} | grep -E '^ +(Standard Deviation): *[^b]*$'`; // 正则提取数值部分 preg_match_all("/(?<=Standard Deviation:)(?: +)([\d\.]+)/", $raw, $matches); // 按Red、Green、Blue顺序整理结果 $stdDeviations = [ 'Red' => $matches[1][0], 'Green' => $matches[1][1], 'Blue' => $matches[1][2] ]; // 输出结果 print_r($stdDeviations);
参考输出示例
gm identify -verbose的典型输出片段(我们需要提取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
内容的提问来源于stack exchange,提问作者yarns
相关产品推荐
相关产品推荐

