Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick Transparent PNG Background

I am trying to take this image, enter image description here which I converted to a .png file and then want to remove the white background to make it transparent.

I have tried running the following but haven't had any luck.

    $strInputFile = 'car.png';
    $execScript = '/usr/local/bin/convert '.$strInputFile.' ';
    $execScript .= "-transparent white car-new.png"; 
    $output = shell_exec($execScript);

Am I doing something wrong? The new file gets created, but still has the white background.

like image 474
Jako Avatar asked Oct 10 '11 21:10

Jako


3 Answers

Similar problem with php imagick:

When converting SVG to transparent PNG, dont forget to put this BEFORE $imagick->readImageBlob():

$imagick->setBackgroundColor(new ImagickPixel('transparent'));
like image 184
psycho brm Avatar answered Nov 17 '22 13:11

psycho brm


As the image is a JPEG, which is subject to losses and slight colour inaccuracies, the whites in your image are not actually 100% pure white. ImageMagick has a way of dealing with this through the -fuzz setting which allows a degree of fuzziness, or a range of allowable error, when specifying colours.

The correct incantation here is:

convert car.jpg -fuzz 10% -transparent white car.png
like image 29
Mark Setchell Avatar answered Nov 17 '22 14:11

Mark Setchell


Try the following code:

$strInputFile = 'car.png';
$target = 'car_transparent.png';
$im = new Imagick($strInputFile);
$im->paintTransparentImage($im->getImageBackgroundColor(), 0, 10000);
$im->setImageFormat('png');
$im->writeImage($target);
$im->destroy();
like image 2
J Ajay Avatar answered Nov 17 '22 14:11

J Ajay