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)
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:
I’ll breakdown what it’s doing as well.
-alpha extract– Take the alpha mask of the image. Completely transparent pixels will be black, completely opaque ones white.-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.-negate– Invert the image. Now our blacks are white and our whites are black.-transparent white– Set white pixels to be transparent. This can be excluded if you’d prefer your originally transparent pixels to be white.Before
After