Reputation: 2711
I am trying to calculate what is the percentage of pixels that has transparency in a specific image. For that, I'm trying to detect if a pixel has transparency (using Imagick on PHP). I know that basically there's 'getPixel' and 'getColor' that returns an rgba array, but I'm getting various values for the 'a' for jpg and non-transparent pngs images as well... I have tried with and without normalization.
For example, for this image I get alpha values of 0, 1, 0.1, 0.9 etc. for various pixels .
This is the code I am using:
$imageIterator = $image->getPixelIterator();
foreach ($imageIterator as $row => $pixels) {
foreach ($pixels as $column => $pixel) {
$color = $pixel->getColor(true);
echo $color['a'] . PHP_EOL;
...
What am I doing wrong? Is there another way of getting this info?
Upvotes: 2
Views: 1529
Reputation: 207445
You can check your version with:
php -i | grep -i -A10 magick
Mine is this:
imagick module => enabled
imagick module version => 3.3.0
imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
Imagick compiled with ImageMagick version => ImageMagick 6.9.3-0 Q16 x86_64 2016-01-08 http://www.imagemagick.org
Imagick using ImageMagick library version => ImageMagick 6.9.3-0 Q16 x86_64 2016-01-08 http://www.imagemagick.org
ImageMagick copyright => Copyright (C) 1999-2016 ImageMagick Studio LLC
ImageMagick release date => 2016-01-08
Running this code, I get all ones for your image - i.e. no alpha layer or a fully opaque image:
#!/usr/local/bin/php -f
<?php
$image = new Imagick("budgie.png");
$pixel_iterator = $image->getPixelIterator();
foreach($pixel_iterator as $y => $pixels)
{
foreach($pixels as $x => $pixel)
{
$color = $pixel->getColor(true);
echo $color['a'] . PHP_EOL;
}
}
?>
And likewise, if I run identify
on your image, it shows no alpha layer present:
identify -verbose budgie.png | more
Image: budgie.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 296x383+0+0
Units: Undefined
Type: TrueColor
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
I think there is something up with your versions/configurations.
Upvotes: 2