How do I split a Photoshop Document into a grid?

2

1

If I have a Photoshop Document containing an image for example. How can I break it into a grid ? Similar to doing: - Select a rectangle - new Layer Via Cut - move to the next rectangle vertically repeat ?

Any scripts/plugins that automate/parameterize that ?

George Profenza

Posted 2010-01-22T16:42:52.590

Reputation: 728

Answers

2

All nice answers, thanks for the contribution.

As I said, I wondering if there was something out there for Photoshop.

For documentation purposes here is a little script I wrote:

var doc  = app.activeDocument;
var arranger = makeDialog();

arranger .arrange.onClick = function(){
    if(isNaN(parseInt(arranger.w.text)) || isNaN(parseInt(arranger.h.text)) || isNaN(parseInt(arranger.c.text)) || isNaN(parseInt(arranger.r.text))){
        alert('Yo! Can you feed \' dis dialog some integer numbers ?\n For Real!');
    }else{
        sliceLayer(parseInt(arranger.w.text),parseInt(arranger.h.text),parseInt(arranger.r.text),parseInt(arranger.c.text));
    }
    arranger.close();
}

arranger.show();

function sliceLayer(w,h,r,c){
    var hw = w * 0.5;
    var hh = h * 0.5;
    var l0 = doc.activeLayer;
    for(var j = 0 ; j < r ; j++){
        for(var i = 0 ; i < c; i++){
            var x = i * w; //edited
            var y = j * h; //here
            //select
            doc.selection.select([[x,y],[x+w,y],[x+w,y+h],[x,y+h]]);
            //new layer via cut
            doc.selection.cut();
            var layer  = doc.artLayers.add();
            doc.paste();
            //move
            //$.writeln('translate x: '+(y)+', y: '+(x));
            // move layer with relative coordinates
            // layer.translate(y,x); 
            // move layer with absolute coordinates
            MoveLayerTo( layer, x, y );
            doc.activeLayer = l0;
        }
    }
    l0.remove();//remove original layer(now empty)
}


function makeDialog(){
        var dialog = new Window('dialog','Slice and Dice');
        dialog.frameLocation = [300,300];
        dialog.w = dialog.add('edittext',undefined,'Selection width');
        dialog.w.prefferedSize = [200,50];
        dialog.h = dialog.add('edittext',undefined,'Selection height');
        dialog.h.prefferedSize = [200,50];
        dialog.c = dialog.add('edittext',undefined,'Grid Columns(V sections)');
        dialog.c.prefferedSize = [200,50];
        dialog.r = dialog.add('edittext',undefined,'Grid Rows(H sections)');
        dialog.r.prefferedSize = [200,50];
        dialog.arrange = dialog.add('button',undefined,'Chop Chop');
        return dialog;
}

//******************************************
// MOVE LAYER TO
// Author: Max Kielland
//
// Moves layer fLayer to the absolute
// position fX,fY. The unit of fX and fY are
// the same as the ruler setting. 

function MoveLayerTo(fLayer,fX,fY) {

    var Position = fLayer.bounds;
    Position[0] = fX - Position[0];
    Position[1] = fY - Position[1];

    fLayer.translate(-Position[0],-Position[1]);
}

If you save this in Photoshop/Presets/Scripts as a .jsx file, you should be able to access it through Scripts in the File menu. You can also use File -> Scripts -> Browse... with the script anywhere on your computer.

It will ask for four things:

  • Selection width — How wide you want each block in the grid to be
  • Selection height — How tall you want each block in the grid to be
  • Grid Columns (V sections) — How many columns there should be once it's split up
  • Grid Rows (H sections) — How many rows there should be once it's split up

If you get an error about the document not being found, you just need to open an image first before running it.

The problem with this JSX approach is, it is a bit slow for large subdivisions (100x100).

I'm guessing it will be faster to try with MATLAB/Photoshop communication, but I'll skip that for now.

Achieved the same goal in actionscript 3 using copyPixels() and it's pretty fast.

George Profenza

Posted 2010-01-22T16:42:52.590

Reputation: 728

4

The only way I've done this is to layout a grid with guides, then slice the image using the slice tool. This should allow you to break the image into many seperate pieces. After using the slice tool you can Save For Web, and export only the slices, each as a separate image file.

warrenkopp

Posted 2010-01-22T16:42:52.590

Reputation: 421

thanks, but I'm afraid that's way to long winded, I'm to lazy for that :) – George Profenza – 2010-01-31T01:38:43.143

1

It is similar to a question I answered here: I did recommend a free tool that allows you to do this, you export the png, load the png there and hit the button....

Other soutions were proposed, too.

Image editor that allows multiple, separate, simultaneous crop selections to be saved? (looking for both free & paid programs)

S.gfx

Posted 2010-01-22T16:42:52.590

Reputation: 1 643