6

What I want to do is to take an image and to cut it up into small squares that are each in a different layer (not export them as individual images).

For instance if I had an image that is 100px by 100px and wanted to take that one layer and create 100 layers each 10px by 10px squares. The image would look the same, but instead of being one layer it would be a grid of separate layers that seamlessly fit together like a puzzle. If I turned off the visibility of one of the layers it would look as if one square "piece" of the puzzle was missing.

I know that I can slice up an image in a grid, export the images and then open them as layers using Bridge. The problem with this approach is that I would end up with a 10px by 10px file with 100 layers stacked on top of each other instead of a 100px by 100px file with all of the layers arranged properly.

Giacomo1968
  • 58,727
MJR
  • 63
  • 1
  • 1
  • 3

2 Answers2

8

You can do this all using Javascript. Here's a quick little script I've written, it will copy your image into 100 layers, each 10px by 10px:

/*
--------Photoshop Script - Grid to Layers------------
Author: Oisin Conolly
        www.DigitalBiscuits.co.uk

This basic script will create new layers from your active layer, each equal in size according to the grid dimensions specified. */

//this is the size of our squares in pixels var squareSize = 10;

var docRef = app.activeDocument;

//set the ruler type if (app.preferences.rulerUnits != Units.PIXELS) { app.preferences.rulerUnits = Units.PIXELS; }

var layerRef = docRef.activeLayer;

for (y = 0; y<docRef.height; y+=squareSize) { for (x = 0; x<docRef.width; x+=squareSize) { //activate the original layer docRef.activeLayer = layerRef; //make the selection docRef.selection.select(Array (Array(x, y), Array(x, y+squareSize), Array(x+squareSize,y+squareSize), Array(x+squareSize,y)), SelectionType.REPLACE, 0, false);

    //copy the selection
    docRef.selection.copy();
    //create and paste new layer
    docRef.artLayers.add();
    docRef.paste();
}

}

To use it, save that file and load it up with Photoshop by going to

File > Scripts > Browse

Make sure the filetype is set to *.JS

If you want to change the size of your squares just open the JavaSCript file in Notepad, change the value for squareSize and save and run it.


Also, if you want to do more advanced things with this script, you can download a Photoshop Scripting reference guide which lists all the classes, functions and variables you can work with. (For example how to rotate a layer).

The above script uses JavaScript syntax, however you can also use AppleScript, and VBScript to work with Photoshop.

Giacomo1968
  • 58,727
0

I made a few adjustments to @OACDesigns script such that it could:

  • Handle transparent layers correctly, without throwing an error
  • Sliced layers are put in a single layer group
  • Display a prompt for entering the desired grid size
/*
--------Photoshop Script - Grid to Layers------------
Original Script Author: Oisin Conolly (www.DigitalBiscuits.co.uk)
Modified by a stackoverflow user.

This basic script will create new layers from your active layer, each equal in size according to the grid dimensions specified. */

//Use docRef.selection.copy() will throw exception when try to copy empty selection //https://community.adobe.com/t5/photoshop/try-to-copy-when-no-pixels-where-selected/td-p/10417075 function TryCopySelection() { try { executeAction(stringIDToTypeID("copyEvent"), undefined, DialogModes.NO); } catch(e) { return false; } return true; }

//Paste in place is required as otherwise, paste item could be at incorrect position //https://stackoverflow.com/questions/25904603/paste-in-place-photoshop-script cTID = function(s) { return app.charIDToTypeID(s); }; sTID = function(s) { return app.stringIDToTypeID(s); }; function PasteInPlace() { var dialogMode = DialogModes.NO; var desc1 = new ActionDescriptor(); desc1.putBoolean(sTID("inPlace"), true); desc1.putEnumerated(cTID('AntA'), cTID('Annt'), cTID('Anno')); executeAction(cTID('past'), desc1, dialogMode); };

//this is the size of our squares in pixels var squareSize = 1024; squareSize = parseInt(prompt("Please enter slice grid size:", 1024));

var docRef = app.activeDocument;

//set the ruler type if (app.preferences.rulerUnits != Units.PIXELS) app.preferences.rulerUnits = Units.PIXELS;

var sourceLayer = docRef.activeLayer; var layerSet = docRef.layerSets.add(); //Create new layer group layerSet.name = sourceLayer.name + " Group";

for (y = 0; y<docRef.height; y+=squareSize) { for (x = 0; x<docRef.width; x+=squareSize) { //activate the original layer docRef.activeLayer = sourceLayer; //make the selection docRef.selection.select(Array (Array(x, y), Array(x, y+squareSize), Array(x+squareSize,y+squareSize), Array(x+squareSize,y)), SelectionType.REPLACE, 0, false);

    //copy the selection
    if(TryCopySelection())
    {
        var layer = docRef.artLayers.add(); //create and paste new layer
        PasteInPlace()
        layer.move(layerSet, ElementPlacement.INSIDE);
    }
}

}

qris
  • 11