Jako
Jako

Reputation: 4901

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.

Upvotes: 12

Views: 24110

Answers (7)

Julesezaar
Julesezaar

Reputation: 3396

The order of doing things is important. First set background, then load data

$svg = new Imagick();

// Important! First set Background color transparent, then read svg
$svg->setBackgroundColor('transparent');
$svg->readImage($svgPath);

Upvotes: 0

Mark Setchell
Mark Setchell

Reputation: 207465

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

Upvotes: 4

Chip Thrasher
Chip Thrasher

Reputation: 422

If you're not making a Shell script, then try this:

$ convert -transparency white car.png car-new.png

This should work in a generic terminal. If it doesn't, let me know. Hope it helps!

Upvotes: 0

J Ajay
J Ajay

Reputation: 329

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

Upvotes: 2

psycho brm
psycho brm

Reputation: 7664

Similar problem with php imagick:

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

$imagick->setBackgroundColor(new ImagickPixel('transparent'));

Upvotes: 31

moropus
moropus

Reputation: 3782

The background of your image is not white (I mean 0xFFFFFF), it's 0xFEFEFE. Try:

$execScript .= "-transparent #FEFEFE car-new.png"; 

Upvotes: 0

Amber
Amber

Reputation: 526603

Did you mean to use -transparency rather than -transparent?

Upvotes: 0

Related Questions