Skip to main content
Participating Frequently
July 10, 2023
Question

image processing 100+ images to be below 600KB but above 500KB

  • July 10, 2023
  • 4 replies
  • 412 views

Is there away to creat an action or skript that will make all my images to below 600 KB but above 500KB? I am currently image processing the ones that are too large to image quility 11, then the ones that are still too large to image quality 10 then 9 then 8... until all are below the right size. I cannot just process all at say at image Quality 7 because then some would be too low. 

4 replies

Abambo
Community Expert
Community Expert
July 10, 2023

In Camera Raw (and in Lightroom), there is for saving (export) a target size with JPEG, that you can give. All files are below that target size, with the best quality possible.

 


You can't set the “above” parameter because that one does not make sense here.

ABAMBO | Hard- and Software Engineer | Photographer
Stephen Marsh
Community Expert
Community Expert
July 10, 2023

@Abambo – thanks for the reminder of the ACR batch save option! Save for Web has such an option, but it can't be batched or scripted.

Stephen Marsh
Community Expert
Community Expert
July 10, 2023

@Simon.25877479lpt6 

 

 

EDIT: Link removed as it is now invalid

 
mindfulretouch.com/the-course/saving/saving-quality
 
Here is the code:

/*
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
 
D Fosse
Community Expert
Community Expert
July 10, 2023

Just another way to look at this: Jpeg file size depends to a high degree on image content. Flat, smooth areas compress much more efficiently than busy high frequency detail.

 

The point is - it will all average out in the end. Let the small remain small and the big remain big. The detailed images might be where you need good quality.

 

These two are same pixel size and same quality level. The first is 47 kB. The second is 376 kB.

c.pfaffenbichler
Community Expert
Community Expert
July 10, 2023

Are you familiar with JavaScript and Photoshop’s DOM? 

Have you done a Forum search? Because the issue of degrading an image until it meets a size requirement has come up previously. 

Participating Frequently
July 10, 2023

Hi

 

I know some Javascript, What is Photoshop's DOM?  

 

Cheers

c.pfaffenbichler
Community Expert
Community Expert
July 10, 2023

Document Object Model; the more readable way to direct Photoshop in Scripting; Action Manager- aka AM-code is less readable but often performs faster.