Page 1 of 1

ImageMagick and spritesheet

Posted: 2012-11-29T05:48:01-07:00
by larles
Hi,

I'm working on a little script that generates a sprite sheet. I have 6 spritesheets and I need to re-organize them and put their content (once ordered) in a unique file.
I logically chose to use ImageMagick. But here I'm stuck.

Here is what I have so far :

Code: Select all

convert '%d.png[0-5]' \( -crop 456x912+0+0 -crop 3x6+0+0@ +append \) -append  test.png
This command line takes my 6 files (0.png to 5.png) crop them, and split them into 18 sprites.
Once splited, the 18 sprites are aligned horizontally and then aligned vertically with the 18 previous one.

The problem is this command seems to only aligned them horizontally. Instead of being composed of 18x6 sprites, test.png is composed by 108x1 sprites.

Any idea how to perform this in one command ?

Thanks,

Re: ImageMagick and spritesheet

Posted: 2012-11-29T11:19:04-07:00
by fmw42
It is really hard to know exactly what your command does without an image. So this is just a guess.

convert '%d.png[0-5]' \( -crop 456x912+0+0 +repage -crop 3x6@ +repage +append \) -append test.png

Typically you should remove the virtual canvas after a crop using +repage. Also for cropping into multiple pieces you generally do not want +0+0

Re: ImageMagick and spritesheet

Posted: 2012-11-29T13:07:08-07:00
by larles
Yes, you're right, sorry.
So here my example:
http://delta.riskle.com/~larles/harshquad_sprite/

0-5.png are my sources. They're composed of 3columns and 6rows.
I need to take every row on the same line (to have 18columns and 1row), and then append the line to a final file named test.png. Then test.png would be composed of 18columns and 6rows.

The command line you gave me produces the same result than the one I used before : http://delta.riskle.com/~larles/harshqu ... e/test.png
Everything is on the same line instead of having 6 rows. (I hope I'm clear enought).

Re: ImageMagick and spritesheet

Posted: 2012-11-29T15:12:15-07:00
by fmw42
Let me see if I understand. You want to have appended:

image0row1 image1row1 ... image5row1
image0row2 image1row2 ... image5row2
.
.
.
image0row6 image1row6 ... image5row6

Is the above correct?

Do you need to separate the sprites in each row and then reappend them, so as perhaps to trim or crop to some size or for something else?

Re: ImageMagick and spritesheet

Posted: 2012-11-30T01:55:06-07:00
by larles
Thanks again for your quick replies.
I guess images will help you understanding what I want to achieve:

My 6 original files look like those :
Image Image Image

I want to crop them into separate sprites to assemble them into one final file.
The final file has to look like this :
Image

But the command line I gave (as yours) produces something like this:
Image

Is it clearer now ?

Re: ImageMagick and spritesheet

Posted: 2012-11-30T11:43:28-07:00
by fmw42
Try this. Crop each image into approx. equal sizes. Then trim to be sure they are exactly the same size. Then pad appropriates to separate the rows properly before appending horizontally. Then append vertically the previous results for each image


convert ss0.png ss1.png ss2.png -trim +repage \
\( -clone 0 -crop 1x3@ -trim +repage -bordercolor white -border 4 +append \) \
\( -clone 1 -crop 1x3@ -trim +repage -bordercolor white -border 4 +append \) \
\( -clone 2 -crop 1x3@ -trim +repage -bordercolor white -border 4 +append \) \
-delete 0-2 -append result.png

Image

You will have to tune the -border size appropriately for your real image. And you may need to pad it differently in the horizontal and vertical dimensions to make each cell be separated properly in a row and between rows as desired.

You can substitute your wildcard expression in place of the three input images if that will suffice to uniquely identify only those you want.

Re: ImageMagick and spritesheet

Posted: 2012-12-01T02:02:36-07:00
by larles
THANK YOU fmw42 \o/

It works perfectly.
Last question, if I may: in your solution I have to indicate files I want to load and repeat the "same" command for each of them... is there a way to say "open 6 files and apply the same command to them" ?

Thanks again for your time!

Re: ImageMagick and spritesheet

Posted: 2012-12-01T12:42:21-07:00
by fmw42
larles wrote: Last question, if I may: in your solution I have to indicate files I want to load and repeat the "same" command for each of them... is there a way to say "open 6 files and apply the same command to them" ?
You can use wildcards to load the 6 images, if your wildcards uniquely isolate them. Unfortunately you need separate commands for each image so that you append them correctly.

convert '%d.png[0-5]' -trim +repage \
\( -clone 0 -crop 1x3@ -trim +repage -bordercolor white -border 4 +append \) \
\( -clone 1 -crop 1x3@ -trim +repage -bordercolor white -border 4 +append \) \
\( -clone 2 -crop 1x3@ -trim +repage -bordercolor white -border 4 +append \) \
\( -clone 3 -crop 1x3@ -trim +repage -bordercolor white -border 4 +append \) \
\( -clone 4 -crop 1x3@ -trim +repage -bordercolor white -border 4 +append \) \
\( -clone 5 -crop 1x3@ -trim +repage -bordercolor white -border 4 +append \) \
-delete 0-5 -append result.png

Are you trying to deal with an arbitrary number of images with the same command each time?

To do it for any arbitrary number of images, you need to use a subshell process, which is indicated by the ( .... ). You can probably use ls piped to grep to create your list filtered to the images you want. (Thanks to Anthony for teaching me about subshell processing)


list="ss0.png ss1.png ss2.png"
(
for img in $list; do
convert $img -trim +repage -crop 1x3@ -trim +repage -bordercolor white -border 4 +append miff:-
done
) | convert - -append result.png