imagemagick - Percentage of pixels that have changed in an image in PHP -
currently have single image retrieving background color. making background color transparent, attempting compare original image determine percentage of image comprised of said background color. here code attempting use:
$image = new imagick($file); // make bg transparent comparison $tpimage = $image->clone(); $tpimage->setformat('png'); $tpimage->setimagepage(0, 0, 0, 0); $swatch_pixel = $tpimage->getimagepixelcolor(1, 1); $tpimage->painttransparentimage($swatch_pixel, 0, 65535 * 0.1); $image->setoption('fuzz', '2%'); $result = $image->compareimages($tpimage, imagick::metric_absoluteerrormetric); echo $result[0];
currently returning 0... i'm not sure if need use different comparison method or what, or if doing else wrong, i've spent quite time on , of resources i've found pretty old.
updated answer
actually, point out explicitly, absolute_error metric not work when difference transparency.
to answer question, want separate out alpha channel using:
separateimagechannel(imagick::channel_alpha);
and statistical mean tell percentage of pixels white - i.e. transparent.
i'll leave original answer below, because useful technique work other image differencing isn't purely transparency.
original answer
you can image properties after comparison , find element called "distortion" in there. so, if start image:
and roll right 10 pixels:
<?php $image1 = new imagick("image.png"); $image2 = new imagick("image.png"); $result = $image1->compareimages($image2,imagick::metric_meanabsoluteerror); $p1=$image1->getimageproperties(); print_r($p1); $image1->rollimage(10,0); $result = $image1->compareimages($image2,imagick::metric_meanabsoluteerror); $p1=$image1->getimageproperties(); print_r($p1); ?>
output
array ( [date:create] => 2016-06-02t14:15:01+01:00 [date:modify] => 2016-06-02t14:15:01+01:00 [distortion] => 0 <--- here's little devil [png:bkgd] => chunk found (see background color, above) [png:chrm] => chunk found (see chromaticity, above) [png:gama] => gamma=0.45454544 (see gamma, above) [png:ihdr.bit-depth-orig] => 4 [png:ihdr.bit_depth] => 4 [png:ihdr.color-type-orig] => 3 [png:ihdr.color_type] => 3 (indexed) [png:ihdr.interlace_method] => 0 (not interlaced) [png:ihdr.width,height] => 200, 200 [png:plte.number_colors] => 5 [png:srgb] => intent=0 (perceptual intent) [png:text] => 2 text/ztxt/itxt chunks found [png:time] => 2016-06-02t14:15:01z ) array ( [date:create] => 2016-06-02t14:15:01+01:00 [date:modify] => 2016-06-02t14:15:01+01:00 [distortion] => 0.0833333 <--- here's little devil [png:bkgd] => chunk found (see background color, above) [png:chrm] => chunk found (see chromaticity, above) [png:gama] => gamma=0.45454544 (see gamma, above) [png:ihdr.bit-depth-orig] => 4 [png:ihdr.bit_depth] => 4 [png:ihdr.color-type-orig] => 3 [png:ihdr.color_type] => 3 (indexed) [png:ihdr.interlace_method] => 0 (not interlaced) [png:ihdr.width,height] => 200, 200 [png:plte.number_colors] => 5 [png:srgb] => intent=0 (perceptual intent) [png:text] => 2 text/ztxt/itxt chunks found [png:time] => 2016-06-02t14:15:01z )