Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert non-transparent pixels to black

I'm looking for a way of converting an image so that all non-transparent pixels (those that have alpha != 1) into black and transparent pixels untouched (or converted to white). The closest I got was with the below imagemagick command:

convert <img> -colorspace Gray <out>

However this still gives me some gray colors instead of a complete black. I have tried all colorspace options and none does the job.

Any idea how I could achieve this with imagemagick or with similar tools (or with a PHP library if it exists)

like image 903
Max Avatar asked Aug 06 '10 17:08

Max


People also ask

How do you make transparent pixels solid?

In the Layers panel, Ctrl+click (or, if on mac Cmd+click ) the layer thumbnail to turn its transparency into a selection. Use Select > Edit in Quick Mask Mode to enter Quick Mask Mode. Use Image > Adjustments > Threshold and set the Threshold to 255, to remove all transparent pixels.

Can pixels be transparent?

Transparent pixels This image has binary transparency (some pixels fully transparent, other pixels fully opaque). It can be transparent against any background because it is monochrome.


2 Answers

I know this question is old, but now I've stumbled on it I might as well answer it.

The ImageMagick command you want is:

convert <img> -alpha extract -threshold 0 -negate -transparent white <out>

I'll breakdown what it's doing as well.

  1. -alpha extract - Take the alpha mask of the image. Completely transparent pixels will be black, completely opaque ones white.
  2. -threshold 0 - Increase all channels to their maximum value if they are greater than zero. In this case, it will make every pixel white except the ones that are completely black.
  3. -negate - Invert the image. Now our blacks are white and our whites are black.
  4. -transparent white - Set white pixels to be transparent. This can be excluded if you'd prefer your originally transparent pixels to be white.

Before

PNG image with alpha channel

After

Previous image after running the convert command

like image 93
Alex Barrett Avatar answered Sep 20 '22 02:09

Alex Barrett


Well, you could do it with GD and a pair of loops:

$img = imagecreatefromstring(file_get_contents($imgFile));
$width = imagesx($img);
$hieght = imagesy($img);

$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);

for ($x = 0; $x < $width; $x++) {
    for ($y = 0; $y < $width; $y++) {
        $color = imagecolorat($img, $x, $y);
        $color = imagecolorforindex($color);
        if ($color['alpha'] == 1) {
            imagesetpixel($img, $x, $y, $black);
        } else {
            imagesetpixel($img, $x, $y, $white);
        }
    }
}

Or, you could replace colors (this may or may not work):

$img = imagecreatefromstring(file_get_contents($imgFile));
$maxcolors = imagecolorstotal($img);
for ($i = 1; $i <= $maxcolors; $i++) {
    $color = imagecolorforindex($i);
    if ($color['alpha'] == 1) {
        imagecolorset($img, $i, 0, 0, 0);
    } else {
        imagecolorset($img, $i, 255, 255, 255);
    }
}
like image 40
ircmaxell Avatar answered Sep 21 '22 02:09

ircmaxell