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);
}
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
...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.
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)
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
Copy link to clipboard
Copied
Have you looked at Image Processor Pro to see if it would do what you need?
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
Copy link to clipboard
Copied
Hi. Try setting the .js format. And try again