Search code examples
ruby-on-railsrubyrmagick

Merge 'tileset images' into a single background using Ruby


I have a table that is 26 squares by 26 squares. Each square is going to be 30px * 30px.

Given the tiles

  • upper_left.png
  • upper_right.png
  • upper_wall.png

and:

  • left_wall.png
  • right_wall.png

and:

  • bottom_left.png
  • bottom_wall.png
  • bottom_right.png

I aim to comprise a background that is 780px*780px. For the sake of speed and to prevent possible rendering errors, I want these tilesets to be composited into one big background. Making 8 tiles was easy, but I'm not a designer...
I believe I need to use an image library like RMagick to put them together, but I have no idea where to begin.

Ideally I'd like to just iterate over the image as though it were a multidimensional array of 26 rows and columns. That way I could just load up each spot with the image I need, and write it out to a file.

Can anybody point me in the right direction as to how I can manipulate images using a program?

The eventual goal is to just upload a tile_set.zip with the 8 images to the server, and have it do this on its own.

But for now, just a simple Ruby shell script would suffice.

Thanks in advance :)


Solution

  • I won't write the script for you, but ImageMagick's +append and -append are the tools that will solve this. The corresponding RMagick documentation is here.