Predicted file size

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: xbytor, DavideBarranca, Paul MR

KOLjkA
Posts: 3
Joined: Mon May 07, 2018 7:56 pm

Predicted file size

Postby KOLjkA » Mon May 07, 2018 8:18 pm

Hi there.
How can I get predicted file size using Save As menu? I want to get it using script without saving jpg.

I wrote script for iteration of jpg quality from 12 to .... required size, and now want to optimaze it.

Can somebody help me?

ImageImage
Last edited by KOLjkA on Thu May 10, 2018 7:46 am, edited 1 time in total.

User avatar
txuku
Posts: 62
Joined: Thu Jan 01, 1970 12:00 am

Re: Predicted file size

Postby txuku » Tue May 08, 2018 5:53 am

Bonjour

I use this script:

Code: Select all

//Enregistrer sous avec un poids maximum de fichier


//****************************************************
//Tire du script : Préparation des posts pour le forum passion-photos.net par Piekes
//Adaptation du script MarcoFirma © manolowarr 2005
//****************************************************

//****************************************************
//VARIABLES A MODIFIER

// var MyFolder = "C:\\Documents and Settings\\moi\\Mes documents\\Mes images\\Z_TRAITEMENT_ENREGISTRER"

// Mettre votre nom d utilisateur a la place de " moi " qui est le mien

//" Z_TRAITEMENT_ENREGISTRER " est le repertoire que j ai cree la ou je veux retrouver les photos traitees - creez en un !

//****************************************************

// Changer l adresse de sauvetage de l image ( "C:/Documents and Settings/moi/Mes documents/Mes images/" )
//Mettez votre nom d USER a la place du mien ( " moi " )

//Creation d un dossier Z_TRAITEMENT_ENREGISTRER dans le dossier "Mes images"

myFile1 = "~/Mes documents/Mes images/";
Z_TRAITEMENT_ENREGISTRER = new Folder(myFile1 +"/Z_TRAITEMENT_ENREGISTRER");
Z_TRAITEMENT_ENREGISTRER.create();


MyFolder = new File(myFile1 + "/Z_TRAITEMENT_ENREGISTRER");




var poidsMax = 300000 // = inferieur a 300 Ko - changer suivant le besoin



//Le reste

//*******************************************************************************
var strtRulerUnits = preferences.rulerUnits
preferences.rulerUnits = Units.PIXELS
var originalDoc = activeDocument

//*******************************************************************************


var MyFile = noneExtension( originalDoc.name )


// Test du POIDS

//**************************************************************
var qualit = 12
var x = 1

var NamesaveRef = MyFolder + "/" + MyFile + "_P.jpg"
var NewfileRef = new File( NamesaveRef )
SaveForForum(NewfileRef, qualit)
while (NewfileRef.length > poidsMax )
{
qualit = qualit - x
NewfileRef = new File( NewfileRef )
NewfileRef.remove();
SaveForForum(NewfileRef, qualit)

if( NewfileRef.length < poidsMax )
{
NewfileRef = new File( NewfileRef );
SaveForForum(NewfileRef, qualit);
NewfileRef.length = poidsMax;
}


if (qualit<=0){
alert("La taille de l'image est telle qu'elle ne peut être sauvegardée avec un poids de 130 ko.")
break
}
}

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

// fermer l original sans enregistrer

activeDocument.close()



// Save JPG

function SaveForForum(filename, qualit)
{
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = qualit;
jpegOptions.embedColorProfile = false;
jpegOptions.FormatOptions = FormatOptions.PROGRESSIVE; //PROGRESSIVE; //STANDARDBASELINE; OPTIMIZEDBASELINE;
app.activeDocument.saveAs( File( filename), jpegOptions, false);
}

//*****************************************************************

function noneExtension( fileName )
{

var Point = fileName.charAt(fileName.length-4);
if ( Point == "." )
{
fileName = fileName.substring( 0, fileName.length-4);
}
return fileName;
}

with the variable poidsMax = 300ko
line 35

KOLjkA
Posts: 3
Joined: Mon May 07, 2018 7:56 pm

Re: Predicted file size

Postby KOLjkA » Thu May 10, 2018 7:50 am

Bonjour
I use this script:
Hi
This script is the same as my. We created file, write and then check file size. If size is bigger we change jpg quality.

In the Save As menu Photoshop can calculate file size without saving. Can we get this size before saving?

User avatar
txuku
Posts: 62
Joined: Thu Jan 01, 1970 12:00 am

Re: Predicted file size

Postby txuku » Thu May 10, 2018 9:18 am

Bonjour

Sorry
No other idea !!! :oops:

KOLjkA
Posts: 3
Joined: Mon May 07, 2018 7:56 pm

Re: Predicted file size

Postby KOLjkA » Thu May 31, 2018 10:36 am

My script for re-save files

Code: Select all

#target photoshop
app.bringToFront();
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var inFolder = Folder.selectDialog();
var ListFileInFolder = inFolder.getFiles("*.jpg"); // list of files as object

// order of big files
var i = 0; // for files in folder
var k = 0; // for big files for alert

// search smalest file for re-save in the end with maximum quality
var SmallFileLength = ListFileInFolder[i].length;
var SmallFileIndex = 0;

while ( ListFileInFolder[i] != undefined ) {

if ( ListFileInFolder[i].length >= 20900000) { // 20Mb*1024*1024 = 20900000b
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 11;

//second while for step save iteration
while (ListFileInFolder[i].length >= 20900000){
app.open(ListFileInFolder[i]); //open file
app.activeDocument.saveAs( File( ListFileInFolder[i] ), jpegOptions ); // save file
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); //close file
ListFileInFolder[i] = new File( ListFileInFolder[i]) //update information
jpegOptions.quality--;
}

k++
}

if (ListFileInFolder[i].length < SmallFileLength) {
SmallFileLength = ListFileInFolder[i].length;
SmallFileIndex = i;
}

i++;
};
if (k > 0) {
//if we did something - re-save smolest file with maximum quality
jpegOptions.quality = 12;
app.open(ListFileInFolder[SmallFileIndex]); //open file
app.activeDocument.saveAs( File( ListFileInFolder[SmallFileIndex] ), jpegOptions ); // save file
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
beep();

alert("Proceed " + k + " files")
} else {
alert("All files are less than 20Mb")
}


Return to “Photoshop Scripting - General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest