PHP Conference Japan 2024

Imagick::getImageColors

(PECL imagick 2, PECL imagick 3)

Imagick::getImageColors取得圖片中不重複顏色的數量

說明

public Imagick::getImageColors(): int

取得圖片中不重複顏色的數量。

參數

此函式沒有任何參數。

回傳值

回傳一個 int,代表圖片中不重複顏色的數量。

新增註解

使用者貢獻的註解 2 則註解

3
holdoffhunger at gmail dot com
12 年前
我發現這個函式很難回傳不重複顏色的數量。此外,我還希望能夠擁有一個陣列,其中每個 $key 值都是像素的 RGB,而每個 $value 值都是該像素出現的次數。基本上,是一個頻率列表。例如,紅色的 $key 值會是 "1 / 0 / 0",而該像素顏色在影像中出現的次數則會是 "25"。因此,我寫了一些程式碼來做到這一點,組合了 readImageFile、getImageWidth、getImageHeight、getImagePixelColor 和一個簡單的 x/y 解析器,如下所示:

<?php

// 測試檔案
// ---------------------------------------------

// 注意:這個檔案只是一張 5x5 紅色方塊的圖片
// 位於一個巨大的 521x512 黑色方塊內,然後
// 我做了一堆隨機的黃色鋸齒。

$file_to_grab_with_location = "test.bmp";

$imagick_type = new Imagick();

// 開啟檔案
// ---------------------------------------------

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

// 預設資訊
// ---------------------------------------------

$frequency_list_of_values = array();

// 解析像素
// ---------------------------------------------

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

print(
"影像解析度:寬度 - $image_resolution_width / 高度 - $image_resolution_height<br><br>");

// 由上至下解析影像 (Y 變數)
// ---------------------------------------------

for($y = 0; $y < $image_resolution_height; $y++)
{
// 由左至右解析影像 (X 變數)
// ---------------------------------------------

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

// 影像像素顏色
// ---------------------------------------------

$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);

// 設定索引值
// ---------------------------------------------

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

// 遞增顏色的陣列項目
// ---------------------------------------------

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("<pre>");
print_r($frequency_list_of_values);
print(
"</pre>");

/*

結果:
------------------

影像解析度:寬度 - 521 / 高度 - 512

Array
(
[1 / 0 / 0] => 25
[0 / 0 / 1] => 264107
[1 / 1 / 0] => 2620
)

*/

?>
-4
amaral dot regis at hotmail dot com
6 年前
O exemplo anterior apresentou números fracionados nas chaves da variável $frequency_list_of_values

Array
(
[1 / 0 / 1] => 157295
[0.83529411764706 / 0.87843137254902 / 0.82352941176471] => 1
[0.80392156862745 / 0.84705882352941 / 0.7843137254902] => 2
[0.8156862745098 / 0.84705882352941 / 0.78823529411765] => 1
...

Então eu fiz uma alteração que melhora o resultado para quem esta buscando saber quais as cores dominantes na imagem, mostrando a chave com o valor rgb exato
<?php

$file_to_grab_with_location
= $imagepath; //圖片檔案的相對路徑

$imagick_type = new \Imagick();

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

$frequency_list_of_values = array();

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

print(
"圖片解析度:寬度 - $image_resolution_width / 高度 - $image_resolution_height<br><br>");

for(
$y = 0; $y < $image_resolution_height; $y++)
{

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

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

if(isset(
$frequency_list_of_values[$pixel_to_examine->getColorAsString()]) == TRUE)
{
$temp = $frequency_list_of_values[$pixel_to_examine->getColorAsString()];
$temp++;
$frequency_list_of_values[$pixel_to_examine->getColorAsString()] = $temp;
}
else
{
$frequency_list_of_values[$pixel_to_examine->getColorAsString()] = 1;
}
}

arsort($frequency_list_of_values);
}

print(
"<pre>");
print_r($frequency_list_of_values);
print(
"</pre>");

?>

結果
圖片解析度:寬度 - 1965 / 高度 - 998

Array
(
[srgba(255,0,255,0)] => 451654
[srgb(54,52,53)] => 281069
[srgb(177,51,54)] => 130449
[srgb(236,50,55)] => 64001
...

對於非常大的圖片,此方法會很耗時,因為它會逐個像素進行分析。
To Top