一,平均顏色的用途:
很多app在流式的展示圖片時,
在圖片沒加載出來之前,
不是使用統一的背景圖,
而是先顯示一個純色的背景,
而背景色和圖片的顏色非常接近,
這樣給用戶的體驗會更好。
我們這里演示的就是獲取這個平均顏色值
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,查看identify的版本和幫助
1,查看版本
[root@blog im5]# identify -version Version: ImageMagick 6.9.10-86 Q16 x86_64 2020-01-13 https://imagemagick.org Copyright: © 1999-2020 ImageMagick Studio LLC License: https://imagemagick.org/script/license.php Features: Cipher DPC Modules OpenMP(4.5) Delegates (built-in): bzlib cairo fftw fontconfig freetype gslib gvc jbig jng jp2 jpeg lcms ltdl lzma openexr pangocairo png ps raqm raw rsvg tiff webp wmf x xml zlib
2,查看幫助
[root@blog im5]# identify -help
3,查看手冊
[root@blog im5]# man identify
三,identify得到平均顏色值的例子:
使用identify得到圖片的平均顏色值
[root@blog im5]# identify -verbose 5.jpg Image: 5.jpg ... Channel statistics: Pixels: 465018 Red: min: 0 (0) max: 255 (1) mean: 173.125 (0.678923) standard deviation: 95.3528 (0.373932) kurtosis: -0.995196 skewness: -0.895732 entropy: 0.858608 Green: min: 0 (0) max: 255 (1) mean: 98.2824 (0.385421) standard deviation: 56.5396 (0.221724) kurtosis: 0.427876 skewness: 0.923531 entropy: 0.947875 Blue: min: 0 (0) max: 255 (1) mean: 124.452 (0.488046) standard deviation: 57.3031 (0.224718) kurtosis: -0.463398 skewness: 0.318713 entropy: 0.967108 Image statistics: Overall: min: 0 (0) max: 255 (1) mean: 131.953 (0.517463) standard deviation: 69.7318 (0.273458) kurtosis: -1.27908 skewness: 0.143493 entropy: 0.92453 Rendering intent: Perceptual ... Version: ImageMagick 6.9.10-86 Q16 x86_64 2020-01-13 https://imagemagick.org
說明:Channel statistics: 下面的
Red:的mean值
Green:的mean值
Blue:的mean值
就是我們需要的三個值
我們用shell把這個三個值取出來:
[root@blog im5]$ identify -verbose 5.jpg | grep mean | head -3 | awk '{match($0," ");print $2}' | awk '{printf("%.0f\n",$1)}' 173 98 124
三個值分別是r/g/b 三種顏色
也可以橫向打印出來
[root@blog im5]$ identify -verbose 5.jpg | grep mean | head -3 | awk '{match($0," ");print $2}' | awk '{printf("%.0f,",$1)}' | head -c-1 173,98,124
四,identify的其他例子:
得到圖片的寬高
[root@blog im5]$ identify -format %wx%h 5.jpg 799x582
-format可打印的變量:
%b file size %c comment %d directory %e filename extension %f filename %h height %i input filename %k number of unique colors %l label %m magick %n number of scenes %o output filename %p page number %q quantum depth %s scene number %t top of filename %u unique temporary filename %w width %x x resolution %y y resolution %# signature \n newline
附:format的官方文檔地址:
https://www.imagemagick.org/script/escape.php
五,使用縮放圖片的方法得到圖片的平均顏色值
#-resize: 調整文件大小,這里是把文件轉為1個像素大小
#!:表示不管原圖片比例,強制縮放后的圖片大小是1×1
#format:指定輸出的信息,這里的r/g/b是三原色的值
#info -: Specify 'file' as '-' for standard input or output.
#不指定文件而是使用-,用來指定標准輸入或標准輸出
[root@blog im5]$ convert 5.jpg -resize 1x1! -format "%[fx:int(255*r+.5)],%[fx:int(255*g+.5)],%[fx:int(255*b+.5)]" info:-
173,98,125
這個計算和identify得到的結果基本一致
六,取平均顏色值的效果展示
1,原圖:
得到平均顏色值:
[root@blog im5]$ identify -verbose 5.jpg | grep mean | head -3 | awk '{match($0," ");print $2}' | awk '{printf("%.0f,",$1)}' | head -c-1 173,98,124
效果:
2,原圖
得到平均顏色值:
[root@blog im4]$ identify -verbose blue.jpg | grep mean | head -3 | awk '{match($0," ");print $2}' | awk '{printf("%.0f,",$1)}' | head -c-1 55,128,225
效果:
3,原圖:
得到平均色值:
[root@blog im5]$ identify -verbose 4.jpg | grep mean | head -3 | awk '{match($0," ");print $2}' | awk '{printf("%.0f,",$1)}' | head -c-1 26,75,22
效果: