Convert GIFs to gray scale while preserving transparency

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
SageRaven

Convert GIFs to gray scale while preserving transparency

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert GIFs to gray scale while preserving transparency

Post 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
SageRaven

Re: Convert GIFs to gray scale while preserving transparency

Post by SageRaven »

Thank you *so* much for your timely reply! Worked like a charm!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert GIFs to gray scale while preserving transparency

Post 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
Post Reply