Search code examples
photoshoptile

Removing the spacing between tiles in tilesheet


So I have an image which contains a tile-sheet, where each tile is approx 16 pixels wide, and high. But there spaced out with a transparent spacer between each tile. Like so:

Have

But this is ugly, and makes displaying the sprites in the program annoying, not to mention it wastes valuable image space. Is there any easy (Besides me manually using Photoshop to move each individual tile) way to make it look like this?

Want

I looked through Photoshop macros, as-well as other programs and I diden't seem to find anything that would directly do this. Google also suggests I go to home-depo and get tile caulk remover.


Solution

  • Try this snippet. As you said, it assumes tiles are always going to be 16 pixels. Top left one is in the correct position and a single pixel gap. The script assumes the document will opened with the layer containing your tiles set as the active layer.

    #target photoshop
    app.preferences.rulerUnits = Units.PIXELS;
    app.preferences.typeUnits = TypeUnits.PIXELS;
    
    var gap = 1;
    var tileSize = 16;
    var doc = app.activeDocument.duplicate();
    var sourceLyr = doc.activeLayer;
    var xTilePosition = 0;
    var yTilePosition = 0;
    
    for (var x = 0; x < sourceLyr.bounds[2]; x = x+ tileSize + 1 ) {
    
        for (var y = 0; y < sourceLyr.bounds[3]; y = y + tileSize + 1) {
    
            if (x > 0 || y > 0) {
                app.activeDocument = doc;
                doc.activeLayer = sourceLyr;
                selRegion = Array(Array(x, y),
                                        Array(x + tileSize, y),
                                        Array(x + tileSize, y + tileSize),
                                        Array(x, y + tileSize),
                                        Array(x, y))
                doc.selection.select(selRegion);
    
                var dx = x - (xTilePosition * tileSize);
                var dy = y - (yTilePosition * tileSize);
    
                doc.selection.translate(0 - dx, 0 - dy);
            }
    
            yTilePosition ++;
        }
    
        xTilePosition++;
        yTilePosition = 0;
    }