Converting an animated gif to a custom grayscale image

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
KenBurns
Posts: 3
Joined: 2015-02-09T21:34:38-07:00
Authentication code: 6789

Converting an animated gif to a custom grayscale image

Post by KenBurns »

Hi all, I've been racking my brain the last few days trying to get this to work and keep running into problems. I'm try to convert an animate gif to a 8-bit grayscale format with a custom color mapping. Essentially the grayscale file is every frame of the animated gif converted to the custom 8-bit mapping, and each frame is just appended to the previous one.

I'm able to do a standard grayscale conversion to get my basic output using:

convert out.gif -append out.gray

However this uses the standard grayscale intensity settings to convert which isn't what I need. However it does convert all the frames of the animated gif and append them to the output file correctly.

Since I need a custom color mapping, I'm using the -fx operator to do this.

convert out.gif -fx "((b*256)&224)/256 + ((g*256)&224)/2048) + ((r*256)&192)/16384)" -append out.gray

However, this only converts the very first frame of the animated gif. How can I do this to convert all frames of the gif?

Any thoughts?

Thanks,

Ken
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting an animated gif to a custom grayscale image

Post by fmw42 »

try adding -coalesce

Code: Select all

convert out.gif -coalesce -fx "((b*256)&224)/256 + ((g*256)&224)/2048) + ((r*256)&192)/16384)" -append out.gray
KenBurns
Posts: 3
Joined: 2015-02-09T21:34:38-07:00
Authentication code: 6789

Re: Converting an animated gif to a custom grayscale image

Post by KenBurns »

Thanks - I've tried to add -coalesce but it's still the same output and didn't fix the issue, it just gives the first frame from the animated gif.

Thanks,

Ken
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting an animated gif to a custom grayscale image

Post by fmw42 »

Yes, -fx only works on the first image in the command line, unless you reference each by u[...]. But I do not think that helps here.

But you can do this loop. Unix syntax.

Code: Select all

num=`convert anim.gif -coalesce -format "%s\n" info: | tail -n 1`
echo $num
for ((i=0; i<=num; i++)); do
convert anim.gif[$i] -fx "((b*256)&224)/256 + ((g*256)&224)/2048) + ((r*256)&192)/16384)" miff:-
done | convert - +append out.png
KenBurns
Posts: 3
Joined: 2015-02-09T21:34:38-07:00
Authentication code: 6789

Re: Converting an animated gif to a custom grayscale image

Post by KenBurns »

Thanks - I was able to get it to work using a shell script like you showed.

Thanks!

Ken
Post Reply