Batch crop multiple sprites while preserving midpoint

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
Tusker
Posts: 3
Joined: 2015-11-13T22:38:40-07:00
Authentication code: 1151

Batch crop multiple sprites while preserving midpoint

Post by Tusker »

I have thousands of character sprites (though, really they're just PNG files with transparency) which were saved out from Maya, which is a 3D program.

These are character animations, with almost all of them being 12 frames each. Each animation is rendered from each of the 8 cardinal directions.

In each animation, the character is already centered in the frame (this is how it's exported from Maya); however, each image has extra transparent space around it that I'd like to crop down. (edit: to clarify, each source frame is PNG and contains the character only, and is on a transparent background)

I need to batch crop these sprites horizontally from the outside -> in order to preserve the character's "midpoint" in the frame. The reason for this is if it's not preserved, as the game swaps from one animation to the next, the character would appear to move or jump around. So, it's important the character is horizontally centered (using the midpoint of their body) in the frame across all their animations. Therefore, equal amounts have to be cropped from both the left and the right, if that makes sense.

Additionally... the character needs to be at or very near the bottom edge of the sprite.

For the coup de grace... the final cropped image needs to be an even number, or divisible by 2, for game engine reasons.

The source images are sequential PNG's (just single frames in order), and the newly cropped images need to be the same thing. I literally just need the cropping; no converting or anything like that. I don't need sprite sheets or anything like that.

Is there a way to go about doing this with IM? If it's complex, I'd be willing to pay someone to develop this. Thanks in advance!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch crop multiple sprites while preserving midpoint

Post by fmw42 »

It would help if you could provide an example. You can post to some place such as dropbox.com and put the URL here. Also provide the IM version and platform you are using.

Also see -crop at http://www.imagemagick.org/Usage/crop/#crop_equal
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch crop multiple sprites while preserving midpoint

Post by snibgo »

Perhaps append the sprites vertically, then trim, then chop them apart.
snibgo's IM pages: im.snibgo.com
Tusker
Posts: 3
Joined: 2015-11-13T22:38:40-07:00
Authentication code: 1151

Re: Batch crop multiple sprites while preserving midpoint

Post by Tusker »

Thanks for the responses.

preview image and sample images here: http://imgur.com/a/gpgDR

This is a very basic preview and sample images. I can't show any of the actual sprites for various reasons, but this is the exact same concept, just using circles.

The entire process is almost like finding the smallest bounding rect from an image sequence, but the difference is that horizontal symmetry is preserved. In my example: even though it could crop off the negative space on the right side, I don't want to do that because it would then make the circle off-center, and I need to preserve the circle's horizontal mid-point/symmetry.

As for what IM version and platform I'm using... at the moment: none. I just figured if I could get regular IM via command line to do this, I'd use that. Otherwise, I'm open to any suggestions, as I'm very much in need of a solution to this. Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch crop multiple sprites while preserving midpoint

Post by snibgo »

Appending them vertically, and trimming the result, works fine, eg:

Code: Select all

f:\web\im>%IM%convert -gravity Center -background None xc:red yke2OU2.png S4cRBXu.png 6da6xe5.png xc:red -append -trim out.png
The single pixels at the top and bottom prevent them getting trimmed off. They can then be chopped off, and crop will give you the sprites.

An alternative is to process one at a time to get format %@ to find what would be trimmed, and some processing per set calculates what trim is common to all the elements in the set, and actually to crop with those values. This is more complex, but may be needed for your extra add-ons like making the width divisable by 2.
snibgo's IM pages: im.snibgo.com
Tusker
Posts: 3
Joined: 2015-11-13T22:38:40-07:00
Authentication code: 1151

Re: Batch crop multiple sprites while preserving midpoint

Post by Tusker »

Hi snibgo, thanks for your feedback. I tested out your sample code, and it worked, however it doesn't preserve the negative space I need on the right side in order to keep the circle centered horizontally. Would that have to be calculated and added back in later on? Otherwise, this seems to be going in the right direction.

The "alternative" you mention sounds like it might be needed.
Post Reply