Detect “overall average” color of the picture

前端 未结 7 960
走了就别回头了
走了就别回头了 2020-12-04 05:53

I have a jpg image.

I need to know \"overall average\" the color of the image. At first glance there can use the histogram of the image (channel RGB).

At wor

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 05:55

    You can use PHP to get an array of the color palette like so:

    \n"; 
    foreach($palette as $color) 
    { 
       echo " #$color\n"; 
    } 
    echo "\n";
    

    Which gives you an array whose values are higher for how often that color has been used.

    EDIT A commenter asked how to use this on all files in a directory, here it is:

        if ($handle = opendir('./path/to/images')) {
    
            while (false !== ($file = readdir($handle))) {
               $palette = colorPalette($file, 10, 4);
               echo "\n"; 
               foreach($palette as $color) { 
                   echo "\n"; 
               } 
               echo "
     #$color
    \n"; } closedir($handle); }

    might not want to do this on too many files, but it's your server.

    Alternatively if you'd rather use Javascript Lokesh's Color-Theif library does exactly what you're looking for.

提交回复
热议问题