5

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.

Thanks.

7

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.

* EDIT *

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.

7
  • Thanks so much, this script worked perfectly! And it also scales well if I change the proportions of my image or change the value of squareSize. I really appreciate it!
    – MJR
    May 18 '12 at 14:50
  • I'm relatively new to javascript and I am trying to learn the syntax better. Let's say I wanted to use your script to cut and paste a bunch of little squares from one larger image but wanted to randomize it a bit and rotate every 5th and 17th squares 90 degrees and rotate every 9th square 180 degrees. How would I do that?
    – MJR
    May 18 '12 at 19:17
  • I'm new to scripting with photoshop myself, but what you want to do should be straight forward. You would want to have three variables (lets say a, b and c) incrementing by 1 each time you copy/paste a selection. Then you would have if statements along the lines of: if(a==5){rotate by 90 degrees, reset a to 0} else if(b==17){rotate by 90 degrees, reset b to 0} else if(c==17){rotate by 180 degrees, reset c to 0} You can find the code to rotate a layer in the scripting guide linked in my answer. Give it a go, and if you get stuck show me what you've done and I'll help you.
    – OACDesigns
    May 20 '12 at 12:53
  • 1
    Works well. Probably worth saying do the maths before running. On a 1000 x 1000 pixel file with squaresize = 10 that's 10,000 layers.
    – BJ292
    May 20 '12 at 22:45
  • good point, +1 aside from the memory issues related to having that many layers, it would also take an age to complete.
    – OACDesigns
    May 22 '12 at 10:21
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);
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.