zoukankan      html  css  js  c++  java
  • ImageMagick:用identify得到图片的平均颜色(基本颜色/主色调)

    一,平均颜色的用途:

    很多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
    ",$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
         
       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

    效果:

      

  • 相关阅读:
    POSTGRESQL 批量权限 管理方法
    centos7安装rabbitmq
    centos7使用cron任务的相关命令(与centos6有区别)
    crontab定时执行shell脚本
    使用kong-dashboard
    Kong组件构成及使用
    Docker基本操作命令
    微服务写的最全的一篇文章
    centos7安装kong和kong-dashboard
    sql练习03
  • 原文地址:https://www.cnblogs.com/architectforest/p/12858424.html
Copyright © 2011-2022 走看看