Montage a whole bunch of tiles?

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
NotTarts

Montage a whole bunch of tiles?

Post by NotTarts »

I've got a folder containing heaps of 512x512 tiles (around 1700), and I wondering how I could merge them together into a single image using montage. They're in this format:

tile_x_y.png (eg, tile_3_-21.png).

I've tried doing it myself with a bat, but I haven't had any success so far.

And yeah, I know the resulting image will be 1.7GP, but bear with me.

EDIT: I'm using the Windows binaries, if that makes a difference.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Montage a whole bunch of tiles?

Post by GreenKoopa »

Wow, 1700 images! Typing in all those names is clearly not an option. ImageMagick can input files using * or ? wildcards, but I don't know how the order works out. Hopefully well since you would almost need a Perl script to generate the batch file.

What have you tried and where are you stuck? Can you get a 2x2 tile area working?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Montage a whole bunch of tiles?

Post by anthony »

I don't know about the handling of negatives, that may make things very tricky!
But if the tiles are all the same size and you can rename them so the sort properly,
you should have no problems.


I have a perl script that allows me to rename files using reformatted numbers. With a little work I can get it to add a constant (to make negatives positive) and then format the number to include the right number of leading zeros so the resulting filenames are all the same length and sort alphabetically.
Of course you will probably also want to make the number order Y then X so they are in row-order.

The technique is based on a script called "mv_perl"
http://www.ict.griffith.edu.au/~anthony ... e/#mv_perl
which includes a perl regular expression for reformatting numbers.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
NotTarts

Re: Montage a whole bunch of tiles?

Post by NotTarts »

Thanks for the help. It's appreciated :)

I did write a batch file that turned all the values positive (by finding the width and adding to the values from there), but I can't get it to work with montage properly, since it's in a map format (which means there are areas that don't have a tile) I think the best thing to do here is:

1. Find the width and height of all the tiles combined (not too difficult)
2. Use convert to create a blank image from the width and height
3. Get the coordinates from the filename
4. Composite the tiles onto the blank image

It's step 3 that I have a problem with. There just doesn't seem to be a way to extract the coordinates from the filename.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Montage a whole bunch of tiles?

Post by anthony »

In montage replace any area that does NOT have a tile with the special image "null:"

In montage this means 'skip this cell'.

See IM Examples, Montage - Arrays of Images
Leaving Gaps in a Montage
http://www.imagemagick.org/Usage/montage/#null
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Montage a whole bunch of tiles?

Post by anthony »

An alturnative is to use a layers method.

In each image, give the image an appropriate virtual offset.

For example for tile 2,3 which are 50x50 pixels place this image at offset +100+150

Code: Select all

      mogrify -repage +100+150  tile_2_3.png
Note that PNG normally can not save virtual canvas size information, though PNG IM saves does save a IM specific metadata with that info. However the PNG format can save virtual offsets.

Once all the images have the right offset you can layer all images simply with...

Code: Select all

    convert tiles_*.png  -background none -layers merge  results.png
NOTE: I used layers merge as it will ignore the virtual canvas of the input image, and creates a final image just large enough to hold all the images (including any positive or negative offset)

See the very next section of IM examples, Layering Images.
http://www.imagemagick.org/Usage/layers/#merge

I do this with my 'jigsaw' tile pieces which are more tricky in that they have unusual offsets that overlap each other (so the pieces fit together).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Wolfgang Woehl
Posts: 34
Joined: 2010-02-25T15:22:50-07:00
Authentication code: 8675308

Re: Montage a whole bunch of tiles?

Post by Wolfgang Woehl »

NotTarts wrote:3. Get the coordinates from the filename ... It's step 3 that I have a problem with. There just doesn't seem to be a way to extract the coordinates from the filename.
With ruby this is one way to do that:

Code: Select all

x,y = 'tile_3_-21.png'.match( /(-?\d+)_(-?\d+)/ )[ 1 .. -1 ].collect { |n| n.to_i }
which matches a string to a regular expression (0 or 1 '-' and 1 or more digits) twice (separated by an underscore) and turns those string matches into integers (to_i). Python, shell, perl ... any script language will do that.
Post Reply