Page 1 of 1

Trim Animated Gif To Bounding Box

Posted: 2017-06-15T11:08:27-07:00
by fmw42
For those interested in gif animation modifications, I found this an interesting question and solution. See https://stackoverflow.com/questions/445 ... magemagick

Re: Trim Animated Gif To Bounding Box

Posted: 2017-06-16T20:29:44-07:00
by GeeMack
fmw42 wrote: 2017-06-15T11:08:27-07:00For those interested in gif animation modifications, I found this an interesting question and solution. See https://stackoverflow.com/questions/445 ... magemagick
As we migrate to IM7 there will be some pretty streamlined ways to accomplish the same things as in IM6. Often we'll be able to obtain image data and use those specs in the same command without using external variables or intermediate files. The rotating bunny GIF image from your linked discussion can be trimmed using a command like this in IM7 (with Windows CMD syntax)...

Code: Select all

magick oHBWq.gif -coalesce +repage ( -clone 0--1 -transparent %[pixel:p{0,0}] -flatten -trim ) ^
   -crop %[fx:u[-1].w]x%[fx:u[-1].h]+%[fx:u[-1].page.x]+%[fx:u[-1].page.y] +repage -delete -1 newgif.gif
That clones the input frames and uses their background color to be the transparent color. Then it flattens the clones and trims the transparent excess to the minimum that still contains all the trimmed images. Then it uses the dimensions and offsets from that result to crop all the original frames. Then it deletes the flattened clone image. Finally it reassembles the cropped original frames for the output GIF.

This assumes the pixel at 0,0 is the background color, of course.

Re: Trim Animated Gif To Bounding Box

Posted: 2017-06-16T20:55:13-07:00
by fmw42
Nice solution GeeMack! Why don't you post that solution on the stackoverflow topic.

The only potential issue I have is that you need to clone every frame, which could be memory intensive for large animations (in number of frames and dimensions).

Re: Trim Animated Gif To Bounding Box

Posted: 2017-06-16T22:03:33-07:00
by GeeMack
fmw42 wrote: 2017-06-16T20:55:13-07:00Nice solution GeeMack! Why don't you post that solution on the stackoverflow topic.
I don't think I have an account there. I suppose I should.
The only potential issue I have is that you need to clone every frame, which could be memory intensive for large animations (in number of frames and dimensions).
I expect those would be some unusually large GIFs.

I played with the idea using IM 6.9.7-6. It seems you can accomplish the same thing using a distort viewport instead of cropping.

Code: Select all

convert oHBWq.gif -coalesce +repage -background none ( -clone 0--1 -trim -flatten -trim ) ^
   -set option:distort:viewport %[fx:u[-1].w]x%[fx:u[-1].h]+%[fx:u[-1].page.x]+%[fx:u[-1].page.y] ^
   -distort SRT 0 +repage -delete -1 newgif_IM6.gif
That clones the input frames, trims them individually, and flattens them keeping their original alignment. Then it trims that flattened one again to get rid of the excess transparent background. The result will be the right size and have the correct page offsets for the finished image. You don't have to know the background color.

Now you can easily get those dimensions and offsets into a distort viewport setting and do the no-op distort. Delete the cloned flattened one that was used to get the measurements, "+repage" the rest, and finish with whatever other GIF settings you need.

Re: Trim Animated Gif To Bounding Box

Posted: 2017-06-16T22:30:26-07:00
by fmw42
Your solution is a good example of the use of fx computations in-line. The usage of the viewport is a good idea for IM 6. Both solutions are very clever.