Page 1 of 1

animation TrimBounds does nothing

Posted: 2011-06-20T16:03:57-07:00
by SEWilco
I'm having trouble trimming an animation. The "-layers TrimBounds" option seems to be what is needed, but I can't get it to do anything. The input is 128x128 and the output also is that size.

With these two images, both of which are 128x128 PNG with transparency:
image1.png: http://img15.imageshack.us/img15/9229/shime2.png
image2.png: http://img233.imageshack.us/img233/1988/shime3.png

Code: Select all

convert image1.png image2.png -background None -layers TrimBounds tmp.miff
The resulting tmp.miff remains size 128x128:

Code: Select all

identify tmp.miff
tmp.miff[0] MIFF 128x128 128x128+0+0 8-bit TrueColor DirectClass 40.5KB 0.000u 0:00.000
tmp.miff[1] MIFF 128x128 128x128+0+0 8-bit TrueColor DirectClass 40.5KB 0.000u 0:00.000
I am able to trim the individual images with:

Code: Select all

 -geometry '+0+0' -trim
but that isn't useful due to alignment problems. I need the alignment which is the reason why TrimBounds was created.

I tried various other incantations, trying to tell it in various ways what to do with the transparency. The IM examples don't hint at any such difficulties with transparency.

Linux Ubuntu 10.10

Version: ImageMagick 6.6.2-6 2010-12-02 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2010 ImageMagick Studio LLC
Features: OpenMP

Re: animation TrimBounds does nothing

Posted: 2011-06-20T17:32:04-07:00
by anthony
I think you are mis-reading what the layers method does!

Trim bounds are for images that contain a virtual canvas. It trims the virtual canvas to match the real images contained in the the sequence.

Unless you PNG image have a virtual canvas and/or offsets it will not do anything except set the virtual canvas to the largest bounds of the input images.

So lest back off a little..

What do you want to do?

Re: animation TrimBounds does nothing

Posted: 2011-06-20T19:42:25-07:00
by SEWilco
Yes, I've mistakenly read the layers option as having behavior similar to "-trim". Not all trimming is the same. Sigh.

I wanted to trim all of the layered images down to the same size, based upon the maximum dimensions of the non-transparent parts of the images.

Re: animation TrimBounds does nothing

Posted: 2011-06-20T20:16:49-07:00
by fmw42
I am not sure I understand? Do you want the same size for each frame or the minimum size for each frame such that each frame might have a different size.

If the former, then for each image you can extract the alpha channel, use -trim to save as PNG or MIFF (but not +repage so as to save the virtual canvas offsets), then get the canvas size and offsets of the result from the verbose info (or from string formats %P and %0 -- see http://www.imagemagick.org/script/escape.php), then compare to get the largest from all the frames, then crop your animation accordingly.

If the latter, then use -coalesce to separate each frame and apply -trim +repage to remove the offsets, then recombine as a new animation.
see http://www.imagemagick.org/Usage/anim_mods/

If I misunderstand, please clarify. Also perhaps provide a link to the animation you want to modify.

Re: animation TrimBounds does nothing

Posted: 2011-06-20T23:04:38-07:00
by anthony
Okay you want to 'trim' the transparency, to the smallest size such that all images are the same size, and still remain separate!

Try this

Code: Select all

convert image1.png image2.png -bordercolor none -border 1 -trim +repage \
            -layers TrimBounds -set dispose background -coalesce \
            -scene 1  trimed_images_%d.png
  • This adds a transparent border (so as to be sure we are trimming only transparency)
  • trims the images to the smallest individual size possible.
  • removes any virtual offests that may be left (optional -- remote if you want to preserve relative offset of each image)
  • Now we use TrimBounds to set the smallest virtual canvas bounds that can hold BOTH images. Even if X bound is from from one and the Y bound was from the other image.
  • and finally a 'coalesce' trick to pad out the images to that virtual canvas size with transparency, while still keeping the two images separate!
  • and save the images as two separate files numbered 1 and 2 respectively
The above pulls techniques and methods from all over ImageMagick Examples, including
Trimming with a Specific Color
http://www.imagemagick.org/Usage/crop/#trim_color
Removing Canvas/Page Geometry
http://www.imagemagick.org/Usage/crop/#crop_repage
Page, Repage, and the Virtual Canvas
http://www.imagemagick.org/Usage/basics/#page
Bounds Trimming - canvas size correction
http://www.imagemagick.org/Usage/anim_mods/#trim
Coalesce, a Progressive Layering
http://www.imagemagick.org/Usage/layers/#coalesce
Written Scene Numbers
http://www.imagemagick.org/Usage/files/#scene

There are lots of ways to skin a cat, and what method you use depends
on what you want that skin for, and how messy you like the results!
-- Anthony Thyssen

Re: animation TrimBounds does nothing

Posted: 2011-06-21T19:52:54-07:00
by SEWilco
Yes, anthony, that does indeed do what I wanted. Thanks a lot. Quite a bit more complex than what I thought would do the trick.

Re: animation TrimBounds does nothing

Posted: 2011-06-21T21:03:38-07:00
by anthony
If you do not mind preserving the relative offset of the objects in the image, there is another solution.
extract the transparency channel (masks) from all the images, and merge them together (flatten with screen or plus composition and a black background). Trim that to get the maximum bounds, then use the infromation from the trim to apply to the original set of images.

This may be faster, or it may be slower. It means only one trim is needed, and you don't have to 'fill out' the virtual canvas of the individual images again, however it does need more compostion steps, though internally there is less temporary image creations required.

Unfortunatally you currently can not easily set arguments from other image attributes , to use the trim results as a crop argument. As such, you need two separate IM commands to make it work (or a API interface) :-(

It is a little like a 'fuzzy trim' but for a different purpose.
http://www.imagemagick.org/Usage/crop/#trim_blur

That problem something that will be fixed in IMv7.