3

I have an image that contains smaller images similar to this:

------
| 1  |
------
| 2  |
------
| 3  |
------
| 4  |
------

Each sub-image is 100x200, and the whole image is 100x800

I would like to combine them all into a single animation that will loop through images 1 to 4.

I use imagemagick's convert tool to crop the images. For example, I made a batch file

convert %1 -crop 100x200+0+0 %~n1_01.png
convert %1 -crop 100x200+0+200 %~n1_02.png
convert %1 -crop 100x200+0+400 %~n1_03.png
convert %1 -crop 100x200+0+600 %~n1_04.png

And then run it on an image called main.jpg

This creates a set of images, but when I try to convert them to a gif like so

convert *.png out.gif

Instead of a single animation of size 100x200, I get an animation of size 100x800, and the images just pop up at certain offsets.

When I identify a couple of the cropped images, it gives me

main_01.png PNG 100x200 100x800+0+0 8-bit sRGB 4.74KB 0.000u 0:00.000
main_02.png PNG 100x200 100x800+0+200 8-bit sRGB 3.39KB 0.000u 0:00.000

Presumably, imagemagick is trying to be helpful by reading the metadata and determining that I would in fact like to use a 100x800 canvas, and each image should be placed at the specific offsets. Which is not what I want.

indeed, reading the tutorial, imagemagick does leave it in there to help you out.

That is IM retains the 'virtual canvas', 'page', or 'layering' information of the image so as to preserve it for later use. This is especially important for the correct working for GIF animation handling.

When I open each file manually in paint and then re-save it, when I re-identify main_02.png, I get

main_02.png PNG 100x200 100x200+0+0 8-bit sRGB 4.03KB 0.000u 0:00.000

Doing this for all of the images, I do the conversion to gif again and finally it is a 100x200 gif which looks like what I want.

How can I set all of the offsets to (0, 0) so that I don't have to do it manually through paint?

2
  • 1
    When you say "reset the offsets" what do you mean? You want to restore the offsets from the original images back into the image re-saved by paint? Or you want to remove the offsets from the original images without using paint? – Jason C Jul 5 '14 at 2:38
  • @JasonC I would like to remove the original offsets. – MxLDevs Jul 5 '14 at 2:40
2

You can reset the virtual canvas coordinates by doing +repage after the crop:

convert %1 -crop 100x200+0+0 +repage %~n1_01.png
convert %1 -crop 100x200+0+200 +repage %~n1_02.png
convert %1 -crop 100x200+0+400 +repage %~n1_03.png
convert %1 -crop 100x200+0+600 +repage %~n1_04.png

Also you don't need a batch file to split the image into tiles; if you specify a size only it will split into tiles for you, use "%d" in the output file name for the tile number:

convert %1 -crop 100x200 +repage %~n1_%d.png

Or in your case with the two-digit fixed-width number:

convert %1 -crop 100x200 +repage %~n1_%02d.png

That will generate four images.

In fact, you can also do the GIF conversion in the same command, without having to generate temporary intermediate image files at all. The following command will do everything you want in one fell swoop:

convert %1 -crop 100x200 +repage out.gif

For more information see the documentation for -crop and also +repage (note that -repage can also be used to edit virtual canvas data in any image by itself).


By the way, an alternate way to split images is to specify the number of tiles instead of the size of the tiles; you can do this by specifying the size in tiles to crop and appending an "@" sign, e.g. this produces the same results as above:

convert %1 -crop 1x4@ +repage %~n1_%02d.png
3
  • Probably would have never knew about the @ sign without reading the documentation on geometry....which probably wouldn't happen since I assume I knew how geometries worked! – MxLDevs Jul 5 '14 at 3:01
  • @MxyL The answer I gave you was incorrect, I edited it. I misremembered the purpose of "@". Simply specify the size with no offset to split into tiles. The "@" is for when you want to specify the number of tiles instead. Sorry! Also; you can convert to animated GIF in one command with no intermediate files (see edit). – Jason C Jul 5 '14 at 3:19
  • Updating my script to use the @ made it much more flexible since I've got tiled images with different number of frames. – MxLDevs Jul 6 '14 at 4:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.