• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Slow operation of the script

Participant ,
Jan 25, 2023 Jan 25, 2023

Copy link to clipboard

Copied

Hi. Once found a script and modified it to suit my needs. It works fine, but it's slow.
I use it for batch processing.
When I manually save for web, I don't notice such delays. Maybe the script itself is a bit crooked?

The goal is to save a file with an existing color profile, jpg, up to 500kb, in the same folder, with the same name.
The file height and width are already correct before saving, the script should only save with the requirements above.

(Analogous to the "save for web" function, which the action doesn't see)

var docRef = activeDocument;
var outputFolder = docRef.path;

var currentNameWithoutExtension = docRef.name.split(".")[0];
NamesaveRef = new File( outputFolder + "/" + currentNameWithoutExtension + ".jpg" );
var NewfileRef = new File( NamesaveRef )

// quality/size constraints
var MaxSz =  500000; // max. 500Kb
var Qlt = 100; // initial quality 100
var x = 1; // decreasing step

// Perform the first SaveForWeb Operation
ExpWeb(NewfileRef, Qlt);

// Keep trying to save the file with max. Qlt, but under MaxSz    
while (NewfileRef.length > MaxSz)
{
      Qlt = Qlt - x;
      NewfileRef = new File( NewfileRef );  
      NewfileRef.remove();
      ExpWeb(NewfileRef, Qlt);  // Perform a new SaveForWeb Operation, with slightly lower Qlt
         if (Qlt <= 10) {
           alert("The file can't be saved with the desired size AND quality.");
           break  // break the loop whenever the quality is as low as 50 (this shouldn't need to happen)
         }
}

var FileSz = NewfileRef.length/1024;
FileSz = Math.round(FileSz);

// SaveForWeb Export, with the desired constraints and parameters
function ExpWeb(FileNm, Qlt)
{
      var options = new ExportOptionsSaveForWeb();
      options.includeProfile = true;
      options.quality = Qlt;   // Start with highest quality (biggest file).
      options.format = SaveDocumentType.JPEG;   // Save Format for the file
      docRef.exportDocument(File(FileNm), ExportType.SAVEFORWEB, options);
}
TOPICS
Actions and scripting

Views

514

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 25, 2023 Jan 25, 2023

It would appear to be reasonable to expect that the script is slow, it has to save, then check the file size on disk, and if the file size isn't to the target, delete the file and repeat again with a 1% lesser value, until it is below the target size on disk.

 

Starting at quality 100 most likely isn't helping. Find the quality value that produces a reasonable looking result. This could be 90 or some other start point value below 100. I understand that 100 is ideal, however, when you compare a l

...

Votes

Translate

Translate
Adobe
Community Expert ,
Jan 25, 2023 Jan 25, 2023

Copy link to clipboard

Copied

It would appear to be reasonable to expect that the script is slow, it has to save, then check the file size on disk, and if the file size isn't to the target, delete the file and repeat again with a 1% lesser value, until it is below the target size on disk.

 

Starting at quality 100 most likely isn't helping. Find the quality value that produces a reasonable looking result. This could be 90 or some other start point value below 100. I understand that 100 is ideal, however, when you compare a lesser value you may find that the visual quality is comparable, however, this may save multiple iterations of the script needlessly running until it produces the desired outcome. Another option would be to increase the decreasing quality step from 1 to 5.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

Indeed) I reduced the initial quality and increased the number of steps, then the script became super fast, without visual changes to the picture.

Thanks for the tip)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

You're welcome, thanks for coming back to update the topic with a reply and correct answer.

 

Edit: While on this topic...

 

/*
https://mindfulretouch.com/the-course/saving/saving-quality/
*/

var doc = app.activeDocument;
var filename = doc.name;
var foldername = doc.path.name;
var filepath = doc.path;
var parentfilepath = doc.path.parent
var jpegQuality = 70; // desired quality
var maxfilesize = 512000; // max size in bytes
var dec = 5; // decreasing step
var minimumqual = 50; //minumum desired quality
savefolder = new Folder(filepath + '/BATCH/');
savefolder.create();
saveFile = new File(savefolder + '/' + filename.split('.').slice(0, -1).join('.') + ".jpg");
doc.flatten();
doc.pathItems.removeAll();
doc.channels.removeAll();

function savebatchweb(saveFile, jpegQuality) {
    var sfwOptions = new ExportOptionsSaveForWeb();
    sfwOptions.format = SaveDocumentType.JPEG;
    sfwOptions.includeProfile = true;
    sfwOptions.interlaced = 0;
    sfwOptions.optimized = true;
    sfwOptions.quality = jpegQuality;
    doc.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
}
savebatchweb(saveFile, jpegQuality);
while (saveFile.length > maxfilesize) {
    jpegQuality = jpegQuality – dec;
    saveFile = new File(saveFile);
    saveFile.remove();
    savebatchweb(saveFile, jpegQuality);
    if (jpegQuality <= minimumqual) {
        break
    }
}
doc.close(SaveOptions.DONOTSAVECHANGES); //closes file

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 26, 2023 Jan 26, 2023

Copy link to clipboard

Copied

Have you looked at Image Processor Pro to see if it would do what you need?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 11, 2023 Jul 11, 2023

Copy link to clipboard

Copied

Hi i copied the above code into notepad and then saved as a .jsx then loaded it into a photoshop action but nothing seems to be happening when i play the action? i am i missing a step? 

 

Regards

 

Simon

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 11, 2023 Jul 11, 2023

Copy link to clipboard

Copied

LATEST

Hi. Try setting the .js format. And try again

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines