5

I need to do the following thing. Having multiple png files in folder with a naming convention set: 1.png 1_m.png, 2.png 2_m.png (and so on). The png files have the same width and height (320 x 360 px).

Now the script should do the following:

take the files 1.png 1_m.png and create a new file in which the 1_m.png is placed on the left and 1.png on the right merge those two on one layer and save it as lets say 1_done.png, run this action on all files in the folder.

This doesn't have to be a Photoshop script I've searched the web but couldn't find any useful solution. Also nothing is set in stone here, the files could be in different folders what ever would be the simplest solution. My Photoshop version is CS5

adam
  • 618
  • 2
  • 11
  • 31

3 Answers3

3

This script will do what you want. Put all the files in a directory and then run the script using automate batch -> script. It'll find an image without and underscore in the the file-name and then open it's paired name file (with "_m"), put them side by side and save it out with _done added on to the file-name.

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

var srcDoc = app.activeDocument;

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

// set original width and height
var imageW = srcDoc.width.value;
var imageH = srcDoc.height.value;

// get the info out of the source doc
var fileName = srcDoc.name;
var docName = fileName.substring(0,fileName.length -4);
var filePath = srcDoc.path.toString();
var fileExt = fileName.substring(fileName.length -4, fileName.length);

var nameCheck = fileName.substring(0,fileName.indexOf("_"));

if (nameCheck <1)
{
   // no underscore so we need to open it's namesake
   // alert(nameCheck)
   var filePair = filePath + "/" + docName + "_m" + fileExt;
   openThisFile(filePair)
   activeDocument.selection.selectAll()
   activeDocument.selection.copy();
   app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
   app.activeDocument = srcDoc;
   activeDocument.resizeCanvas(imageW *2, imageH, AnchorPosition.MIDDLELEFT);
   selectRect(0, imageW, imageW*2, imageH)
   activeDocument.paste()
   activeDocument.flatten();
   var newName = filePath + "/" + docName + "_done" + fileExt
   saveMe(newName)
}
    else
    {
      app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    }


function openThisFile(masterFileNameAndPath)
{
 var fileRef = new File(masterFileNameAndPath)
 if (fileRef.exists)
 //open that doc
 {
    app.open(fileRef);
 }
 else
 {
    alert("error opening " + masterFileNameAndPath)
 }
}


function selectRect(top, left, right, bottom)
{
    srcDoc.selection.deselect()
    // =======================================================
    var id1 = charIDToTypeID( "setd" );
    var desc1 = new ActionDescriptor();
    var id2 = charIDToTypeID( "null" );
    var ref1 = new ActionReference();
    var id3 = charIDToTypeID( "Chnl" );
    var id4 = charIDToTypeID( "fsel" );
    ref1.putProperty( id3, id4 );
    desc1.putReference( id2, ref1 );
    var id5 = charIDToTypeID( "T   " );
    var desc2 = new ActionDescriptor();
    var id6 = charIDToTypeID( "Top " );
    var id7 = charIDToTypeID( "#Pxl" );
    desc2.putUnitDouble( id6, id7, top );
    var id8 = charIDToTypeID( "Left" );
    var id9 = charIDToTypeID( "#Pxl" );
    desc2.putUnitDouble( id8, id9, left );
    var id10 = charIDToTypeID( "Btom" );
    var id11 = charIDToTypeID( "#Pxl" );
    desc2.putUnitDouble( id10, id11, bottom );
    var id12 = charIDToTypeID( "Rght" );
    var id13 = charIDToTypeID( "#Pxl" );
    desc2.putUnitDouble( id12, id13, right );
    var id16 = charIDToTypeID( "Rctn" );
    desc1.putObject( id5, id16, desc2 );
    executeAction( id1, desc1, DialogModes.NO );
}

function saveMe(fPath)
{

// save out the image
var pngFile = new File(fPath);
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 saved png
 app.activeDocument.close()
}
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125
  • Thanks for the script. It works perfect. I just cant find the batch -> script command Is it only in the extended version of Photoshop? – adam Nov 23 '12 at 17:32
  • Firstly record the script as an action. Action--> New Action (give it a name), record and then STOP recording - that's often important. and then call the script from the batch file. I'm not sure about CS5 - but certainly under CS2 you will find it under File--> Automate--> Batch, then choose the folder that it's in. Hit Ok to run it. – Ghoul Fool Nov 26 '12 at 09:22
  • I'm trying to use this script but with jpg images. I've change pngSaveOptions to JPG and removed the .matte but when I try to run it places my image and on the right just a blank rectangle. I have the files in the same folder and one is line name.jpg and the other is name_m.jpg.. Can anyone help me identifying what am I doing wrong ? Thanks – user1782638 Dec 03 '13 at 15:40
  • Just found the problem... Somehow the activeDocument.flatten(); is causing the images to "realign"... I've just commented out the line and all works fine now! Thanks for the great script! – user1782638 Dec 03 '13 at 16:43
  • I am using Photoshop CS 3 Extended. Cannot find the option batch -> script commands. can you let me know which version are you using. – Ashok Shah May 25 '16 at 15:25
2

I would do this with ImageMagick which is available for free for Windows, OSX and Linux - in fact it is installed on most Linux distros anyway.

The crux of it is to use ImageMagick's convert command to append two images side by side, basically like this:

convert left.png right.png +append out.png

So the script looks like this - (nearly half of it is comments):

#!/bin/bash
for i in [0-9]*_m.png; do
    # Deduce name of left image
    left="$i"
    # Deduce name of right image
    right="${i/_m/}"
    # Deduce name of output image
    done="${i/_m/_done}"
    # Merge the little devils
    convert "$left" "$right" +append "$done"
done

If we start with images like this:

enter image description here

and

enter image description here

It will generate this for all pairs:

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

I just got done doing the same thing as you and did and posted my results here.

Before you choose an option, I will say that if you have a couple hundred files or more, it will be really important to evaluate how much RAM and open disk space you have. Because Option 2 requires having all the combined images opened at once in photoshop this isn't recommended for large data sets. I quickly filled 20GB of scratch before finishing 50 files. The resolution of the combined file was 3300 x 2300px.

For your situation, your files are already setup to do option 2. Be sure that you have leading 00s for your file names and no other files are in that folder. I had 50 files so I only had to do one leading 0, but if not, you get 1.jpg lining up with 10.jpg, 2.jpg with 20.jpg, etc.

After opening the Contact Sheet Dialogue, make sure under Document you change units to pixels and change the width to 2x your original file width.

Thumbnails Settings: Place Across, Columns 2, Rows 1

Uncheck Use Filename for Caption

After Contacts Sheet is done, Photoshop will have each one of these open as a separate active document. This is where Option 1 might be a better options if you have too many files. Anyway, with all the contact sheets opened in photoshop, goto File > Automate > Batch and select "Use open images" for the source. For keeping them as a png, you'll have to record an action that saves an open document as a PNG AND closes that file. See here and see the first comment by spentak.