Kinetic Pulse
  • Home
  • Services
  • Portfolio
  • Blog
  • Terms
  • Contact

Create a script for Photoshop to save layers as separate jpgs

May 26, 2011

Ahh creating product images for clients ecommerce sites – dontcha just love it?
Usually I insist that the client or the designer provide all product images in a uniform size with a suitable naming convention, but in my madness on this current project I said I would do the photo cropping/tweaking.
The client sends me a random set of product shots in a variety of sizes & orientations.
So in Photoshop I create a standard size PSD and drag and drop and move these motley collection of pictures around until I have a multi layer PSD with all the layers nicely named for the products they represent.
Surely there must be a “Save Layers as JPGS” option in Photoshop?
Alas no – so I had to learn how to script in Photoshop!

I found this very useful post here:
http://ask.metafilter.com/4903/Is-it-possible-to-batch-process-layers-in-Photoshop

On which I based my findings.
After installing the Scripts plugin (see previous article for link) I wrote the following javascript script to save off each layer as a separate JPG file.

try
{
var docRef = activeDocument;
var filename = docRef.name;
var nameParts = filename.split(".psd");
var nameRoot = nameParts[0];
var counter = 0;
for (var i = 0; i < docRef.layers.length; i++){
if (docRef.artLayers[i].visible){
docRef.activeLayer = docRef.layers[i]
docRef.layers[i].copy();
var docRef2 = documents.add(docRef.width,docRef.height,docRef.resolution,docRef.activeLayer.name);
docRef2.paste();
docRef2.artLayers[1].remove();

jpgFile = new File( docRef.path+"/"+docRef.activeLayer.name+".jpg" );
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = 1;
activeDocument.saveAs(jpgFile, jpgSaveOptions, true,
Extension.LOWERCASE);

saveFile = saveOptions = null;
docRef2.close(SaveOptions.DONOTSAVECHANGES);
counter++;
}
}

docRef.selection.deselect();
docRef = null
docRef2 = null

}
catch(someError)
{
alert( "JavaScript error occured. Message = " + someError );
}

I saved this as “01LayersToJPGs.js” in the Photoshop \ Presets \ Scripts subdirectory.

So now if I have my multi layer PSD open and run this script (File > Automate > Scripts) It will whirr away and save each image off as a separate jpg.
A nice unexpected touch is that it saves the image dead center within a blank white background – which certianly helped making some of the tiny images look better!
As usual I’d love your feedback on this!
(BTW I’m only using Photoshop 7 – there may well be a way of doing this automatically in later versions of Photoshop!)

Share

Facebook Google+ Twitter Pinterest Email

Back to Blog

Testimonials

Thanks so much for all your work on this, really appreciated. It’s come on in leaps and bounds since you took over.
Pete KewRedwood Strip Curtains
Thank you guys so much for all that you’ve done helping us to create a really awesome website!! We get such great feedback – everyone loves it & we couldn’t be prouder! Look forward to working with you again soon!
Wild Thyme PlantsWild Thyme Plants
Thank you for all of your hard work its looks fab and I am over the moon with it!
Amanda MercerAmanda Mercer Ceramics
It’s been fab working with you – we love the site and certainly going to recommend you!
James DaviesThirty Eight Degrees North
Amazing and brilliant, Kinetic Pulse have lifted a dream to reality, Highly recommended and great if you are total novice, they know their stuff…Thanks again
Annie LindridgeSalt Yourself Out
I like it.  I like it a lot !!!

 You have interpreted what I wanted to achieve perfectly considering what you have to play with i.e not redoing the whole thing in the process.

Fiona Simmons-MooreSouth Gloucestershire Parents & Carers
Absolutely fantastically professional web developer – I would highly recommend!!! Thank you so much Kinetic Pulse!
Tania MarstonDoris Designs

Copyright 2021 Kinetic Pulse