Add padding to tile sheet. [Resolved]

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
poohshoes
Posts: 5
Joined: 2013-11-14T03:52:48-07:00
Authentication code: 6789

Add padding to tile sheet. [Resolved]

Post by poohshoes »

Hey Guys,

I am new to ImageMagick and I am trying to take an image which is a grid of tiles and insert a 2 pixel transparent space between each tile.

Input example:
Image

Output example:
Image
(Extra space around the border would be fine.)

I've manage to separate all the tiles and add a border with:
convert floorTiles.png -crop 128x128 ^
-set filename:tile "%%[fx:page.x/128+1]_%%[fx:page.y/128+1]" ^
+repage +adjoin -border 1x1 "tile_%%[filename:tile].png"

This yields extra images that aren't full size tiles, how do I get rid of them?
How can I use the column_row file name to re-combine the image with every image in the correct location?
How do make the border transparent?
Or perhaps there is an easier way of doing this?

Thanks,
Poohshoes
Last edited by poohshoes on 2013-11-17T23:22:58-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Add padding to tile sheet.

Post by snibgo »

Your input isn't a multiple of 128 pixels because it has a white border on two edges. You can trim this off:

Code: Select all

-bordercolor White -border 1 -trim +repage
So if you want the individual files:

Code: Select all

convert ^
  floorTiles.png ^
  -bordercolor White -border 1 -trim +repage ^
  -crop 128x128 ^
  -set filename:tile "%%[fx:page.x/128+1]_%%[fx:page.y/128+1]" ^
  +repage +adjoin -border 1x1 "tile_%%[filename:tile].png"
Or if you want one row with all 10 tiles:

Code: Select all

convert ^
  floorTiles.png ^
  -bordercolor White -border 1 -trim +repage ^
  -crop 128x128 ^
  -border 1 ^
  +append ^
  -trim +repage ^
  tOneRow.png
Note that I have added a 1-pixel white border around each tile, so they are separated by 2 pixels, and each is 130x130. At the end I remove the outer white border.

If we want 7 on the first row, 7*130 = 910. We crop the long line at 910 and append these vertically.

Code: Select all

convert ^
  floorTiles.png ^
  -bordercolor White -border 1 -trim +repage ^
  -crop 128x128 ^
  -border 1 ^
  +append ^
  -crop 910x130 ^
  -append ^
  -trim +repage ^
  tTwoRows.png
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Add padding to tile sheet.

Post by snibgo »

Sorry, you also asked about transparency. In each command the second added border, around the individual tiles, is white because that was the set "-bordercolor". Instead we can make it transparent:

Code: Select all

-bordercolor None -border 1
snibgo's IM pages: im.snibgo.com
poohshoes
Posts: 5
Joined: 2013-11-14T03:52:48-07:00
Authentication code: 6789

Re: Add padding to tile sheet.

Post by poohshoes »

Thanks so much snibgo. I settled on the following script which, unlike my example, requires the input to have a transparent background.

Code: Select all

convert ^
  floorTiles.png ^
  -bordercolor None -border 1 -trim +repage ^
  -crop 128x128 +repage ^
  -border 1 ^
  +append ^
  -crop 780x130 +repage ^
  -append ^
  -trim +repage ^
  output.png
Cheers,
poohshoes
poohshoes
Posts: 5
Joined: 2013-11-14T03:52:48-07:00
Authentication code: 6789

Re: Add padding to tile sheet.

Post by poohshoes »

I also stumbled upon this http://www.imagemagick.org/Usage/transf ... cing_tiles which may be an easier way of doing it.
Post Reply