Kusum Gupta Kusum Gupta - 1 year ago 122
PHP Question

How to replace background pixel with white pixel using php imagemagick draw method

I am using php imagick extension.But i don't understand how to change background color of an image using draw method.

enter image description here

Answer Source

The polyvore tutorial that you are following said

Next, we replace the background with white, starting at the pixel that is at the xy position of 2,2. We start there since it's almost certainly part of the background and not the product. ImageMagick has a Draw command that replaces pixels that are the same color as the start pixel with a new color. We use Draw to replace background pixels (those the same color as our start pixel) with white pixels. We also use Draw with a fuzz factor so that it also replaces pixels that are similar in color to the starting pixel to account for subtle gradients and variations in the background.

The image you presented already has a white background, so apparently you succeeded with this step. All that remains is to convert the white pixels to transparent. From the commandline, it's

magick UQ1U4.png -fill none -fuzz 5% -draw 'color 2,2 floodfill' out.png

This makes any pixel that is the same color as the (2,2) pixel (near the upper left corner pixel) transparent (color "none"). Floodfill recolors any pixel that matches the color of the target pixel and is a neighbor to the target pixel or to another pixel that is being recolored.

You can do the same operation through the PHP IMagick interface:

bool Imagick::floodFillPaintImage ( mixed $fill , float $fuzz , mixed $target , int $x , int $y , bool $invert [, int $channel =magick::CHANNEL_DEFAULT ] )' 

using the same parameters as in the commandline.

If you have an older version of ImageMagick, use convert instead of magick. If you are on Windows, use %% instead of a single %.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download