Save for web at 100K size

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

tawnee24

Save for web at 100K size

Post by tawnee24 »

Wondering if it's possible to write a script that will save for web as jpeg, optimized, with color profile embedded, and with the quality set for each individual image at whatever level is necessary for each file to be 100K in size. Currently every image I blog has to be sfw individually because I need them to be 100K in size, and with varying data in each image, it's not possible to input the same quality level for each.

I'd like the sfw files to be placed in a subfolder from the source folder labeled "blog" which I would create ahead of time. The script would used in an action that would resize the images before running the script (to account for landscape/portrait files both). I'd like the file names to be the same as the original.

Is it possible? Thanks in advance for any help!

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

txuku

Save for web at 100K size

Post by txuku »

Hello tawnee24

You can try this but the image should not be too large :

Code: Select all//SauveWebJPG_Poids.jsx
//Sauve l image a un poids inferieur a 100 ko

var docRef = activeDocument;
var outputFolder = docRef.path;
var blog = new Folder(outputFolder  +"/blog");
blog.create();
var outputFolder= new Folder(outputFolder +"/blog");

Nom = docRef.name;
NamesaveRef = new File( outputFolder + "/" + Nom );
var NewfileRef = new File( NamesaveRef )

// Test du POIDS
var poidsMax =  100000; // pour 100 ko maximum
var qualit = 100;
var x = 1;

EnregWeb(NewfileRef, qualit);
   
while (NewfileRef.length > poidsMax )
{
      qualit = qualit - x;
      NewfileRef = new File( NewfileRef );   
      NewfileRef.remove();
      EnregWeb(NewfileRef, qualit);
         if (qualit<=0)
         {
        alert("La taille de l'image est telle qu'elle ne peut être sauvegardée avec un poids de 100 ko.");
         break         
         }
}

var poidsFichier = NewfileRef.length /1000;
poidsFichier = Math.round(poidsFichier);

// fermer l original sans enregistrer
activeDocument.close(SaveOptions.DONOTSAVECHANGES);


function EnregWeb(filename, qualit)
{
      var options = new ExportOptionsSaveForWeb();
      options.quality = qualit;   // Start with highest quality (biggest file).
      options.format = SaveDocumentType.JPEG;   // Or it'll default to GIF.
      docRef.exportDocument(File( filename), ExportType.SAVEFORWEB, options);
}

tawnee24

Save for web at 100K size

Post by tawnee24 »

Thanks for the reply txuku. That script isn't doing the trick, though -- I need the size to be 100k, not the quality.... The quality can be whatever it needs to be to set the file size to 100k. Any other ideas out there?
xbytor

Save for web at 100K size

Post by xbytor »

Any other ideas out there?

The only way to do it is save at a quality level, lets say 10. Check the size of the file. If it's too big, save the file at quality level 9. Repeat until done.
Mike Hale

Save for web at 100K size

Post by Mike Hale »

X, I seem to remember you giving this a try some time back and also had trouble getting it to work correctly. Something about the file size getting cached so the script doesn't always get the correct size of the saved file when saving over and over with the same name.
xbytor

Save for web at 100K size

Post by xbytor »

That does sound familiar.
You would probably have to save to a unique filename each time using a new File object each time to get around any caching problems.
When you get to the desired size, do a copy or rename. Regardless, the performance is going to be unpleasant.
Mike Hale

Save for web at 100K size

Post by Mike Hale »

Just so tawnee24 is clear, the 'Optimize to File Size...' sub menu in the save for web dialog is not scriptable.

As X said, a script would have to keep saving the image with different quality settings until it finds the setting that comes close to the desired file size without going over. That looks like what txuku's script is doing but it may have the cache problem. And as X said it will be slow.

It may be faster to tweak the way the quality is chosen that doing a simple loop. For example start with a quality setting of 50. If the size is too big try 25 next. If too small try 75. Then the next loop takes have of those values and continues until it reaches the desired size.