I'm attempting to try and assign a value to an image based on its 'saturation level', to see if the image is black and white or color. I'm using Imagick, and have found what seems to be the perfect code for the command line and am trying to replicate it using the PHP library.
I think I understand the concept:
- Convert image to HSL.
- Extract the 'g' channel (which is the S channel in HSL).
- Calculate the mean of this channel.
Command line code
convert '$image_path' -colorspace HSL -channel g -separate +channel -format '%[fx:mean]' info:
My PHP code
$imagick = new Imagick($image_path);
$imagick->setColorspace(imagick::COLORSPACE_HSL);
print_r($imagick->getImageChannelMean(imagick::CHANNEL_GREEN));
Output
My PHP code isn't outputting the same sort of values as the command line code, though. For example, a grayscale image gives 0
for the command line code, but the PHP code gives [mean] => 10845.392051182 [standardDeviation] => 7367.5888849872
.
Similarly, another grayscale image gives 0
vs. [mean] => 31380.528443457 [standardDeviation] => 19703.501101904
.
A colorful image gives 0.565309
vs. [mean] => 33991.552881892 [standardDeviation] => 16254.018540044
.
There just doesn't seem to be any kind of pattern between the different values. Am I doing something obviously wrong?
Thanks.
Just to add, I've also tried this PHP code
$imagick = new Imagick($image_path);
$imagick->setColorspace(imagick::COLORSPACE_HSL);
$imagick->separateImageChannel(imagick::CHANNEL_GREEN);
$imagick->setFormat('%[fx:mean]');
But I get an Unable to set format
error when I try and set the format. I've also tried setFormat('%[fx:mean] info:')
, setFormat('%[mean]')
, setFormat('%mean')
, etc.
Update — FIXED!
Thanks to @danack for figuring out I needed to use transformImageColorspace()
and not setColorspace()
. The working code is below.
$imagick = new Imagick($image_path);
$imagick->transformImageColorspace(imagick::COLORSPACE_HSL);
$saturation_channel = $imagick->getImageChannelMean(imagick::CHANNEL_GREEN);
$saturation_level = $saturation_channel['mean']/65535;