Restricting the changing part of an animated GIF

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
ZaneBlue
Posts: 2
Joined: 2014-05-15T17:14:25-07:00
Authentication code: 6789

Restricting the changing part of an animated GIF

Post by ZaneBlue »

I have some gameplay footage that I want to turn into an animated GIF. I've done this successfully, but I'd like to optimize it.

I've tried the techniques in http://www.imagemagick.org/Usage/video/, and the ordered dither and -layers Optimize methods both produce significant optimization but not very acceptable results.

The background doesn't really change, but being the nature of gameplay footage, it appears to change to ImageMagick in subtle ways that mess with optimization. What I'd like to do is to find some way to make static everything but the small area in the center that does change. On a single frame basis, I could layer the first image over each other frame, using a mask to make the center area transparent.

My question is, can this be done without generating intermediate images that have to be stored on disk? That is to all in one fell swoop:
Generate the unchanging part by using mask to make the center area transparent (if the mask can be generated on the fly too, that would be awesome)
Step through each of the *.png images, layering the unchanging part over the frame.
Assembling the results into an animated gif, with whatever optimization takes advantage of the now unchanging parts of each frame.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Restricting the changing part of an animated GIF

Post by fmw42 »

If I understand, you can do it with -mask. Here is an example of a 4 image sequence (you can just use *.png). I draw a circle for the mask and apply it with -mask. But in order to do it in one command you need to have created the mask beforehand or as I did it, by using mpr: in memory image format. You cannot do it with clones for the mask. Try this on your own and you will see. You can create the mask ahead of time or replace my circle with some other command to make your image into some mask, such as thresholding or something like that. You can add gif optimization commands, delays and looping control as desired.

Code: Select all

convert rose: rose: rose: rose: \
\( +clone -fill black -colorize 100 -fill white -draw "circle 35,23 35,40" -write mpr:circle +delete \) \
 -mask mpr:circle -fill none -draw "matte 0,0 filltoborder" +mask rose2.gif
The above is unix syntax. On Windows, remove the \ before the parens and replace the end of line \ with ^

In the future, please identify your IM version and platform.
ZaneBlue
Posts: 2
Joined: 2014-05-15T17:14:25-07:00
Authentication code: 6789

Re: Restricting the changing part of an animated GIF

Post by ZaneBlue »

Version: ImageMagick 6.8.8-9 Q16 x86_64 2014-03-23
Platform: MacOSX Mavericks

The provided code (thank you, btw) works, but produces an animated gif with a transparent background. I fiddled with it, and this is what I came up with:

Code: Select all

convert -delay 1x30 -loop 0 *.png \
\( +clone -fill black -colorize 100 -fill white -draw "rectangle 166,143 248,256" -write mpr:rectmask +delete \) \
-mask mpr:rectmask -draw "image SrcOver 0,0 0,0 danceanim001.png" +mask -layers Optimize danceanim.gif
This uses the first frame of the animation (danceanim001.png) as the background. Note that it's important to have the -layers Optimize at the end, otherwise it doesn't appear to actually do any optimization. This resulted in a final gif that was 1/20th the size of the original with no loss of quality, so a big win!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Restricting the changing part of an animated GIF

Post by fmw42 »

On a single frame basis, I could layer the first image over each other frame, using a mask to make the center area transparent.
I was not 100% sure what you wanted. I thought you wanted a transparent background (from the statement above). But you can put any background you want there as you did. When I said you could add optimization and delay and loop controls, I assumed you knew how to do that. Typically -delay and -dispose come before the images and -loop comes right before the output with -optimize just before that. But I guess IM is not that particular where the -loop is placed.
Post Reply