Skip to main content
Known Participant
June 28, 2013
Answered

Image Processing Script

  • June 28, 2013
  • 3 replies
  • 7656 views

Hi everybody,

this is what I need to do:

All image files from my input folder must be converted to JPEG, RGB, 72dpi and 234 Pixels in width or height.

Source files are TIFF, JPEG or EPS files.

I do not know much about scripting, but I searched the Internet for my purposes and built the follwing script from my search results:

// main settings

var picSourcePath = "C:\\Users\\nolte\\Desktop\\Originale\\";

var picTargetPath = "C:\\Users\\nolte\\Desktop\\Ausgang\\";

var maxSize = 234; // max width and height

//###################################################################################

// save settings and customize environment

var startRulerUnits = app.preferences.rulerUnits;

var startTypeUnits = app.preferences.typeUnits;

var startDisplayDialogs = app.displayDialogs;

app.preferences.rulerUnits = Units.PIXELS;

app.preferences.typeUnits = TypeUnits.PIXELS;

app.displayDialogs = DialogModes.NO;

//var doc = app.activeDocument;

var picFolder = Folder(picSourcePath);

var fileList = picFolder.getFiles();

if (picSourcePath != null && picTargetPath != null) {

    for (var i=0; i<fileList.length; i++) {

        if (fileList instanceof File) {

            open(fileList);

            var newFileName = fileList.name;

            app.activeDocument.changeMode(ChangeMode.RGB);

{       

                // resize

                resize(maxSize);       

                //if (confirm('Sharpen?')) {                       

                //    app.activeDocument.activeLayer.applySharpen();

                //}

                //if (confirm('Save and Close?')) {                       

                    saveWebJpg(newFileName.substring(0, newFileName.length-4) + ".jpg", picTargetPath, 100);

                    //app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                //}

            }

            // ... and close at all events

            app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

        }

        }

}

// restore settings

app.preferences.rulerUnits = startRulerUnits;

app.preferences.typeUnits = startTypeUnits;

app.displayDialogs = startDisplayDialogs;

// Funktion um Dokument als Jpeg zu speichern (Fuer Web speichern...)

// Parameter: Dateiname, Pfad mit abschliessendem '\'!, Qualitaet 1-100

function saveWebJpg(jpgName, filePath, jpgQuality ) {

var saveFile = new File(filePath + jpgName);

var webJpgOptions = new ExportOptionsSaveForWeb();

webJpgOptions.format = SaveDocumentType.JPEG;

webJpgOptions.optimized = true;

webJpgOptions.quality = jpgQuality;

activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, webJpgOptions);

File = null; ExportOptionsSaveForWeb = null;

}

function resize(size)

{

  if(app.activeDocument.width > app.activeDocument.height)

    app.activeDocument.resizeImage(size, (size * app.activeDocument.height/app.activeDocument.width),null, ResampleMethod.BICUBIC);

  else

    app.activeDocument.resizeImage((size * app.activeDocument.width/app.activeDocument.height), size, null, ResampleMethod.BICUBIC);

}

The script works fine for JPEG, TIFF and pixel EPS files. The only problem occurs with vector EPS files.

Photoshop opens vector EPS files in low resolution (72dpi), then my script exports these files with the "Save as Web JPG" settings. The output quality of the processed pictures is really bad.

I found out, that there is an "epsOpenOptions" command for Photoshop scripting. I tried to build the following code into my script in order to force Photoshop to open vector EPS files with an resolution of 300dpi. Then the rest of my script shall export these according to my specifications.

// Create a EPS option object [height & width are doc size]

var epsOpenOptions = new EPSOpenOptions

epsOpenOptions.antiAlias = true

epsOpenOptions.mode = OpenDocumentMode.RGB

epsOpenOptions.resolution = 300

epsOpenOptions.page = 3

epsOpenOptions.constrainProportions = true

However, I cannot get this to work and I don't know where I'm wrong. Can anybody help me to modify my script, so that only EPS files will be opened with these options and all other files will be opened as they are?

Thanks in advance!

This topic has been closed for replies.
Correct answer Muppet Mark

Thanks for your quick reply!

I would like to have one script only.

Maybe I got your answer wrong, but I don't think that my problem is solved.

I'm thinking of a solution, where the script detects the file type while opening. EPS files shall be opened with a resolution of 300dpi and exported according to my specifications.

All other file types shall be opened as they are and be exported as well.


OK… I had a very quick chop about with your code… Try this and see how you get on…

#target photoshop

var picSourcePath = Folder( Folder.desktop + '/Originale' );

var picTargetPath = Folder( Folder.desktop + '/Ausgang' );

var maxSize = 234; // max width and height

var startRulerUnits = app.preferences.rulerUnits;

var startTypeUnits = app.preferences.typeUnits;

var startDisplayDialogs = app.displayDialogs;

app.preferences.rulerUnits = Units.PIXELS;

app.preferences.typeUnits = TypeUnits.PIXELS;

app.displayDialogs = DialogModes.NO;

var epsOpenOptions = new EPSOpenOptions

epsOpenOptions.antiAlias = true

epsOpenOptions.mode = OpenDocumentMode.RGB

epsOpenOptions.resolution = 300

//epsOpenOptions.page = 3 EPS DOES NOT HAVE MULTI-PAGE

epsOpenOptions.constrainProportions = true

if ( picSourcePath.exists && picTargetPath.exists ) {

   

    var fileList = picSourcePath.getFiles( /\.(eps|jpg|tif)$/i );

    for( var i=0; i<fileList.length; i++ ) {

       

        if ( /\.eps$/i.test( fileList.name ) ) {

           

            open( fileList, epsOpenOptions );

        } else {

           

            open( fileList );

           

        }

        var newFileName = fileList.name;

       

        app.activeDocument.changeMode(ChangeMode.RGB);

       

        resize(maxSize);

               

        saveWebJpg(newFileName.substring(0, newFileName.length-4) + ".jpg", picTargetPath, 100);

       

        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

       

    }

}

app.preferences.rulerUnits = startRulerUnits;

app.preferences.typeUnits = startTypeUnits;

app.displayDialogs = startDisplayDialogs;

function saveWebJpg(jpgName, filePath, jpgQuality ) {

    var saveFile = new File(filePath + '/' + jpgName);

    var webJpgOptions = new ExportOptionsSaveForWeb();

    webJpgOptions.format = SaveDocumentType.JPEG;

    webJpgOptions.optimized = true

    webJpgOptions.quality = jpgQuality;

    activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, webJpgOptions);

    File = null; ExportOptionsSaveForWeb = null;

};

function resize(size) {

  if(app.activeDocument.width > app.activeDocument.height)

    app.activeDocument.resizeImage(size, (size * app.activeDocument.height/app.activeDocument.width),null, ResampleMethod.BICUBIC);

  else

    app.activeDocument.resizeImage((size * app.activeDocument.width/app.activeDocument.height), size, null, ResampleMethod.BICUBIC);

}

3 replies

Inspiring
August 1, 2017

1) There is no way to make a single call so save a jpeg file with a maximum file size. The only way to do it is to try different jpeg quality settings until you get the file size you want. Performance sucks doing this. People have posted bits of code here and maybe on ps-scripts.com that do this. This save-until-you-get-the-correct-size is not available in Image Processor or Image Processor Pro (IPP).

2) I'm the author of IPP. If you have an input file that is giving you problems, send me the original file and what the expected output file should be. I'll see what I can do about fixing the problem. In general, IPP should be able take any input file and export it to any desired file format regardless of the settings of the original or desired settings of the output file.

c_nolteAuthor
Known Participant
August 8, 2017

1) thanks for your reply. I already feared that it's not possible but still hoped that there could be a solution via scripting.

2) IPP is a really good tool. The problem I'm personally having is that - like most other tools - it opens vector eps files in 100% of their size and does not allow to set a minimum width or height before opening.

Example: I have a vector eps file with content in the dimension of 5x5 millimetres. IPP will open it in exactly that size which will lead to no good result if the output file is required larger. Due to the fact that it's vector data, it would be no problem to scale it without any limits, but there is no such feature in IPP.

The script Muppet Mark has written for me opens vector file in high quality (size and resolution) so that the source file is definetely larger than the required output file.

I hoped to find a way to include some kind of loop, so that I can work with the existing (and good working) script and just add a "save-until-you-get-the-correct-size" function.

Inspiring
August 8, 2017

I hoped to find a way to include some kind of loop, so that I can work with the existing (and good working) script and just add a "save-until-you-get-the-correct-size" function.

Set up a loop around the saveWebJpg function that changes the jpgQuality parameter until you get the correct size. I would suggest using an increment size of 5 or 10.

c_nolteAuthor
Known Participant
June 21, 2017

no one?

Tom Ruark
Inspiring
June 21, 2017

Drop the quality you pass into the save function until you get the size below what you want.

Here is a script that does something similar only it uses the built in JPEG instead of Save for Web.

// Version 2017.6.12

// Given a max size, reduce the quality of the jpeg

// until it is small enough

var maxSize = 1024 * 1024;

var smallEnough = false;

var tries = 1;

var q = 12;

var f = new File('/d/Testout/1meg.jpg');

if (f.exists)

    f.remove();

if (f.exists)

    throw('what!');

while ( ! smallEnough && q ) {

    smallEnough = SaveFile(f, maxSize, q);

    q--;

}

SaveFileAll([f.parent + "/", f.name.split(".")[0], "." + f.name.split(".")[1]]);

'DONE ' + q;

function SaveFile(inFile, inMaxSize, inQ) {

    var smallEnough = false;

    var idsave = stringIDToTypeID( "save" );

    var desc78 = new ActionDescriptor();

    var idas = stringIDToTypeID( "as" );

        var desc79 = new ActionDescriptor();

        var idextendedQuality = stringIDToTypeID( "extendedQuality" );

        desc79.putInteger( idextendedQuality, inQ );

        var idmatteColor = stringIDToTypeID( "matteColor" );

        var idmatteColor = stringIDToTypeID( "matteColor" );

        var idnone = stringIDToTypeID( "none" );

        desc79.putEnumerated( idmatteColor, idmatteColor, idnone );

        var idJPEG = stringIDToTypeID( "JPEG" );

        desc78.putObject( idas, idJPEG, desc79 );

        var idin = stringIDToTypeID( "in" );

        desc78.putPath( idin, inFile );

    var idcopy = stringIDToTypeID( "copy" );

    desc78.putBoolean( idcopy, true );

    executeAction( idsave, desc78, DialogModes.NO );

    // there is inFile.length but it is wrong on windows 7

    if ( inFile.open('r') ) {

        var a = inFile.read();

        inFile.close();

        if (a.length <= inMaxSize)

            smallEnough = true;

    }

    return smallEnough;

}

// Another version that tries to save them all, call in the same loop above

// but pass in an array of dir, filename, fileExtension

function SaveFileAll(inF) {

    for (var i = 1; i < 13; i++) {

        var idsave = stringIDToTypeID( "save" );

        var desc78 = new ActionDescriptor();

        var idas = stringIDToTypeID( "as" );

        var desc79 = new ActionDescriptor();

        var idextendedQuality = stringIDToTypeID( "extendedQuality" );

        desc79.putInteger( idextendedQuality, i );

        var idmatteColor = stringIDToTypeID( "matteColor" );

        var idmatteColor = stringIDToTypeID( "matteColor" );

        var idnone = stringIDToTypeID( "none" );

        desc79.putEnumerated( idmatteColor, idmatteColor, idnone );

        var idJPEG = stringIDToTypeID( "JPEG" );

        desc78.putObject( idas, idJPEG, desc79 );

        var idin = stringIDToTypeID( "in" );

        var f = new File( inF[0] + inF[1] + i + inF[2] );

        desc78.putPath( idin, f );

        var idcopy = stringIDToTypeID( "copy" );

        desc78.putBoolean( idcopy, true );

        executeAction( idsave, desc78, DialogModes.NO );

    }

}

c_nolteAuthor
Known Participant
June 23, 2017

Thanks for your reply!

Is it possible that this script does not work in Photoshop CS6? I always receive an error code 8800 (general photoshop error... command "save" is not available at the moment...).

Also, the script seems to be working solo only. What do I have to do to include that code in the script Muppet Mark has written for me?

Inspiring
June 28, 2013

Do all your *.eps files contain vector data or are they a mixed bag ( some photoshop + some other creators )?

c_nolteAuthor
Known Participant
June 28, 2013

The EPS files do only contain vector data.

Inspiring
June 28, 2013

OK you have a choice of having 1 script that deals with the conditions… or using 2 scripts…? An easy edit may be…?

// Will ONLY get EPS files

var fileList = picFolder.getFiles( /\.eps$/i );

// Will ONLY get JPG & TIF files

var fileList = picFolder.getFiles( /\.(jpg|tif)$/i );

// Will ONLY get EPS, JPG & TIFfiles

var fileList = picFolder.getFiles( /\.(eps|jpg|tif)$/i );