Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
    
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. –  OAC Designs 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. –  OAC Designs May 22 '12 at 10:21
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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