note 108203 added to imagick.getimagecolors

From: Date: Fri, 06 Apr 2012 22:55:01 +0000
Subject: note 108203 added to imagick.getimagecolors
Groups: php.notes 
Request: Send a blank email to php-notes+get-187004@lists.php.net to get a copy of this message
I've had difficulty getting this function to return the number of unique colors. Also, I wanted to be able to have an array with each $key value being the RGB of the pixel, and each $value value being the number of times that pixel occurs. Basically, a frequency list. For example, you would have "1 / 0 / 0" for red as a $key value, and "25" for the number of times that pixel color was in the image. So, I wrote some code to do that, using a combination of readImageFile, getImageWidth, getImageHeight, getImagePixelColor, and a simple x/y parser, like so : <?php // Test File // --------------------------------------------- // Note: This file was just a picture of a 5x5 red square // inside of a gigantic 521x512 black square, and // then I did a bunch of random, yellow zigzags. $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; } } } // Print Out Array of Values // --------------------------------------------- print("<pre>"); print_r($frequency_list_of_values); print("</pre>"); /* Results: ------------------ Image Resolution: Width - 521 / Height - 512 Array ( [1 / 0 / 0] => 25 [0 / 0 / 1] => 264107 [1 / 1 / 0] => 2620 ) */ ?> ---- Server IP: 69.147.83.197 Probable Submitter: 68.9.137.178 ---- Manual Page -- http://www.php.net/manual/en/imagick.getimagecolors.php Edit -- https://master.php.net/note/edit/108203 Del: integrated -- https://master.php.net/note/delete/108203/integrated Del: useless -- https://master.php.net/note/delete/108203/useless Del: bad code -- https://master.php.net/note/delete/108203/bad+code Del: spam -- https://master.php.net/note/delete/108203/spam Del: non-english -- https://master.php.net/note/delete/108203/non-english Del: in docs -- https://master.php.net/note/delete/108203/in+docs Del: other reasons-- https://master.php.net/note/delete/108203 Reject -- https://master.php.net/note/reject/108203 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#187004) next »