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

Place and create layer mask of selected images with shape layers.

Participant ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

Hello,

I have different photoshop collage template.

Each has different number of shape layers in order to use as photo mask.

I would like to automatically populate those shape layers from selected images. (Selected image's total number would be different than square count. So I would like to repeat with the same images or randomize selections.)

I appreciate any help, thank you. ( Below you may find my initial script )

For example first thing I got stuck on is how to make condition - if layer name matches "Photo" then select Photo1 place first selected image above and create layer mask, and etc...

sc1.jpg

sc1b.jpg

sc2.jpg

#target photoshop

app.bringToFront();

cTID = function(s) { return app.charIDToTypeID(s);}; sTID = function(s) { return app.stringIDToTypeID(s);};

function collectNamesAM(re){var allLayers = new Array();var startLoop = Number( !hasBackground() );var endLoop = getNumberOfLayer();for( var l = startLoop;l < endLoop; l++){  while( !isValidActiveLayer( l ) ) {l++;}   if( getLayerNameByIndex( l ).match(re) != null){allLayers.push( l );}}return allLayers; }; 

function getActiveLayerIndex() { var ref = new ActionReference(); ref.putProperty( 1349677170 , 1232366921 );  ref.putEnumerated( 1283027488, 1332896878, 1416783732 );  var res = executeActionGet(ref).getInteger( 1232366921 )  - Number( hasBackground() );   res == 4 ? res++:res ;    return res;     } 

function isValidActiveLayer( idx ) {var propName = stringIDToTypeID( 'layerSection' );var ref = new ActionReference();ref.putProperty( 1349677170 , propName);ref.putIndex( 1283027488, idx );var desc =  executeActionGet( ref );var type = desc.getEnumerationValue( propName );       var res = typeIDToStringID( type );        return res == 'layerSectionEnd' ? false:true;  }; 

function hasBackground(){var res = undefined;try{var ref = new ActionReference();ref.putProperty( 1349677170 , 1315774496);ref.putIndex( 1283027488, 0 );executeActionGet(ref).getString(1315774496 );    res = true;     }catch(e){ res = false}      return res;  }; 

function makeActiveByIndex( idx, forceVisible ){try{var desc = new ActionDescriptor();var ref = new ActionReference();ref.putIndex(charIDToTypeID( "Lyr " ), idx)  ;desc.putReference( charIDToTypeID( "null" ), ref ); desc.putEnumerated(sTID("selectionModifier"), sTID("selectionModifierType"), sTID("addToSelection"));

desc.putBoolean( charIDToTypeID( "MkVs" ), forceVisible ); executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );}catch(e){ return -1;}  }; 

function getNumberOfLayer(){var ref = new ActionReference();ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );var desc = executeActionGet(ref);var numberOfLayer = desc.getInteger(charIDToTypeID("NmbL"));return numberOfLayer;}; 

function getLayerNameByIndex( idx ) {var ref = new ActionReference();ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " ));ref.putIndex( charIDToTypeID( "Lyr " ), idx );return executeActionGet(ref).getString(charIDToTypeID( "Nm  " ));;}; 

function placeEmbeded(filePath) {var actDesc = new ActionDescriptor(); actDesc.putPath(charIDToTypeID('null'), filePath);actDesc.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa'));executeAction(charIDToTypeID('Plc '), actDesc, DialogModes.NO); } 

function main() {

var doc=app.activeDocument;

var myFolder = Folder("/../..c:");

var myFiles = [];

var myMask = "JPG:*.jpg";

myFiles = File.openDialog("Selection prompt",myMask,true);

var FileCount  = myFiles.length;

var re = /Photo/// -- Write between /    /  your string...

var matches = collectNamesAM(re);// get array of matching layer indexes  

for( var m = 0; m < matches.length; m++ ){   

   

    makeActiveByIndex( matches, true );      

   

   //alert (getLayerNameByIndex(m));

   placeEmbeded(myFiles);  

}

}

main();

TOPICS
Actions and scripting

Views

2.9K
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
Adobe
Community Expert ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

You could create selections for your shape layers and save alpha channels to map your images.  Use then instead of your shape layers.  Create simpler template instead of your more complex one.  Photoshop limits is 53 Alpha channels.  Your Collage template will be limited to 53 Images.  However you can tile out as many images as you want the Photoshop 8,000 layer limit.

If you need more than 53 Images.  You could try to change my scripts to useShape layers instead of Alpha channels for mapping images Sizes locations and shape.  My Scripts are saved as plain text not save binary.. IMO you should not try to forde files names and layer mane to be a match,  You should be able to populate and image file.  You design also doe not seem to address sizeing???.

Photo Collage Toolkit

Photoshop scripting is powerful and I believe this package demonstrates this here is a video showing a 5 image collage PSD template  being populated with images

The package includes four simple rules to follow when making Photo Collage Template PSD files so they will be compatible with my Photoshop scripts.

  1. Size the photo collage templates for the print size you want - width, height and print DPI resolution.
  2. Photo collage templates must have a Photoshop background layer. The contents of this layer can be anything.
  3. Photo collage templates must have alpha channels named "Image 1", "Image 2", ... "Image n".
  4. Photo collage templates layers above the background layers must provide transparent areas to let the images that will be placed below them show through.

There are fifteen scripts in this package they provide the following functions:

  1. HelpPhotoCollageToolkit.jsx - Online Help
  2. TestCollageTemplate.jsx - Used to test a Photo Collage Template while you are making it with Photoshop.
  3. CollageTemplateBuilder.jsx - Can build Templates compatible with this toolkit's scripts.
  4. LayerToAlphaChan.jsx - Used to convert a Prototype Image Layer stack into a template document.
  5. InteractivePopulateCollage.jsx - Used to interactively populate Any Photo Collage template. Offers most user control inserting pictures and text.
  6. ReplaceCollageImage.jsx - use to replace a populated collage image Smart Object layer with an other image correctly resized and positioned.
  7. ChangeTextSize.jsx - This script can be used to change Image stamps text size when the size used by the populating did not work well.
  8. PopulateCollageTemplate.jsx - Used to Automatically populate a Photo Collage template and leave the populated copy open in Photoshop.
  9. BatchOneImageCollage.jsx - Used to Automatically Batch Populate Collage templates that only have one image inserted. The Collage or Image may be stamped with text.
  10. BatchMultiImageCollage.jsx - Used to Automatically Batch Populate Any Photo Collage template with images in a source image folder. Easier to use than the interactive script. Saved collages can be tweaked.
  11. PasteImageRoll.jsx - Paste Images into a document to be print on roll paper.
  12. BatchPicturePackage.jsx - Used to Automatically Batch Populate Any Photo Collage template with an image in a source image folder.  Images Rotates for best fit.
  13. BatchPicturePackageNoRotate.jsx - Used to Automatically Batch Populate Any Photo Collage template with an image in a source image folder.
  14. PopulatePicturePackage.jsx - Used to Automatically populate a Photo Collage template Fill with the same single image and leave the populated copy open in Photoshop.
  15. PCTpreferences.jsx - Edit This File to Customize Collage Populating scripts default setting and add your own Layer styles.

Documentation and Examples

JJMack

Votes

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
Participant ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

Thanks but what I need is more simple here also I would like to my templates. I decided to use Smart Objects pre masked. Their name starts with "PhotoMask".

Now script seems to be working but;

*for example if user selects more images than available layers it should turn back to first image or continue adding images randomly between selected images.

*Also it would be much more useful if we can get an array of initial PhotoMask sizes and coordinates and resize replaced images with these coordinates.

It's getting complicated for me as a graphic designer so any additional help would still be great:) Here is my latest unfinished script;

#target photoshop

app.bringToFront();

var docName = decodeURI(activeDocument.name);

// Define pixels as unit of measurement

var defaultRulerUnits = preferences.rulerUnits;

preferences.rulerUnits = Units.PIXELS;

cTID = function(s) { return app.charIDToTypeID(s);}; sTID = function(s) { return app.stringIDToTypeID(s);};

var allLayers = new Array();

var layerNames = new Array();

var layerCoordinates = new Array();

// Main

function main() {

var doc=app.activeDocument;

var myFolder = Folder("/../..c:");

var myFiles = [];

var myMask = "JPG:*.jpg";

myFiles = File.openDialog("Selection prompt",myMask,true);

var FileCount  = myFiles.length;

var re = /PhotoMask/ ;

var matches = collectNamesAM(re);// get array of matching layer indexes  

for( var m = 0; m < FileCount; m++ ){   

    //alert (allLayers);

layerNames = getLayerNameByIndex(allLayers);

alert(layerNames );

selectlayer (1,0,String(layerNames));

replacecontents(1,0,myFiles);

preferences.rulerUnits = defaultRulerUnits; // Set preferences back to user 's defaults

}

}

function collectNamesAM(re){

    var startLoop = Number( !hasBackground() );var endLoop = getNumberOfLayer();

    for( var l = startLoop;l < endLoop+1; l++){

        while( !isValidActiveLayer( l ) ) {l++;}

        if( getLayerNameByIndex( l ).match(re) != null){

           

            allLayers.push( l );

            layerNames.push(l);

            layerCoordinates.push (

            }

        }

    return allLayers;

    return layerNames;

}; 

function getActiveLayerIndex() { var ref = new ActionReference(); ref.putProperty( 1349677170 , 1232366921 );  ref.putEnumerated( 1283027488, 1332896878, 1416783732 );  var res = executeActionGet(ref).getInteger( 1232366921 )  - Number( hasBackground() );   res == 4 ? res++:res ;    return res;     } 

function isValidActiveLayer( idx ) {var propName = stringIDToTypeID( 'layerSection' );var ref = new ActionReference();ref.putProperty( 1349677170 , propName);ref.putIndex( 1283027488, idx );var desc =  executeActionGet( ref );var type = desc.getEnumerationValue( propName );       var res = typeIDToStringID( type );        return res == 'layerSectionEnd' ? false:true;  }; 

function hasBackground(){var res = undefined;try{var ref = new ActionReference();ref.putProperty( 1349677170 , 1315774496);ref.putIndex( 1283027488, 0 );executeActionGet(ref).getString(1315774496 );    res = true;     }catch(e){ res = false}      return res;  }; 

function makeActiveByIndex( idx, forceVisible ){try{var desc = new ActionDescriptor();var ref = new ActionReference();ref.putIndex(charIDToTypeID( "Lyr " ), idx)  ;desc.putReference( charIDToTypeID( "null" ), ref ); desc.putEnumerated(sTID("selectionModifier"), sTID("selectionModifierType"), sTID("addToSelection"));

desc.putBoolean( charIDToTypeID( "MkVs" ), forceVisible ); executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );}catch(e){ return -1;}  }; 

function getNumberOfLayer(){var ref = new ActionReference();ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );var desc = executeActionGet(ref);var numberOfLayer = desc.getInteger(charIDToTypeID("NmbL"));return numberOfLayer;}; 

function getLayerNameByIndex( idx ) {var ref = new ActionReference();ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " ));ref.putIndex( charIDToTypeID( "Lyr " ), idx );return executeActionGet(ref).getString(charIDToTypeID( "Nm  " ));;}; 

function placeEmbeded(filePath) {var actDesc = new ActionDescriptor(); actDesc.putPath(charIDToTypeID('null'), filePath);actDesc.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa'));executeAction(charIDToTypeID('Plc '), actDesc, DialogModes.NO); } 

function selectlayer(enabled, withDialog,layername) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putName(cTID('Lyr '), layername);

    desc1.putReference(cTID('null'), ref1);

    desc1.putBoolean(cTID('MkVs'), false);

    var list1 = new ActionList();

    list1.putInteger(70);

    desc1.putList(cTID('LyrI'), list1);

    executeAction(cTID('slct'), desc1, dialogMode);

  };

// Replace Contents

function replacecontents(enabled, withDialog,filename) {

     if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    desc1.putPath(cTID('null'), new File(filename));

    executeAction(sTID('placedLayerReplaceContents'), desc1, dialogMode);

  };

main();

screenshot.jpg

Votes

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 ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

Replace content requires that all replacement Image be the same size are resolutions of the original object used in your templates. For all Smart object layers have an associated transform that is not replaced or altered when you replace the layers objects contents... If the size and resolution are not the same as the associated transform will not work correctly.  If you want to be able to populate  your template with any size images you need to design in resizing images to populating your template image location.

Using Smart Object layer in templates are very good for creating Picture packages and template and other types of templates where all image are the same size and the populated layers need to have some kind of image prospective added by the associated smart Object Layers transform.

Your example look like you are just tiling images

screenshot.jpg

to do something like that does not ever require a template.  I simple script can do that.  One is included in my Photo Collage Toolkit.

http://www.mouseprints.net/old/dpr/PasteImageRoll.html

http://www.mouseprints.net/old/dpr/PasteImageRoll.jsx

JJMack

Votes

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
Participant ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

Actually they are not in same size. I have created unique collage templates and all of them has different sizes than each other. Please check my latest post I have updated it a bit.

To simply;

- There are pre-prepared layers in document with the name "PhotoMask1..2..3" ( Total count is not the same like 3x3,4x4...)

- Find all layers including PhotoMask and create an index.

- Let user select multiple images.

- Select PhotoMask layer in order and replace its contents with selected images one by one.

- Resize Smart Object's size proportionally to the closest value of Photo Mask.

Votes

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
Participant ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

Ok, now my script looks like this;

I still could not find a way to find percentage difference between original smart object and layer mask.(It calculate height difference values as "infinity")

I need to scale smart object size according to layer mask's size. It should be proportional and closest value to layer mask. Of course I need to calculate both layer mask's proportion and smart object photo's proportion.

As a non-coder please check my script and help:)...

#target photoshop

app.bringToFront();

var docName = decodeURI(activeDocument.name);

// Define pixels as unit of measurement

var defaultRulerUnits = preferences.rulerUnits;

preferences.rulerUnits = Units.PIXELS;

cTID = function(s) { return app.charIDToTypeID(s);}; sTID = function(s) { return app.stringIDToTypeID(s);};

var allLayers = new Array();

var height;

var width;

var layerNames = new Array();

var layerxCoordinates = new Array();

var layeryCoordinates = new Array();

var layerWidth = new Array();

var layerHeight = new Array();

// Main

function main() {

var doc=app.activeDocument;

var myFolder = Folder("/../..c:");

var myFiles = [];

var myMask = "JPG:*.jpg";

myFiles = File.openDialog("Selection prompt",myMask,true);

var FileCount  = myFiles.length;

var re = /PhotoMask/ ;

var matches = collectNamesAM(re);// get array of matching layer indexes  

for( var m = 0; m < FileCount; m++ ){   

    //alert (allLayers);

layerNames = getLayerNameByIndex(allLayers);

//alert(layerNames );

selectlayer (1,0,String(layerNames));

getposition (doc.activeLayer);

getLayerSize(doc.activeLayer,width,height);

replacecontents(1,0,myFiles);

positionLayer(   doc.activeLayer,   layerxCoordinates,  layeryCoordinates     );

alert (layerHeight);

currentlayer = doc.activeLayer;

wcurrent = currentlayer.bounds[2]-currentlayer.bounds[0];

hcurrent = currentlayer.bounds[3]-currentlayer.bounds[1];

differencex = 100/ (wcurrent - layerWidth);

differencey = 100 / (hcurrent - layerHeight);

alert (differencex);

alert (differencey);

ResizeImage(1,0,differencex,differencey);

preferences.rulerUnits = defaultRulerUnits; // Set preferences back to user 's defaults

}

}

// ______________________________________//

function getposition (lyr) {

    var x = lyr.bounds[0].value;

    var y = lyr.bounds[1].value;

layerxCoordinates.push(x);

layeryCoordinates.push (y);  

  }

function positionLayer( lyr, x, y ){// layerObject, Number, Number 

     // if can not move layer return 

     if(lyr.iisBackgroundLayer||lyr.positionLocked) return 

     // get the layer bounds 

     var layerBounds = lyr.bounds; 

     // get top left position 

     var layerX = layerBounds[0].value; 

     var layerY = layerBounds[1].value; 

     // the difference between where layer needs to be and is now 

     var deltaX = x-layerX; 

     var deltaY = y-layerY; 

     // move the layer into position 

     lyr.translate (deltaX, deltaY); 

function collectNamesAM(re){

    var startLoop = Number( !hasBackground() );var endLoop = getNumberOfLayer();

    for( var l = startLoop;l < endLoop+1; l++){

        while( !isValidActiveLayer( l ) ) {l++;}

        if( getLayerNameByIndex( l ).match(re) != null){

           

            allLayers.push( l );

            layerNames.push(l);

            }

       }

    return allLayers;

    return layerNames;

}; 

function getActiveLayerIndex() { var ref = new ActionReference(); ref.putProperty( 1349677170 , 1232366921 );  ref.putEnumerated( 1283027488, 1332896878, 1416783732 );  var res = executeActionGet(ref).getInteger( 1232366921 )  - Number( hasBackground() );   res == 4 ? res++:res ;    return res;     } 

function isValidActiveLayer( idx ) {var propName = stringIDToTypeID( 'layerSection' );var ref = new ActionReference();ref.putProperty( 1349677170 , propName);ref.putIndex( 1283027488, idx );var desc =  executeActionGet( ref );var type = desc.getEnumerationValue( propName );       var res = typeIDToStringID( type );        return res == 'layerSectionEnd' ? false:true;  }; 

function hasBackground(){var res = undefined;try{var ref = new ActionReference();ref.putProperty( 1349677170 , 1315774496);ref.putIndex( 1283027488, 0 );executeActionGet(ref).getString(1315774496 );    res = true;     }catch(e){ res = false}      return res;  }; 

function makeActiveByIndex( idx, forceVisible ){try{var desc = new ActionDescriptor();var ref = new ActionReference();ref.putIndex(charIDToTypeID( "Lyr " ), idx)  ;desc.putReference( charIDToTypeID( "null" ), ref ); desc.putEnumerated(sTID("selectionModifier"), sTID("selectionModifierType"), sTID("addToSelection"));

desc.putBoolean( charIDToTypeID( "MkVs" ), forceVisible ); executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );}catch(e){ return -1;}  }; 

function getNumberOfLayer(){var ref = new ActionReference();ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );var desc = executeActionGet(ref);var numberOfLayer = desc.getInteger(charIDToTypeID("NmbL"));return numberOfLayer;}; 

function getLayerNameByIndex( idx ) {var ref = new ActionReference();ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " ));ref.putIndex( charIDToTypeID( "Lyr " ), idx );return executeActionGet(ref).getString(charIDToTypeID( "Nm  " ));;}; 

function ResizeImage(enabled, withDialog,resizex,resizey) {

  // Transform

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    desc1.putEnumerated(cTID('FTcs'), cTID('QCSt'), sTID("QCSAverage"));

    var desc2 = new ActionDescriptor();

    desc2.putUnitDouble(cTID('Hrzn'), cTID('#Pxl'), 0);

    desc2.putUnitDouble(cTID('Vrtc'), cTID('#Pxl'), 0);

    desc1.putObject(cTID('Ofst'), cTID('Ofst'), desc2);

    desc1.putUnitDouble(cTID('Wdth'), cTID('#Prc'), 10); // Percentage X

    desc1.putUnitDouble(cTID('Hght'), cTID('#Prc'), 10); // Percentage Y

    desc1.putBoolean(cTID('Lnkd'), true);

    executeAction(cTID('Trnf'), desc1, dialogMode);

};

  ResizeImage();

function placeEmbeded(filePath) {var actDesc = new ActionDescriptor(); actDesc.putPath(charIDToTypeID('null'), filePath);actDesc.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa'));executeAction(charIDToTypeID('Plc '), actDesc, DialogModes.NO); } 

function getLayerSize(layer,width,height){

    // Calculate length and width based on the rectangular bounds of the selected layer

    width = layer.bounds[2]-layer.bounds[0]; //Grab the length

    height = layer.bounds[3]-layer.bounds[1]; //Grab the width

    width= width.toString().replace(' px', '');

    height = height.toString().replace(' px', '');

   

    layerWidth.push(parseInt(width));

    layerHeight.push(parseInt(height));

    return layerWidth;

    return layerHeight;

}

function selectlayer(enabled, withDialog,layername) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putName(cTID('Lyr '), layername);

    desc1.putReference(cTID('null'), ref1);

    desc1.putBoolean(cTID('MkVs'), false);

    var list1 = new ActionList();

    list1.putInteger(70);

    desc1.putList(cTID('LyrI'), list1);

    executeAction(cTID('slct'), desc1, dialogMode);

  };

// Replace Contents

function replacecontents(enabled, withDialog,filename) {

     if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    desc1.putPath(cTID('null'), new File(filename));

    executeAction(sTID('placedLayerReplaceContents'), desc1, dialogMode);

  };

main();

Votes

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 ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

arteangelus  wrote

Ok, now my script looks like this;

I still could not find a way to find percentage difference between original smart object and layer mask.(It calculate height difference values as "infinity")

I need to scale smart object size according to layer mask's size. It should be proportional and closest value to layer mask. Of course I need to calculate both layer mask's proportion and smart object photo's proportion.

Your Smart Object layer is being scaled and perhape distorted by your template's smart Object layers associated transform.  The Smart object layer's layer mask would be masking the pixels rendered for the transformed object.

You can get the bounds of the Smart Object Layer's Layer mask if the layer has a layer mask.  It will be rectangular which may not be the shape of the layer mask.

I do not think you have a good understanding as to how Smart Object Layer work and layer mask work. Before you can script Photoshop you need to know how Photoshop works.

JJMack

Votes

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
Participant ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

How you know that I don't know  how photoshop works:)

I think you did not read my whole question and script.

Smart Objects are placing into scene with their original sizes and since there is no link between smart object and its mask we are able to see its original pixel values right? For example my initial photomask areas size is 200x200. And it has a layer mask with exactly same pixel size. After I replace my content it's coming with original size. Because I'm using replace content command. And I'm able to see that replaced image's size is 2000x2000 px from transform menu right? So it should be %10 for both width and height.

That's what I'm asking otherwise without transform it is working. So there is no any distortion in smart object. We can manually lock proportion and resize smart object by %10 without any distortion no?

So I got no issue using smart objects I just wanted to ask how would I proportionally scale them to initial photo mask's sizes?

If you look at my script I am able to store initial photo mask width and height sizes into an array.

Votes

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 ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

I know because I did read what you wrote.  All Smart Object layers  are scaled and may be distorted by the layers associated transform.   The object is the full size image being placed in and it may well be scaled by you Photoshop Preferences resized during place.  If the image does not need to be scaled to fit on canvas or you set the preference not to resize the transform scale will be 100%.  A Smart Object Layer associated transform is not replaced or updated  when you replace the layers object.  You do not seem to know this.  If you did you would know the replacement object must be the same size as the orignal object so repmacemen will be transformed like the onignal object is being transformed.

JJMack

Votes

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
Participant ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

before.jpg

after replacement;

after.jpg

May be I could not explain well, look while it is still smart object pixel value changes. I just replaced content with another image here.  That's what I'm trying to do. Store previous (initial layer mask pixel values.) than compare to replaced image's size. Find percentage difference than scale replace image size to this percentage. If they are not equal and calculate width height proportion and make the closest resizing.

Votes

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
Participant ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

By the way I might not use smart objects as well but it will create another task as creating layer clipping with the below mask layer. Like this it would be easier for user to move, rotate or scale afterwards. For example please look at this image below, I have 307 layers. And I would like to make photo mosaic from selected images.

screenshot.jpg

Votes

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 ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

Here is what will happen when the replacement Image is the same size and resolution as the original image object And what will happen when they are not the same size.  Here all images are Canon D20 Raw files however all orientations are not Landscape.   Layer Mask will not change the layers size or location.   Layer Mask just mask layers pixels they do not resize or mover the layers pixels.  There are no layer mask in this example.  I just replace the document single shared smart object. All Layer mask here would do is preserve Canvas white areas in the template.  Replacement Image must be the same size and resolution as the image in the template.

Capture.jpg

JJMack

Votes

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
Participant ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

It is able to populate each layer includes "PhotoMask"  with selected images. And if selected images count is less than layer count it turns back to initial one and repeats.

Now only problem is to compare initial layer size with replaced image's size and resize replaced image's size proportionally by percentage. Still needs an expert touch I guess.

#target photoshop

app.bringToFront();

var docName = decodeURI(activeDocument.name);

// Define pixels as unit of measurement

var defaultRulerUnits = preferences.rulerUnits;

preferences.rulerUnits = Units.PIXELS;

cTID = function(s) { return app.charIDToTypeID(s);}; sTID = function(s) { return app.stringIDToTypeID(s);};

var allLayers = new Array();

var layerNames = new Array();

var f = 0;

// Main

function main() {

var doc=app.activeDocument;

var myFolder = Folder("/../..c:");

var myFiles =  new Array();

var myMask = "JPG:*.jpg";

myFiles = File.openDialog("Selection prompt",myMask,true);

alert (myFiles);

var FileCount  = myFiles.length;

var count = FileCount;

var re = /PhotoMask/ ;

var matches = collectNamesAM(re);// get array of matching layer indexes  

var layercount = allLayers.length;

for( var m = 0; m < layercount; m++ ){   

   

layerNames = getLayerNameByIndex(allLayers);

selectlayer (1,0,String(layerNames));

$.write (myFiles[m%myFiles.length]) ;

replacecontents(1,0,myFiles[m%myFiles.length]);

}

}

// ______________________________________//

function collectNamesAM(re){

    var startLoop = Number( !hasBackground() );var endLoop = getNumberOfLayer();

    for( var l = startLoop;l < endLoop+1; l++){

        while( !isValidActiveLayer( l ) ) {l++;}

        if( getLayerNameByIndex( l ).match(re) != null){

           

            allLayers.push( l );

            layerNames.push(l);

            }

       }

    return allLayers;

    return layerNames;

}; 

function getActiveLayerIndex() { var ref = new ActionReference(); ref.putProperty( 1349677170 , 1232366921 );  ref.putEnumerated( 1283027488, 1332896878, 1416783732 );  var res = executeActionGet(ref).getInteger( 1232366921 )  - Number( hasBackground() );   res == 4 ? res++:res ;    return res;     } 

function isValidActiveLayer( idx ) {var propName = stringIDToTypeID( 'layerSection' );var ref = new ActionReference();ref.putProperty( 1349677170 , propName);ref.putIndex( 1283027488, idx );var desc =  executeActionGet( ref );var type = desc.getEnumerationValue( propName );       var res = typeIDToStringID( type );        return res == 'layerSectionEnd' ? false:true;  }; 

function hasBackground(){var res = undefined;try{var ref = new ActionReference();ref.putProperty( 1349677170 , 1315774496);ref.putIndex( 1283027488, 0 );executeActionGet(ref).getString(1315774496 );    res = true;     }catch(e){ res = false}      return res;  }; 

function makeActiveByIndex( idx, forceVisible ){try{var desc = new ActionDescriptor();var ref = new ActionReference();ref.putIndex(charIDToTypeID( "Lyr " ), idx)  ;desc.putReference( charIDToTypeID( "null" ), ref ); desc.putEnumerated(sTID("selectionModifier"), sTID("selectionModifierType"), sTID("addToSelection"));

desc.putBoolean( charIDToTypeID( "MkVs" ), forceVisible ); executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );}catch(e){ return -1;}  }; 

function getNumberOfLayer(){var ref = new ActionReference();ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );var desc = executeActionGet(ref);var numberOfLayer = desc.getInteger(charIDToTypeID("NmbL"));return numberOfLayer;}; 

function getLayerNameByIndex( idx ) {var ref = new ActionReference();ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " ));ref.putIndex( charIDToTypeID( "Lyr " ), idx );return executeActionGet(ref).getString(charIDToTypeID( "Nm  " ));;}; 

function placeEmbeded(filePath) {var actDesc = new ActionDescriptor(); actDesc.putPath(charIDToTypeID('null'), filePath);actDesc.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa'));executeAction(charIDToTypeID('Plc '), actDesc, DialogModes.NO); } 

function selectlayer(enabled, withDialog,layername) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putName(cTID('Lyr '), layername);

    desc1.putReference(cTID('null'), ref1);

    desc1.putBoolean(cTID('MkVs'), false);

    var list1 = new ActionList();

    list1.putInteger(70);

    desc1.putList(cTID('LyrI'), list1);

    executeAction(cTID('slct'), desc1, dialogMode);

  };

// Replace Contents

function replacecontents(enabled, withDialog,filename) {

     if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    desc1.putPath(cTID('null'), new File(filename));

    executeAction(sTID('placedLayerReplaceContents'), desc1, dialogMode);

  };

main();

portrait2.jpg

Votes

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 ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

Once again every smart object layers has an associated transforms which is not replace or updated when you replace a smart object layer object content.  That transform may scale and deform the object.  You can only replace an object with the same size object. If the replacement object size is different the associated transform will not work correctly.  You can not use different size images.  

That is the main reason I use Alpha channels to map Collage image location size and shape.   My Scripts can not automate image distortion rotation etc.  But they can scale  position and shaped the places smart object images layer using the required Alpha Channels.

JJMack

Votes

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
Participant ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

I got your point but the thing is I prepare these smart objects from shape layers at the beginning. Then I add layer mask from its transparency channel. So it preserves its initial size. Then I remove link between mask and smart object. And after applying replacement new photo comes with its original size but mask stays still of course. Afterwards user may drag and position image but overall composition would not affect.  Of course if replaced image size is smaller then it will come small:) I don't claim anything else. Your example shows proportional difference problem but since we'll have layer masks it won't show up. (Of course if anyone helps with proportional scaling thing.)

Here is what I mean;

sample.jpg

Votes

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 ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

LATEST

All your  unlinked layer mask does is confine where pixel can show on the layer.  Protect your templates white space like I wrote. Your replacement  image will  still be transformed by the smart object layer's  associated transform.  Even manually using Transform  to resize and position the image may not work well if the aspect ratio are very different you may not come up with an acceptable image composition.

If yow want to be able to use any size image and have more then 53 Images  you could use  Shape layers to map your images location size and shape.  Use the Shape layer transparency  Slections  get its bounds and resize the placed images to fiil thats ares. Position that imahe layer to that area and mask the layer to the selection shape like I use my Alpha channels.

Capture.jpg

JJMack

Votes

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