Skip to content

Instantly share code, notes, and snippets.

@chsh
Last active May 22, 2018 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chsh/27eee3c61b017bb32b37eb1a6e8a26e6 to your computer and use it in GitHub Desktop.
Save chsh/27eee3c61b017bb32b37eb1a6e8a26e6 to your computer and use it in GitHub Desktop.
Detect num non gray colors using ImageMagick's convert command.
list = `convert 9784152097712_10.jpg -format %c histogram:info:`.split("\n")
rgbs = list.map { |line| $1 if line =~ /\s+#([\dA-F]+)\s+/ }
num_non_gray = rgbs.select { |rgb|
r = rgb[0,2]; g = rgb[2,2]; b = rgb[4,2]
r != g || g != b
}.count
num_non_gray > 0 # true: 正しい画像, false: (一色かどうかはわからないが)グレー画像
# output example: convert 9784152097712_10.jpg -format %c histogram:info:
==begin
1: (172,172,172) #ACACAC srgb(172,172,172)
1: (174,174,174) #AEAEAE srgb(174,174,174)
1: (175,175,175) #AFAFAF srgb(175,175,175)
4: (177,177,177) #B1B1B1 srgb(177,177,177)
7: (178,178,178) #B2B2B2 srgb(178,178,178)
11: (179,179,179) #B3B3B3 grey70
29: (180,180,180) #B4B4B4 srgb(180,180,180)
22: (181,181,181) #B5B5B5 grey71
25: (182,182,182) #B6B6B6 srgb(182,182,182)
13: (183,183,183) #B7B7B7 srgb(183,183,183)
8: (184,184,184) #B8B8B8 grey72
3: (185,185,185) #B9B9B9 srgb(185,185,185)
1: (186,186,186) #BABABA grey73
4: (187,187,187) #BBBBBB srgb(187,187,187)
1: (188,188,188) #BCBCBC srgb(188,188,188)
1: (189,189,189) #BDBDBD grey74
3: (191,191,191) #BFBFBF grey75
2: (194,194,194) #C2C2C2 grey76
2: (196,196,196) #C4C4C4 grey77
1: (197,197,197) #C5C5C5 srgb(197,197,197)
1: (198,198,198) #C6C6C6 srgb(198,198,198)
2: (200,200,200) #C8C8C8 srgb(200,200,200)
2: (201,201,201) #C9C9C9 grey79
2: (204,204,204) #CCCCCC grey80
1: (207,207,207) #CFCFCF grey81
1: (210,210,210) #D2D2D2 srgb(210,210,210)
1: (211,211,211) #D3D3D3 LightGray
==end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment