5

I've made a script which is being used in HTML panel for saving files as PNG or JPG based on selected option! below is my code.

function saveMain() {

                var finalName = userName.replace(/ /g, "_").toLowerCase();

                if (ext === ".png") {
                    var saveFile = new File(filePathFolder + "/" + finalName + ext);

                    pngSaveOptions = new PNGSaveOptions();

                    pngSaveOptions.interlaced = false;

                    app.activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
                } else {
                    var saveFile = new File(filePathFolder + "/" + finalName + ext);

                    var saveOptions = new JPEGSaveOptions();
                    saveOptions.embedColorProfile = true;
                    saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
                    saveOptions.matte = MatteType.NONE;
                    saveOptions.quality = 10;

                    app.activeDocument.saveAs(saveFile, saveOptions, true, Extension.LOWERCASE);
                }

            }

The Problem is my exported PNG side is way too big eg. 5.88 MB and the same file generated from save for web is only 38 KB! what am I missing in configuration! Thanks!

enter image description here

Edit : Problem is fixed by using ExportOptionsSaveForWeb instead of saveAs. but as Billy kerr pointed out even saving manually as Save As doesn't output such a big file so what is going on! use below code which trigerring sized PNG!

var saveFile = new File("~/Desktop/test.png");
pngSaveOptions = new PNGSaveOptions();
pngSaveOptions.interlaced = false;
app.activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
10
  • 1
    Did you rescale the image when using save for web? Check the dimensions of these images. Also, are they both the same type of PNG. i.e. PNG8 or PNG24?.
    – Billy Kerr
    Jul 29, 2019 at 7:42
  • 1
    You are not doing a web png. You are doing the same thig as saving from the normal save dialog which is a png meant for print. Full size, full color and with all metadata embedded.
    – joojaa
    Jul 29, 2019 at 7:59
  • Well i didn't rescaled but @joojaa seems right. so how am I supposed to export as save for web . seems should use ExportOptionsSaveForWeb instead of PNGSaveOptions!
    – Mr.Online
    Jul 29, 2019 at 8:06
  • @joojaa can you add your comment as answer so that i can accept it!
    – Mr.Online
    Jul 29, 2019 at 8:19
  • @DesignPhoenix - perhaps convert it to Indexed colour mode (i.e. PNG8), before you save the PNG. Also I really suspect there has been some scaling too. You need to check if the PNGs are the same dimensions and type of PNG. Open them up in PS, and compare the image sizes, and the colour modes. If you see any differences there, then that explains the huge file size differences.
    – Billy Kerr
    Jul 29, 2019 at 8:29

2 Answers 2

3

I managed to get Photoshop to save an adequately compressed PNG from a script. Using the ScriptingListener plugin. I just copied and pasted the code from the ScriptingListener.log. Obviously the save path is for my Desktop, but I'm sure you'll be able to fix that.

var idsave = charIDToTypeID( "save" );
    var desc8 = new ActionDescriptor();
    var idAs = charIDToTypeID( "As  " );
        var desc9 = new ActionDescriptor();
        var idMthd = charIDToTypeID( "Mthd" );
        var idPNGMethod = stringIDToTypeID( "PNGMethod" );
        var idquick = stringIDToTypeID( "quick" );
        desc9.putEnumerated( idMthd, idPNGMethod, idquick );
        var idPGIT = charIDToTypeID( "PGIT" );
        var idPGIT = charIDToTypeID( "PGIT" );
        var idPGIN = charIDToTypeID( "PGIN" );
        desc9.putEnumerated( idPGIT, idPGIT, idPGIN );
        var idPNGf = charIDToTypeID( "PNGf" );
        var idPNGf = charIDToTypeID( "PNGf" );
        var idPGAd = charIDToTypeID( "PGAd" );
        desc9.putEnumerated( idPNGf, idPNGf, idPGAd );
        var idCmpr = charIDToTypeID( "Cmpr" );
        desc9.putInteger( idCmpr, 6 );
    var idPNGF = charIDToTypeID( "PNGF" );
    desc8.putObject( idAs, idPNGF, desc9 );
    var idIn = charIDToTypeID( "In  " );
    desc8.putPath( idIn, new File( "C:\\Users\\Billy\\Desktop\\testimage.png" ) );
    var idDocI = charIDToTypeID( "DocI" );
    desc8.putInteger( idDocI, 195 );
    var idCpy = charIDToTypeID( "Cpy " );
    desc8.putBoolean( idCpy, true );
    var idsaveStage = stringIDToTypeID( "saveStage" );
    var idsaveStageType = stringIDToTypeID( "saveStageType" );
    var idsaveBegin = stringIDToTypeID( "saveBegin" );
    desc8.putEnumerated( idsaveStage, idsaveStageType, idsaveBegin );
executeAction( idsave, desc8, DialogModes.NO );

testimage.png is the output file

My comparison here shows the output file size compared to Save As from within Photoshop GUI, and Save for Web (Legacy)

enter image description here

4
  • 1
    By the way, this is the first time I have ever tried anything like this. Perhaps I ought to look into this scripting lark. So anyway, big LOL's if I got anything wrong. That does look like a hell of a lot of code!!!
    – Billy Kerr
    Jul 29, 2019 at 13:58
  • 1
    Yeah. Scriptlistener plugin is best start.btw you can you adobe extendscript toolkit to directly run your script without saving it or you can use vs code plugin too! And well this is same as we do it manually so for sure it won't have that big size issue and welcome to scripting world lol! Btw as of that adobe forum "PngMethod" is one which is related for compression! Btw +1 for your try and please don't leave scripting world😂 And instead of that long desktop path u can use "˜/Desktop" Because username varies as per pc
    – Mr.Online
    Jul 29, 2019 at 14:02
  • 1
    Fyi, if you install SuperpPNG plugin, (fnordware.com/superpng) you'll be able to save quantized pngs with scripting: optimized pngs with 8bit alpha (similar to what tinypng.com does) Jul 29, 2019 at 14:53
  • yeah i used it once! it uses PNG-Quant technology to compress pixel data :D it saves upto 70% size :D but tinyPNG is one step ahead of PNG-Quant; i just couldn't found solution like tinyPNG xD
    – Mr.Online
    Jul 30, 2019 at 8:53
2

It's because my function just FORCED photoshop to export PNG without compression

So after a long of discussion, chatting and research (Thanks @Billy Kerr; you can see our chat from comment section) we finally figured out what the CAUSE is!

so basically in all type of export or save as photoshop uses some sort of algorithms and it converts/optimizes as per users input but somehow by my code (obviously it's script of photoshop so no wonder) photoshop was exporting all PNG as the flattened version of my document in printing PNG of Adobe Photoshop's format so it was maximum possible quality of my image that's why image size was going too heavy as much as 8 MB while save for web was exporting the image with same quality around 300 KB size :)

TLDR: Photoshop got dark secrets lol and it's always amusing to mess with one of them so the solution was instead of saving it as Photoshop's PNG I had to go with SaveForWeb Options and thanks @joojaa for suggesting that! all credits of solution goes to joojaa! Cheers :)

4
  • I wonder if it has anything to do with this "bug". forums.adobe.com/thread/2573336
    – Billy Kerr
    Jul 29, 2019 at 12:59
  • Yeah maybe because i didn't specified Compression Ratio! Will dig im tomorrow since already out of office :P
    – Mr.Online
    Jul 29, 2019 at 13:24
  • 1
    I've now added an answer that seems to fix your problem. Take a look.
    – Billy Kerr
    Jul 29, 2019 at 14:00
  • 1
    Looked already :D
    – Mr.Online
    Jul 29, 2019 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.