Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Custom icon saving script is exporting PNGs at HUGE sizes?

Explorer ,
Mar 11, 2019 Mar 11, 2019

Hello, I have a script which saves icons out at various sizes, the problem being is that there is odd behavior exhibited, such as:

A 22px by 22px png at 72dpi with a file size of 12.5Mb

Clearly there is some metadata or some such which must account for the inflated size. There just is not enough "physical" data there to generate that size. The other explanation is that my script is just all jacked up. If anyone could take a look and maybe see where I have gone wrong, I would appreciate it.

Entire .jsx:

#target 'photoshop'

//Array of desired resolutions

var resolutions = [1024, 912, 512, 152, 120, 114, 80, 76, 58, 50, 44, 40, 29, 25, 22];

//Variable reference to the current opened photoshop file

var doc = app.activeDocument;

var docPath = File(app.activeDocument.path + '/' + app.activeDocument.name);

//Create the GUI window

var appWindow = new Window('dialog', 'Icon Processor (ver. 1.0)');

//Styling the window

//Size (must use an window.onShow(){}; callback

appWindow.onShow = function(){appWindow.size = 'width: 230, height: 300';}

//The Create Directory input

var createDir = appWindow.add('statictext',undefined,'Create Folder Name:');

//Input textfield

var dirNameInpt = appWindow.add('edittext',undefined,undefined);

dirNameInpt.characters = 20;

dirNameInpt.active = true;

//var dirName = dirNameInpt.text;

//The Create Directory input

var createFile = appWindow.add('statictext',undefined,'Enter File Name:');

//Input textfield

var fNameInpt = appWindow.add('edittext',undefined,undefined);

fNameInpt.characters = 20;

//var fileName = fNameInpt.text;

//The execute button

var execBtn = appWindow.add('button',undefined,'Execute');

execBtn.onClick = function()

{

      

//Loop through the array of resolutions

for(var i=0; i<resolutions.length; i++)

{

//Resize the image by each resolution in the array set dpi to 72

doc.resizeImage(UnitValue(resolutions, 'px'), UnitValue(resolutions, 'px'), 72, ResampleMethod.BICUBIC, 100);

           

//Call the export PNG function for each resolution pass in the doc and name of the file as parameters

exPNG24(fNameInpt.text, dirNameInpt.text, resolutions);

           

            closeFile();

            openFile();

}

       

        //Close the application window

        appWindow.close();

       

};

//Close button

    var closeBtn = appWindow.add('button',undefined,'Cancel');

    closeBtn.onClick = function()

    {

        appWindow.close();

    };

//Display the window

appWindow.show();

function closeFile()

{

    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

}

function openFile()

{

    app.open(docPath);

    doc = app.activeDocument;

}

function exPNG24(f,d,i)

    {

        var name = f;

var directory = d;

var i = i;

       

        //Write a new file appending the file name with the resolution of the image

        var folder = new Folder(doc.path + '/' + directory);

        if(!folder.exists);

        folder.create();

        var file = new File(folder + '/' + name + '_' + i + '.png');

       

        //These are the options for saving the PNGs

        var options = new PNGSaveOptions();

        options.format = SaveDocumentType.PNG;

        //Use PNG-24

        options.PNG8 = false;

        //Preserve transparency

        options.transparency = true;

        options.interlaced = false;

        options.embedColorProfile = false;

        //Maximum quality and medium compression to keep file size low

        options.quality = 100;

        options.compression = 5;

       

        //remove meta data

        doc.info = null;

        //Save the image

        doc.saveAs(file, options, true, Extension.LOWERCASE);

       // doc.exportDocument(file, ExportType.SAVEFORWEB, options);

       

    }

//This doesn't work yet but not important as the window can be manually closed by pressing 'cancel'

appWindow.close();

TOPICS
Actions and scripting
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 11, 2019 Mar 11, 2019

You would be looking for entries similar to (not the full code, just snippets):

exportOptions = new ExportOptionsSaveForWeb();

exportOptions.format = SaveDocumentType.PNG;

exportOptions.PNG8 = false; // false = PNG-24

exportOptions.transparency = true; // true = transparent

exportOptions.interlaced = false; // true = interlacing on

exportOptions.includeProfile = true; // false = don't embedd ICC profile

document.exportDocument(newFile, ExportType.SAVEFORWEB, exportOptions);

document.close(SaveOptions.DO

...
Translate
Adobe
Community Expert ,
Mar 11, 2019 Mar 11, 2019

I’m guessing that it is the following issue:

Prepression: Metadata Bloat – photoshop:DocumentAncestors

You are using “save as” for the PNG, however if you changed the code to use export/save for web such metadata would be stripped from the export (if that is the actual problem in the first place).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 11, 2019 Mar 11, 2019

Thanks, I will dig into it. I haven't heard of that before. kind of new to scripting in photoshop.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 11, 2019 Mar 11, 2019

The metadata issue is not specific to scripting.

You will see script code to remove the metadata in my link, so perhaps you could graft that onto your existing code and achieve smaller file sizes (if that is the issue)… Or just search for PNG save for web javascript code, there is heaps out there as well as being in the offical JavaScript reference. Let me know if you need a sample. I’m new to scripting too.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 11, 2019 Mar 11, 2019

Thanks! I'm scouring the scripting guide currently! Thanks for the info!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 11, 2019 Mar 11, 2019

You would be looking for entries similar to (not the full code, just snippets):

exportOptions = new ExportOptionsSaveForWeb();

exportOptions.format = SaveDocumentType.PNG;

exportOptions.PNG8 = false; // false = PNG-24

exportOptions.transparency = true; // true = transparent

exportOptions.interlaced = false; // true = interlacing on

exportOptions.includeProfile = true; // false = don't embedd ICC profile

document.exportDocument(newFile, ExportType.SAVEFORWEB, exportOptions);

document.close(SaveOptions.DONOTSAVECHANGES);

or

function getPNGOptions()

{

   

    // Create the PDFSaveOptions object to set the PDF options

    var pngExportOpts = new ExportOptionsPNG24();

       

    // Setting PNGExportOptions properties. Please see the JavaScript Reference

    // for a description of these properties.

    // Add more properties here if you like

    pngExportOpts.antiAliasing = true;

    pngExportOpts.artBoardClipping = false;

    //pngExportOpts.horizontalScale = 100.0;

    //pngExportOpts.matte = true;

    //pngExportOpts.matteColor = 0, 0, 0;

    pngExportOpts.saveAsHTML = false;

    pngExportOpts.transparency = true;

    //pngExportOpts.verticalScale = 100.0;

    return pngExportOpts;

}

or

function SaveForWeb() {

    var sfwOptions = new ExportOptionsSaveForWeb();

    sfwOptions.format = SaveDocumentType.PNG;

    sfwOptions.PNG8 = false;

    sfwOptions.interlaced = false;

    sfwOptions.transparency = true;

    sfwOptions.includeProfile = true;

    //sfwOptions.matte = true;

    //sfwOptions.matteColor = 0, 0, 0;

    activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);

   

};

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 11, 2019 Mar 11, 2019

Great thanks! That is very similar to what I have BUT this

exportOptions = new ExportOptionsSaveForWeb(); 

makes the difference. I will try it out! Thanks for all the help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 11, 2019 Mar 11, 2019

I would think all you would need to do is download and install Image process pro Photoshop Plug-in  You could save for web 10 different size png files for each of your source documents in the first action step then save 5 more in the second action step.

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 11, 2019 Mar 11, 2019

Thanks for the reply. Solving these sorts of issues is really helpful in my developer training and much more satisfying. Were I a layman I would just grab a script from somewhere. Thank you for the information though

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 11, 2019 Mar 11, 2019

Thanks a bunch that solved it! Here is the new code:

#target 'photoshop'

//Array of desired resolutions

var resolutions = [1024, 912, 512, 152, 120, 114, 80, 76, 58, 50, 44, 40, 29, 25, 22];

//Variable reference to the current opened photoshop file

var doc = app.activeDocument;

var docPath = File(app.activeDocument.path + '/' + app.activeDocument.name);

//Create the GUI window

var appWindow = new Window('dialog', 'Icon Processor (ver. 1.0)');

//Styling the window

//Size (must use an window.onShow(){}; callback

appWindow.onShow = function(){appWindow.size = 'width: 230, height: 300';}

//The Create Directory input

var createDir = appWindow.add('statictext',undefined,'Create Folder Name:');

//Input textfield

var dirNameInpt = appWindow.add('edittext',undefined,undefined);

dirNameInpt.characters = 20;

dirNameInpt.active = true;

//var dirName = dirNameInpt.text;

//The Create Directory input

var createFile = appWindow.add('statictext',undefined,'Enter File Name:');

//Input textfield

var fNameInpt = appWindow.add('edittext',undefined,undefined);

fNameInpt.characters = 20;

//var fileName = fNameInpt.text;

//The execute button

var execBtn = appWindow.add('button',undefined,'Execute');

execBtn.onClick = function()

{

      

//Loop through the array of resolutions

for(var i=0; i<resolutions.length; i++)

{

//Resize the image by each resolution in the array set dpi to 72

doc.resizeImage(UnitValue(resolutions, 'px'), UnitValue(resolutions, 'px'), 72, ResampleMethod.BICUBIC, 100);

           

//Call the export PNG function for each resolution pass in the doc and name of the file as parameters

exPNG24(fNameInpt.text, dirNameInpt.text, resolutions);

           

            closeFile();

            openFile();

}

       

        //Close the application window

        appWindow.close();

       

};

//Close button

    var closeBtn = appWindow.add('button',undefined,'Cancel');

    closeBtn.onClick = function()

    {

        appWindow.close();

    };

//Display the window

appWindow.show();

function closeFile()

{

    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

}

function openFile()

{

    app.open(docPath);

    doc = app.activeDocument;

}

function exPNG24(f,d,i)

    {

        var name = f;

var directory = d;

var i = i;

       

        //Write a new file appending the file name with the resolution of the image

        var folder = new Folder(doc.path + '/' + directory);

        if(!folder.exists);

        folder.create();

        var file = new File(folder + '/' + name + '_' + i + '.png');

       

        var options = new ExportOptionsSaveForWeb();

       

        options.format = SaveDocumentType.PNG;

        //Use PNG-24

        options.PNG8 = false;

        //Preserve transparency

        options.transparency = true;

        options.interlaced = false;

        options.embedColorProfile = false;

        //Maximum quality and medium compression to keep file size low

        options.quality = 100;

        options.compression = 5;

       

      

        //export the image

       doc.exportDocument(file, ExportType.SAVEFORWEB, options);

       

       

    }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 11, 2019 Mar 11, 2019

Glad that worked! Which would appear to point to the issue being metadata, most probably photoshop:DocumentAncestors related.

I would go back and examine all of your source files and remove this metadata using one of the methods in my blogpost.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 11, 2019 Mar 11, 2019
LATEST

12.5Mb down to 2k That is a crazy amount of meta data...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines