12

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.

Jako
  • 4,731
  • 7
  • 39
  • 54

7 Answers7

29

Similar problem with php imagick:

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

$imagick->setBackgroundColor(new ImagickPixel('transparent'));
psycho brm
  • 7,494
  • 1
  • 43
  • 42
  • I had same problem and the above suggestion helped me get a little further. However now the png file generated have black background. :( – codingbbq Dec 31 '12 at 04:53
  • 1
    So after a lot of trial and error, i realized that i was using $im->adaptiveSharpenImage(2,1); in my php code after setbackgroundColor method and this appeared to create a non transparent background. After removing that method, i could get png transparent backgrounds. Now to figure out how to get sparp, crisp and complete png images from svg. :) Thanks again for the answer. – codingbbq Dec 31 '12 at 07:10
  • 3
    `dont forget to put this BEFORE ` MUST BE GIVEN IN BOLD. saved several hours !! – JayKandari Feb 25 '16 at 15:10
  • In extra bold, or even heavy or black, with an extended display typeface! – philipp Mar 13 '18 at 13:50
4

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
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 1
    With a few attempts I got a perfect result with: `convert source.png -fuzz 4% -transparent white result.png`. `10%` was eating too many pixels. – dan Jun 11 '17 at 14:01
2

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();
Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
J Ajay
  • 319
  • 3
  • 3
0

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!

Chip Thrasher
  • 332
  • 2
  • 4
  • 15
0

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);
Julesezaar
  • 2,658
  • 1
  • 21
  • 21
0

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

Amber
  • 507,862
  • 82
  • 626
  • 550
  • I tried using -transparency white and the new file doesn't even get created now. – Jako Oct 10 '11 at 22:08
  • Have you tried running the command by hand on the system? Or failing that, echoed the value of `$output` so you can see if there's an error message? – Amber Oct 10 '11 at 22:12
  • echoing $output gives me this `/usr/local/bin/convert /var/www/html/test/car.png -transparency white /var/www/html/test/car-new.png` – Jako Oct 10 '11 at 22:22
  • That looks more like you echo'd `$execScript`, not `$output`. – Amber Oct 10 '11 at 22:24
  • When I tried, `$output = shell_exec($execScript); echo $output;` nothing happened. The page was blank. – Jako Oct 10 '11 at 22:50
0

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

$execScript .= "-transparent #FEFEFE car-new.png"; 
moropus
  • 3,672
  • 1
  • 22
  • 30