Trim Animated Gif To Bounding Box
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Trim Animated Gif To Bounding Box
For those interested in gif animation modifications, I found this an interesting question and solution. See https://stackoverflow.com/questions/445 ... magemagick
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Trim Animated Gif To Bounding Box
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)...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
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
This assumes the pixel at 0,0 is the background color, of course.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Trim Animated Gif To Bounding Box
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).
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).
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Trim Animated Gif To Bounding Box
I don't think I have an account there. I suppose I should.
I expect those would be some unusually large GIFs.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 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
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Trim Animated Gif To Bounding Box
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.