Scripting 在shell脚本中是否可以区分灰度和(扫描的)单色?
我有几千张图片,我想根据它们属于三个类别中的哪一个运行各种IM命令:Scripting 在shell脚本中是否可以区分灰度和(扫描的)单色?,scripting,imagemagick,Scripting,Imagemagick,我有几千张图片,我想根据它们属于三个类别中的哪一个运行各种IM命令: 颜色(通常是明亮的颜色) 灰度(从纸上扫描,“白色”通常带有淡黄色) 单色(扫描,带淡黄色,如上所述) 这可以从shell脚本中进行分类吗 颜色示例#1 灰度示例#1 单色示例#1和#2 首先:你问题的标题有误导性 “是否可以在shell脚本中区分灰度和(扫描的)单色?” 直接的识别告诉颜色空间和位深度 这是误导,因为您提供的所有示例图像实际上都位于8位sRGB颜色空间中: identify http://i.stac
- 颜色(通常是明亮的颜色)
- 灰度(从纸上扫描,“白色”通常带有淡黄色)
- 单色(扫描,带淡黄色,如上所述)
首先:你问题的标题有误导性 “是否可以在shell脚本中区分灰度和(扫描的)单色?” 直接的
识别
告诉颜色空间和位深度
这是误导,因为您提供的所有示例图像实际上都位于8位sRGB
颜色空间中:
identify http://i.stack.imgur.com/lygAE.png \
http://i.stack.imgur.com/H7vBP.png \
http://i.stack.imgur.com/ZOCTK.png
http://i.stack.imgur.com/lygAE.png=>lygAE.png PNG 236x216 236x216+0+0 8-bit sRGB 127KB 0.000u 0:00.000
http://i.stack.imgur.com/H7vBP.png=>H7vBP.png[1] PNG 259x192 259x192+0+0 8-bit sRGB 86.2KB 0.000u 0:00.000
http://i.stack.imgur.com/ZOCTK.png=>ZOCTK.png[2] PNG 264x179 264x179+0+0 8-bit sRGB 86.7KB 0.000u 0:00.000
如您所见,identify
命令(ImageMagick命令集的一部分)可以轻松地告诉您图像的深度和颜色空间
使用标识
-格式
参数告诉特定的图像属性
您可以将-format
参数包含在“转义百分比”中,以便仅获取图像的特定属性:
:用于图像文件名f
:用于映像的目录组件d
:用于图像深度z
:用于图像类别和颜色空间r
identify -format "%f %d : %z %r\n" \
http://i.stack.imgur.com/lygAE.png \
http://i.stack.imgur.com/H7vBP.png \
http://i.stack.imgur.com/ZOCTK.png
结果:
lygAE.png //i.stack.imgur.com : 8 DirectClass sRGB
H7vBP.png //i.stack.imgur.com : 8 DirectClass sRGB
ZOCTK.png //i.stack.imgur.com : 8 DirectClass sRGB
out.png:
-- number of unique colors: 8
-- depth: 8
-- class/space: PseudoClass sRGB
-- image type: Palette
将一幅图像转换为真正的单色
现在,为了向您展示真实的“单色”图像的外观,让我们相应地转换其中一个示例:
convert \
-colorspace gray \
http://i.stack.imgur.com/lygAE.png \
+dither \
-colors 2 \
-depth 1 \
bmp3:monochrome.bmp
及
以下是相应的图像:
说出独特颜色的数量
如果您(像您一样)在sRGB颜色空间中拥有8位深度的所有图像,那么从理论上讲,每个图像可以有多达16.777.216(1600万)种颜色(也称为“TrueColor”)。然而,大多数实际图像并没有使用该光谱的全部范围,而出现“灰色”的图像实际上使用的数量更少
因此ImageMagick还有两个“百分比转义”来返回有关图像的信息:
:返回图像中唯一颜色的数量。这是一个计算值。IM必须对图像进行处理,并分析图像中的每个像素,才能得出这个数字%k
identify -format "%f - number of unique colors: %k\n" \
http://i.stack.imgur.com/lygAE.png \
http://i.stack.imgur.com/H7vBP.png \
http://i.stack.imgur.com/ZOCTK.png
结果:
lygAE.png - number of unique colors: 47583
H7vBP.png - number of unique colors: 7987
ZOCTK.png - number of unique colors: 5208
正如你所看到的,你的带有明显颜色的图像使用的uniq颜色大约是“灰色”扫描的6倍
然而,情况未必如此。例如,请参见此图像:
它是彩色的,不是吗
我用这个命令生成了它:
convert -size 100x100 \
xc:red \
xc:green \
xc:blue \
xc:white \
xc:black \
xc:cyan \
xc:magenta \
xc:yellow \
+append \
out.png
你甚至可以通过简单的观察来计算独特颜色的数量:8
现在,标识
告诉了我们什么
identify \
-format "%f:\n \
-- number of unique colors: %k\n \
-- depth: %z\n \
-- class/space: %r\n \
-- image type: %[type]\n" \
out.png
结果:
lygAE.png //i.stack.imgur.com : 8 DirectClass sRGB
H7vBP.png //i.stack.imgur.com : 8 DirectClass sRGB
ZOCTK.png //i.stack.imgur.com : 8 DirectClass sRGB
out.png:
-- number of unique colors: 8
-- depth: 8
-- class/space: PseudoClass sRGB
-- image type: Palette
因此,少量的独特颜色并不一定证明图像是“灰色”的
你将不得不稍微利用这些参数,看看你是否能想出一个组合来帮助你正确地对现实世界中的“成千上万的图像”进行分类
也考虑图像统计
在identify-format%.的帮助下,您可以查看更多的值。。。filename.suffix
:
:图像gamma的值%[gamma]
:计算:图像熵%[熵]
:计算:图像的峰度值统计%[峰度]
:计算:图像的最大值统计%[max]
:计算:图像的平均值统计%[平均值]
:计算:图像的最小值统计%[min]
:icc配置文件信息%[profile:icc]
:icm配置文件信息%[profile:icm]
命令行工具exiftool是一个很好的工具。首先:你的问题标题有误导性 “是否可以在shell脚本中区分灰度和(扫描的)单色?” 直接的
识别
告诉颜色空间和位深度
这是误导,因为您提供的所有示例图像实际上都位于8位sRGB
颜色空间中:
identify http://i.stack.imgur.com/lygAE.png \
http://i.stack.imgur.com/H7vBP.png \
http://i.stack.imgur.com/ZOCTK.png
http://i.stack.imgur.com/lygAE.png=>lygAE.png PNG 236x216 236x216+0+0 8-bit sRGB 127KB 0.000u 0:00.000
http://i.stack.imgur.com/H7vBP.png=>H7vBP.png[1] PNG 259x192 259x192+0+0 8-bit sRGB 86.2KB 0.000u 0:00.000
http://i.stack.imgur.com/ZOCTK.png=>ZOCTK.png[2] PNG 264x179 264x179+0+0 8-bit sRGB 86.7KB 0.000u 0:00.000
如您所见,identify
命令(ImageMagick命令集的一部分)可以轻松地告诉您图像的深度和颜色空间
使用标识
-格式
参数告诉特定的图像属性
您可以将-format
参数包含在“转义百分比”中,以便仅获取图像的特定属性:
:用于图像文件名f
:用于映像的目录组件d
:用于图像深度z
:用于图像类别和颜色空间r
identify -format "%f %d : %z %r\n" \
http://i.stack.imgur.com/lygAE.png \
http://i.stack.imgur.com/H7vBP.png \
http://i.stack.imgur.com/ZOCTK.png
结果:
lygAE.png //i.stack.imgur.com : 8 DirectClass sRGB
H7vBP.png //i.stack.imgur.com : 8 DirectClass sRGB
ZOCTK.png //i.stack.imgur.com : 8 DirectClass sRGB
out.png:
-- number of unique colors: 8
-- depth: 8
-- class/space: PseudoClass sRGB
-- image type: Palette
将一幅图像转换为真正的单色
现在,为了向您展示真实的“单色”图像的外观,让我们相应地转换其中一个示例:
convert \
-colorspace gray \
http://i.stack.imgur.com/lygAE.png \
+dither \
-colors 2 \
-depth 1 \
bmp3:monochrome.bmp
及
以下是相应的图像:
说出独特颜色的数量
如果您(像您一样)在sRGB颜色空间中拥有8位深度的所有图像,那么从理论上讲,每个图像可以有多达16.777.216(1600万)种颜色(也称为“TrueColor”)。然而,大多数实际图像并没有使用该光谱的全部范围,出现“灰色”的图像实际上使用的是偶数
convert image.png \
-colorspace gray \
-contrast-stretch 1% \
-black-threshold 20% \
-white-threshold 80% -fill black -opaque white \
\( +clone -evaluate set 0 \) \
-metric ae -compare -format "%[distortion]" info:
#!/bin/bash
for i in colour.png grey.png mono.png; do
SatMean=$(convert $i -colorspace HSL -channel S -separate -format "%[mean]" info:)
HueStdDev=$(convert $i -colorspace HSL -channel H -separate -format "%[standard-deviation]" info:)
NumMidTones=$(convert $i -colorspace gray -contrast-stretch 1% -black-threshold 20% -white-threshold 80% -fill black -opaque white \( +clone -evaluate set 0 \) -metric ae -compare -format "%[distortion]" info:)
NumPixels=$(convert $i -ping -format "%[fx:w*h]" info:)
PctMidTones=$((NumMidTones*100/NumPixels))
echo $i: Mean saturation: $SatMean, Hue Std-Dev: $HueStdDev, PercentMidTones: $PctMidTones
done
colour.png: Mean saturation: 17807.9, Hue Std-Dev: 16308.3, PercentMidTones: 70
grey.png: Mean saturation: 7019.67, Hue Std-Dev: 2649.01, PercentMidTones: 39
mono.png: Mean saturation: 14606.1, Hue Std-Dev: 1097.36, PercentMidTones: 27