Freelancer

Sunday, December 8, 2013

Simple photoshop script

This is my simple script to join images using photoshop:

// create a new document size 30000x500 pixels at 72ppi
var doc = app.documents.add (30000, 500, 72);

// prepare the real width of the joined image
var wid = new UnitValue(0, "px");

// loop through all images
// 3 is the number of images
// images are store in c:\img folder
// images' file name: img1.jpg, img2.jpg, ...
for (i = 1; i <= 3; i++) {
    // take a file
    var fileRef = File("C:\\imgs\\img" + i + ".jpg");

    // open the image in a new document
    var imgdoc = app.open(fileRef);

    // resize the opened image to height 500px
    var ws = 500 * imgdoc.width / imgdoc.height;
    imgdoc.resizeImage(ws, 500);

    // copy the resized image to working document
    imgdoc.selection.selectAll();
    imgdoc.selection.copy();
    app.activeDocument = doc;
    doc.paste();

    // get the top layer
    var pslayer = doc.layers[0];

    // move the pasted image to the right position
    var position = pslayer.bounds;
    position[0] = wid - position[0];
    position[1] = 100 - position[1];
    pslayer.translate(position[0], -position[1]);

    // close the opened image and update the real width
    app.activeDocument = imgdoc;
    wid += imgdoc.width;
    imgdoc.close(SaveOptions.DONOTSAVECHANGES);
}

No comments:

Post a Comment