2
votes

I have a single photoshop file, and 200 image files (png). Using the photoshop as a pattern, I need to generate 200 new images where each image is a result of a different png placed in the photoshop pattern.

Basically, replacing an image of a layer inside photoshop with external png file I have.

Is it something that can be done automatically using a photoshop script?

2

2 Answers

1
votes

Based on the request I suggest using the Variables feature inside photoshop. Menu->Images->Variables

Then just select the layer you want to change and assign a variable name and choose "pixel replacement" behavior.

Outside Photoshop, create a text file with the variable name in the first line and the file names in new lines for each.

Go to menu-->file-->import-->variable datasets and browse for your text file.

If you see your error message then everything is correct.

Go to menu-->file-->export-->datasets to files and voila!

4
votes

Yes, with scripting, you can do this. With a source image (psd) then load each of the 200 images and place it into the source file (then do what ever you want, save out the file) Switch back to the source file and carry on looping over the images till it's all done.

// must have source psd open to start with.

//pref pixels
app.preferences.rulerUnits = Units.PIXELS;

// call the source document
var srcDoc = app.activeDocument;


var inFolder = Folder.selectDialog("Please select folder to process");
if (inFolder != null)
{
  var fileList = inFolder.getFiles(/\.(png)$/i);
}


// main loop starts here
for(var i = 0; i < fileList.length; i++)
{
  // load the frames one by one
  var doc = open(fileList[i]);

  var tempImage = app.activeDocument.name;

  //select all
  activeDocument.selection.selectAll()

  //copy image
  activeDocument.selection.copy();

  //close that document without saving
  app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

  // select the source image
  activeDocument = srcDoc;

  getMeThisLayer("my favourite layer")

  //paste
  app.activeDocument.paste();

  //deselect all
  activeDocument.selection.deselect()

  var filePath = srcDoc.path + "/" + tempImage;

  // Flatten the image
  activeDocument.flatten();

  // save out the image
  var pngFile = new File(filePath);
  pngSaveOptions = new PNGSaveOptions();
  pngSaveOptions.embedColorProfile = true;
  pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
  pngSaveOptions.matte = MatteType.NONE; pngSaveOptions.quality = 1;

  activeDocument.saveAs(pngFile, pngSaveOptions, false, Extension.LOWERCASE);

  // close that save png
  app.activeDocument.close()
}



function getMeThisLayer(aLayerName)
{
  try
  {
    // try to find the layer
    app.activeDocument.activeLayer = app.activeDocument.layers.getByName(aLayerName)
    return
  }

  catch(e)
  {
    //Whoops can't find layer
    alert("Can't find layer " + aLayerName + "\n" + e)
  }
}

Have fun.