I’ve had a hard time finding out how to set a colour (black in my case) to be transparent in a GIF using ImageMagick. How can I do that to a set of existing GIF images?
I’ve tried this option but it doesn’t seem to do anything:
mogrify -transparent-color black -transparent black *.gif
I’ve read through this but it seems a little baffling in what actually makes a colour become transparent in a GIF: http://www.imagemagick.org/Usage/formats/#boolean_trans
For example:
Note that setting “-transparent-color” does NOT add any transparency
to a GIF image, nor does it convert the specified color to become
transparent. All the option does is specify what color should placed
in the color table for the color index that is used representing the
transparent colors in a GIF image.If you want to change a specific (exact) color to become transparent,
then use the “-transparent” Color Replacement Operator.
but then…
Use +transparent to invert the pixels matched. that is make all
non-matching colors transparent.The -opaque operator is exactly the same as -transparent but replaces
the matching color with the current -fill color setting, rather than
transparent. However the -transparent operator also ensures that the
image has an alpha channel enabled, as per “-alpha set”, and does not
require you to modify the -channel to enable alpha channel handling.Note that this does not define the color as being the ‘transparency
color’ used for color-mapped image formats, such as GIF. For that use
-transparent-color
I’d like to warn you from using
mogrify.mogrifydoes convert images by overwriting the original ones. If anything goes wrong, your images are gone.Rather use
convert. You can always delete your original images after you are sure the conversion is ok.To make black pixels transparent, use this:
Works perfectly for me.
My IM version: ImageMagick 6.7.8-0 2012-07-04 Q16. What’s yours?
If it still doesn’t work for you, the ‘black’ in your GIFs may not be black after all, but only very dark gray. ImageMagick also has tools to enumerate all colors in a GIF. The following 2 commands may help you in this case:
and
Use of color black can be identified by name ‘black’, by RGB-values ‘(0, 0, 0)’ as well as by hex value ‘#000000’. So pure blacks should appear in the output of the last command as something like:
Dark grays could for example be:
Once there is transparency enabled in your GIF, the last command should show a tuple of 4 values for each color, the last value representing the Alpha channel. Your formerly black would appear in the output as something like: