• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Divide my image to layers

Community Beginner ,
Oct 22, 2021 Oct 22, 2021

Copy link to clipboard

Copied

Hi, I'm trying to divide a high res image into 100 separate rectangles, then I need each separate rectangle in its own layer so I can turn them on and off. I've looked at the slice tool but this saves separate images elsewhere. I need to have all divided slices in one file on separate layers. (So when all layers are 'turned on' the image appears normal.)

Is there a way to do this please? Any help appreciated.

Using PS CC.

Thanks

TOPICS
Actions and scripting , macOS , Windows

Views

5.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 22, 2021 Oct 22, 2021

A while back I believe Pfaffenbichler posted a script that dices up an image onto layers according to guidelines.  I use his script  in an action that has an interactive New Guide Layout step before the Script step. Its easy to dice up an image that way. Here the script again.

 

// split Image to layer according to guides;
// 2015, use it at your own risk;
#target photoshop

if (app.documents.length > 0) {
	var myDocument = app.activeDocument;
	var myResolution = myDocument.resolution;
	var theL
...

Votes

Translate

Translate
Adobe
Adobe Employee ,
Oct 22, 2021 Oct 22, 2021

Copy link to clipboard

Copied

This is a job for scripting. Hopefully one of the folks on here who write scripts can help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 22, 2021 Oct 22, 2021

Copy link to clipboard

Copied

Slice markup is not a bad way. Here is a script* that copies the contents of each slice to a separate layer**

 

#target photoshop
var s2t = stringIDToTypeID,
    AR = ActionReference,
    AD = ActionDescriptor;

try {
    try {(r = new AR).putProperty(s2t('property'), p = s2t('layerID'));
    r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
    var id = executeActionGet(r).getInteger(p);}
    catch (e) {throw "No layer selected!\nOpen the document and select layer"}

    try {(r = new AR).putProperty(s2t('property'), p = s2t('slices'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var slices = executeActionGet(r).getObjectValue(p).getList(p);} 
    catch (e) {throw "This version of photoshop does not have access to slices"}

    for (var i = 0; i < slices.count - 1; i++) {
        (r = new AR).putIdentifier(s2t('layer'), id);
        (d = new AD).putReference(s2t('target'), r);
        executeAction(s2t('select'), d, DialogModes.NO);

        (r = new AR).putProperty(s2t('channel'), s2t('selection'));
        (d = new AD).putReference(s2t('target'), r);
        d.putObject(s2t('to'), s2t('rectangle'),
            function (b, d) {
                for (var i = 0; i < b.count; i++)
                    d.putUnitDouble(k = (b.getKey(i)), s2t('pixelsUnit'), b.getInteger(k))
                return d;
            }(slices.getObjectValue(i).getObjectValue(s2t('bounds')), new AD)
        );
        executeAction(s2t('set'), d, DialogModes.NO);

        try {executeAction(s2t('copyToLayer'), undefined, DialogModes.NO)}
        catch (e) {throw "Script cannot create layer from empty space!\nMake sure that current layer contains pixels in all slices"}
    }
} catch (e) { alert(e) }

 

* script will work only on the latest versions of Photoshop. If you are really using CC (v14), then you need another solution
** at the time of launching the script, the layer that needs to be divided into parts must be selected

2021-10-23_11-50-46.png

2021-10-23_11-53-52.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 25, 2021 Oct 25, 2021

Copy link to clipboard

Copied

Thank you. I'm not in a position to upgrade my PS, but I really appreciate your response.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 22, 2021 Oct 22, 2021

Copy link to clipboard

Copied

quote

I've looked at the slice tool but this saves separate images elsewhere.

==========

Starting with what you have now  -- image slices saved as individual files, simply use File > Scripts > Load files into stack.  I think that solves the problem without writing additional scripts.

 

Nancy O'Shea— Product User, Community Expert & Moderator
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 25, 2021 Oct 25, 2021

Copy link to clipboard

Copied

Thanks for your response. It does indeed load the slices into a file, stacked. But I need them aligned/assembled to make the original image. Also using Image Slice only renders 72dpi files. My search continues....

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 22, 2021 Oct 22, 2021

Copy link to clipboard

Copied

A while back I believe Pfaffenbichler posted a script that dices up an image onto layers according to guidelines.  I use his script  in an action that has an interactive New Guide Layout step before the Script step. Its easy to dice up an image that way. Here the script again.

 

// split Image to layer according to guides;
// 2015, use it at your own risk;
#target photoshop

if (app.documents.length > 0) {
	var myDocument = app.activeDocument;
	var myResolution = myDocument.resolution;
	var theLayer = myDocument.activeLayer;
	var layerID = getLayerId(theLayer);
	var originalRulerUnits = app.preferences.rulerUnits;
	app.preferences.rulerUnits = Units.POINTS;
	// check guides;
	var theVer = new Array;
	var theHor = new Array;
	var theNumber = myDocument.guides.length;
	for (var m = 0; m < theNumber; m++) {
		if (myDocument.guides[m].direction == Direction.HORIZONTAL) {theHor.push(myDocument.guides[m].coordinate)};
		if (myDocument.guides[m].direction == Direction.VERTICAL) {theVer.push(myDocument.guides[m].coordinate)};
	};
	// sort and add beginning and end;
	theHor = treatGuideArray (theHor, app.activeDocument.height);
	theVer = treatGuideArray (theVer, app.activeDocument.width);
	$.writeln(theHor.join("\n")+"\n\n\n"+theVer.join("\n"));
	// create selections;
	for (var y = 0; y < theHor.length - 1; y++) {
		var Y1 = theHor[y];
		var Y2 = theHor[y+1];
		for (var x = 0; x < theVer.length - 1; x++) {
			try {
				var X1 = theVer[x];
				var X2 = theVer[x+1];
				rectangularSelection([Y1, X1, Y2, X2], false);
				// layer via copy;
				var id14 = charIDToTypeID( "CpTL" );
				executeAction( id14, undefined, DialogModes.NO );
				// add mask;
				intersectedLayerMask (layerID)
			} catch (e) {};
		// reselct layer;
		myDocument.activeLayer = theLayer;
		};
	};
	activeDocument.selection.deselect();
	// reset the ruler units;
	app.preferences.rulerUnits = originalRulerUnits
};
////////////////// the functions //////////////////
////// treat array //////
function treatGuideArray (theArray, theExtreme) {
	theArray.sort(function(a,b){return a - b});
	if (Number (theArray[theArray.length - 1]) != theExtreme) {theArray.push(theExtreme)};
	if (Number (theArray[0]) != 0) {theArray.unshift(new UnitValue(0, "pt"))};
	theArray.sort(function(a,b){return a - b});
	return theArray;
};
////// rectangular selection //////
function rectangularSelection (theBounds, add) {
	// =======================================================
	if (add == false ||  add == undefined) {var idsetd = charIDToTypeID( "setd" )}
	else {var idsetd = charIDToTypeID( "AddT" )};
		var desc55 = new ActionDescriptor();
		var idnull = charIDToTypeID( "null" );
			var ref11 = new ActionReference();
			var idChnl = charIDToTypeID( "Chnl" );
			var idfsel = charIDToTypeID( "fsel" );
			ref11.putProperty( idChnl, idfsel );
		desc55.putReference( idnull, ref11 );
		var idT = charIDToTypeID( "T   " );
			var desc56 = new ActionDescriptor();
			var idTop = charIDToTypeID( "Top " );
			var idRlt = charIDToTypeID( "#Rlt" );
			desc56.putUnitDouble( idTop, idRlt, theBounds[0] );
			var idLeft = charIDToTypeID( "Left" );
			var idRlt = charIDToTypeID( "#Rlt" );
			desc56.putUnitDouble( idLeft, idRlt, theBounds[1] );
			var idBtom = charIDToTypeID( "Btom" );
			var idRlt = charIDToTypeID( "#Rlt" );
			desc56.putUnitDouble( idBtom, idRlt, theBounds[2] );
			var idRght = charIDToTypeID( "Rght" );
			var idRlt = charIDToTypeID( "#Rlt" );
			desc56.putUnitDouble( idRght, idRlt, theBounds[3] );
		var idRctn = charIDToTypeID( "Rctn" );
		desc55.putObject( idT, idRctn, desc56 );
	executeAction( idsetd, desc55, DialogModes.NO );
};
// by mike hale, via paul riggott;
function getLayerId(theLayer){
	// http://forums.adobe.com/message/1944754#1944754
	app.activeDocument.activeLayer = theLayer;
	//Assumes activeDocument and activeLayer
		var ref = new ActionReference();
		ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
		d = executeActionGet(ref);
	return d.getInteger(charIDToTypeID('LyrI'));
};
////// load transparency, ontersect with layer mask ofd another layer, add layer mask //////
function intersectedLayerMask (layerID) {
	var idChnl = charIDToTypeID( "Chnl" );
	// =======================================================
	var idsetd = charIDToTypeID( "setd" );
		var desc2 = new ActionDescriptor();
		var idnull = charIDToTypeID( "null" );
			var ref1 = new ActionReference();
			var idfsel = charIDToTypeID( "fsel" );
			ref1.putProperty( idChnl, idfsel );
		desc2.putReference( idnull, ref1 );
		var idT = charIDToTypeID( "T   " );
			var ref2 = new ActionReference();
			var idTrsp = charIDToTypeID( "Trsp" );
			ref2.putEnumerated( idChnl, idChnl, idTrsp );
		desc2.putReference( idT, ref2 );
	executeAction( idsetd, desc2, DialogModes.NO );
	// =======================================================
	var idIntr = charIDToTypeID( "Intr" );
		var desc3 = new ActionDescriptor();
		var idnull = charIDToTypeID( "null" );
			var ref3 = new ActionReference();
			var idMsk = charIDToTypeID( "Msk " );
			ref3.putEnumerated( idChnl, idChnl, idMsk );
			var idLyr = charIDToTypeID( "Lyr " );
			ref3.putIdentifier( idLyr, layerID );
		desc3.putReference( idnull, ref3 );
		var idWith = charIDToTypeID( "With" );
			var ref4 = new ActionReference();
			var idfsel = charIDToTypeID( "fsel" );
			ref4.putProperty( idChnl, idfsel );
		desc3.putReference( idWith, ref4 );
	executeAction( idIntr, desc3, DialogModes.NO );
	// =======================================================
	var idMk = charIDToTypeID( "Mk  " );
		var desc4 = new ActionDescriptor();
		var idNw = charIDToTypeID( "Nw  " );
		desc4.putClass( idNw, idChnl );
		var idAt = charIDToTypeID( "At  " );
			var ref5 = new ActionReference();
			var idMsk = charIDToTypeID( "Msk " );
			ref5.putEnumerated( idChnl, idChnl, idMsk );
		desc4.putReference( idAt, ref5 );
		var idUsng = charIDToTypeID( "Usng" );
		var idUsrM = charIDToTypeID( "UsrM" );
		var idRvlS = charIDToTypeID( "RvlS" );
		desc4.putEnumerated( idUsng, idUsrM, idRvlS );
	executeAction( idMk, desc4, DialogModes.NO );
};

 

Here is what my Action look like.

image.png

image.png

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 25, 2021 Oct 25, 2021

Copy link to clipboard

Copied

Thanks for your response. This is rather above my 'pay-grade' as I've never created or run a script before. How do I actually use this complex code? Can I decide how many slices/boxes result? Will they fall into a file registered and aligned in separate layers?

Thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 25, 2021 Oct 25, 2021

Copy link to clipboard

Copied

Save the code as Script.jsx in Presets/Scripts folder of your Photoshop. After (re)launching the application you can find the script as exactly same named 'Script' item in File / Scripts menu.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 25, 2021 Oct 25, 2021

Copy link to clipboard

Copied

Will this assemble the image so it looks right with all layers turned on? And in original high resolution? (Slices make low-res web images) Both of these are deal-breakers for the job.

I've been experimenting and 'Load files into Stack' is nearly what's required, but it still requires manual re-assembly. 

(I know it sounds crazy but client wants image diced into 1000 parts!)

Thanks again.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 25, 2021 Oct 25, 2021

Copy link to clipboard

Copied

That was only instruction how to run the script 😉

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 25, 2021 Oct 25, 2021

Copy link to clipboard

Copied

Ah yes of course. Ok thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 25, 2021 Oct 25, 2021

Copy link to clipboard

Copied

Info on how to save and use scripts here:

 

Downloading and Installing Adobe Scripts

 

My earlier post offers scripts to split a large image into separate tiles, and another script to re-assemble.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 25, 2021 Oct 25, 2021

Copy link to clipboard

Copied

Thanks I'll give this a try.

 

 

Nailed it! Thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2021 Oct 26, 2021

Copy link to clipboard

Copied

@Fenton61 


Did you mark my post as a correct answer because of the link to scripts to divide and combine?

 

Or for the link on saving and installing scripts...

 

Or for both?

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 27, 2021 Oct 27, 2021

Copy link to clipboard

Copied

I was surprised this answer was marked as correct solution too.

I'm not remember well but I think @J453 could do it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 27, 2021 Oct 27, 2021

Copy link to clipboard

Copied

Both scripts were the solution. Sorry, I have marked as correct answer now. (New to the forum).

Thanks again.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 27, 2021 Oct 27, 2021

Copy link to clipboard

Copied

Shouldn't the post with script from Oct 23, 2021 be marked as correct solution?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 27, 2021 Oct 27, 2021

Copy link to clipboard

Copied

I'm happy to mark it as such, but I don't see a post from 23rd (may be a time difference thing? I'm in UK).

Trying to do the right thing as everyone has been very helpful.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 27, 2021 Oct 27, 2021

Copy link to clipboard

Copied

LATEST

The date I posted linking you to right post, just click it or find it by author: JJMack

 

btw. marking another posts as correct solution with each visit won't be helpful for new users.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 22, 2021 Oct 22, 2021

Copy link to clipboard

Copied

Some related options here:

 

Adobe Photoshop - Scripts Related to Montaging

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines