1

I am trying to get dominant colors from image with percentage or with count of each color. I tried this method from imagick php library.

 $imagick->quantizeImage(256, \Imagick::COLORSPACE_YIQ, 0, false, false);
        $imagick->uniqueImageColors();
        $imagick->scaleimage($imagick->getImageWidth(), $imagick->getImageHeight() * 20);

        header("Content-Type: image/png");
        echo $imagick->getImageBlob();

It is returning the image blob but i want color value in hex or rgba with count/perncentage.

bestprogrammerintheworld
  • 5,417
  • 7
  • 43
  • 72
mobeen
  • 158
  • 1
  • 10
  • $imagick->getImageBlob(); of course returns the image sequence as a blob string. – Ken Lee Jul 05 '21 at 06:29
  • @KenLee is it possible to parse this data in the form of array or json ? – mobeen Jul 05 '21 at 06:36
  • If I remember correctly, the number of possible color is 16,777,216 in the RGB color space. So you have to prepare to have a huge array to store your data (different colors and the count of each color in your photo). – Ken Lee Jul 05 '21 at 06:44
  • is there any other method from imagick library from which i can get the colors with count ? – mobeen Jul 05 '21 at 06:46
  • You may refer to this [link](https://stackoverflow.com/questions/10290259/detect-main-colors-in-an-image-with-php) – Ken Lee Jul 05 '21 at 06:56
  • https://www.php.net/manual/en/imagick.getimagecolors.php ? – bestprogrammerintheworld Jul 05 '21 at 07:40
  • It will return only number of colors, how to get colors with hex values ? @bestprogrammerintheworld – mobeen Jul 05 '21 at 08:50

1 Answers1

1

You can get the color count of each pixel by this script, hope this helps

$file_to_grab_with_location = "test.bmp";

$imagick_type = new Imagick();

// Open File
// ---------------------------------------------

$file_handle_for_viewing_image_file = fopen($file_to_grab_with_location, 'a+');
$imagick_type->readImageFile($file_handle_for_viewing_image_file);

// Preset Information
// ---------------------------------------------

$frequency_list_of_values = array();

// Parse Pixels
// ---------------------------------------------

$image_resolution_width = $imagick_type->getImageWidth();
$image_resolution_height = $imagick_type->getImageHeight();

print("Image Resolution:  Width - $image_resolution_width / Height - $image_resolution_height<br><br>");

// Parse Image Top-to-Bottom (Y-Variable)
// ---------------------------------------------

for($y = 0; $y < $image_resolution_height; $y++)
{
    // Parse Image Left-to-Right (X-Variable)
    // ---------------------------------------------

    for($x = 0; $x < $image_resolution_width; $x++)
    {

        // Image Pixel Color
        // ---------------------------------------------

        $pixel_to_examine = $imagick_type->getImagePixelColor($x,$y);

        $pixel_to_examine_color_value_red = $pixel_to_examine->getColorValue(imagick::COLOR_RED);
        $pixel_to_examine_color_value_green = $pixel_to_examine->getColorValue(imagick::COLOR_GREEN);
        $pixel_to_examine_color_value_blue = $pixel_to_examine->getColorValue(imagick::COLOR_BLUE);

        // Set Key Value
        // ---------------------------------------------

        $key_value =    $pixel_to_examine_color_value_red    .    " / "    .
            $pixel_to_examine_color_value_green    .    " / "    .
            $pixel_to_examine_color_value_blue            ;

        // Increment Array Entry for Color
        // ---------------------------------------------

        if(isset($frequency_list_of_values[$key_value]) == TRUE)
        {
            $temp = $frequency_list_of_values[$key_value];
            $temp++;
            $frequency_list_of_values[$key_value] = $temp;
        }
        else
        {
            $frequency_list_of_values[$key_value] = 1;
        }
    }
}
mobeen
  • 158
  • 1
  • 10