There's plenty of information about cropping images, but attempting to crop (or trim) animations produces strange results. Sometimes they flicker, or come with extra frames, or some frames crop correctly and others become offset. How do I prevent all this from happening?
4 Answers
convert input.gif -coalesce -repage 0x0 -crop WxH+X+Y +repage output.gif
- Animated gifs are often optimised to save space, but imagemagick doesn't seem to consider this when applying the crop command and treats each frame individually.
-coalesce
rebuilds the full frames. - Other commands will take into consideration the offset information supplied in the original gif, so you need to force that to be reset with
-repage 0x0
. - The crop itself is straightforward, with width, height, x offset and y offset supplied respectively. For example, a crop 40 wide and 30 high at an x offset of 50 = 40x30+50+0.
- Crop does not remove the canvas that it snipped from the image. Applying
+repage
after the crop will do this.

- 3,822
- 1
- 30
- 36

- 2,141
- 2
- 16
- 12
-
The command outputs a sequence of separate files with -0, -1, -2 etc appended, rather than a single file as in output.gif – Chris Harrison Feb 23 '13 at 09:57
-
Works fine for me. Much appreciated! – musiKk Apr 22 '13 at 10:04
-
1`+repage` is really what did the trick for me, thanks! ImageMagick was only leaving the canvas for animated .gifs for some reason (didn't happen with .png or .jpegs). – Abe Voelker Apr 26 '15 at 01:59
-
3this is slower as gifsicle and also from my 600kB makes 16MB file – Dusan Plavak Jun 14 '17 at 09:29
-
By the way, if you're having trouble understanding width, height, x offset and y offset, then think of it like this: x-offset and y-offset is where the crop will start from, and width and height wiil be how far the crop will travel. – Paul Chris Jones Nov 09 '18 at 20:04
Even with the coalesce and repage, I could not get ImageMagick to crop and resize animated gifs very well.
I found a program called Gifsicle and it works great for manipulating animated gifs.
gifsicle --crop 0,0-100,100 --output out.gif in.gif
It can also do all sorts of other operations. Check it out!

- 563
- 5
- 7
-
This was helpful for keeping the file size small. ImageMagick blew up the size by 3.7x for my use case. – Wesley Baugh May 25 '16 at 04:46
-
5First 2 numbers are the top left coordinates in pixels, and the other 2 numbers are the bottom right coordinates of the area you want to crop. – Mijo Oct 28 '16 at 02:20
-
2You can either use `--crop X1,Y1-X2,Y2` to input the top-right and bottom-left coordinates to crop between, or `--crop X1,Y1+WxH` to specify the width and height of the area you want – Jezzamon Oct 04 '21 at 00:38
Animations are often optimized, which means that some frames are smaller than others. So in ImageMagick you probably want to coalesce the animation before cropping.
convert in_animation.gif -coalesce -crop WxH+X+Y +repage -layers optimize out_animation.gif
You may need to add a -dispose method before reading the input animation to avoid a flicker. Also set the -delay and -loop at the end, if you want to make changes.
See
http://www.imagemagick.org/Usage/anim_basics/#dispose http://www.imagemagick.org/Usage/anim_basics/#coalesce http://www.imagemagick.org/script/command-line-options.php#layers

- 46,825
- 10
- 62
- 80
The following line worked with me on Mac
convert -dispose 2 input.gif -trim -layers TrimBounds animation.gif
Here is the source

- 591
- 12
- 23
-
1This is the one that finally worked for me. I spent the last half-hour or so trying to figure out how to automatically trim empty space from my gifs, and this answer is what did it. For future people in my time, `convert $input -trim -layers trim-bounds $output` does exactly what you probably need, if the file is properly formatted. The `+repage` option might seem like it does the right thing, but I assure you it does not, in this case. – PrincessRTFM Mar 15 '20 at 23:07