Page 1 of 1

Convert GIFs to gray scale while preserving transparency

Posted: 2010-03-02T17:49:51-07:00
by SageRaven
I've got a ton of color GIFs, most with a transparent background. I want to convert them all to gray scale.

So I do this:

Code: Select all

convert -type grayscale foo.gif foo.gif
Unfortunately. that clobbers the transparent background. I've done a ton of searches with "ImageMagick 'preserve transparency'" but have found little regarding GIFs and none in the context of gray scale.

I'm not overly familiar with the guts of image formats, nor am a web designer by trade. I'm just an admin who's been tasked with a bulk conversion. So forgive me if this is a simple problem. I'm hoping someone can give me the correct incantation for "convert" to accomplish what I need to do.

Thanks.

EDIT: I guess I should state that I'm using v6.5.8.10 on FreeBSD.

Re: Convert GIFs to gray scale while preserving transparency

Posted: 2010-03-02T18:08:31-07:00
by fmw42
see the list

convert -list type

you need grayscalematte to save the alpha channel

convert image.gif -type grayscalematte image_gray.gif


also correct IM 6 format has the input image before the options in most cases. see http://www.imagemagick.org/Usage/basics/#why

Re: Convert GIFs to gray scale while preserving transparency

Posted: 2010-03-02T19:05:55-07:00
by SageRaven
Thank you *so* much for your timely reply! Worked like a charm!

Re: Convert GIFs to gray scale while preserving transparency

Posted: 2010-03-02T19:19:52-07:00
by fmw42
this will make your images without an alpha channel have one even if it is totally opaque. so the file size will be larger. you would have to test if the input image had an alpha channel and then decide whether to use -type grayscale or -type grayscalematte.

Unix commands:

test=`convert image -format "%A" info:`
if [ "$test" = "True" ]; then
convert image -type grayscalematte result
else
convert image -type grayscale result
fi

see %A at http://www.imagemagick.org/script/escape.php