Skip to main content
Known Participant
September 16, 2018
Question

Script is Great - PNG Size is Gigantic!!!

  • September 16, 2018
  • 6 replies
  • 2582 views

Hi All,

I found this script that will save to PNG without having to go through all the step. It is from here - https://www.reddit.com/r/gamemaker/comments/346v82/photoshop_script_for_you_save_as_png/

It works perfectly.

The only issue is that the PNG file it saves is 50+MB!!! When I export PNGs they are usually about 150KB!

What in the code do I need to change?  Thanks!

c.pfaffenbichlerJJMack​ any ideas?

Here it is (sorry, don't know how to paste this in nicely):

// Exports a saved multi-layered file as a .png in the same directory.

function ExportPNG()

{

    // Confirm the document has already been saved and so has a path to use

    try

    {

        app.activeDocument.save()

    } catch(e) {

        alert("Could not export PNG as the document is not saved.\nPlease save and try again.")

        return

    }

    // Store the active doc handle in variable

    var originalDoc = app.activeDocument

   

    // Check there is at least 1 visible layer.

var foundVisible = false

    for (i = 0; i < originalDoc.layers.length; i++)

    {

        if (originalDoc.layers.visible)

        {

            foundVisible = true

break

        }

    }

if (!foundVisible){

alert("No visible layers found. PNG export failed.")

return

}

    // Duplicate. We'll save the duplicate as a .png and close it.

    var newDoc = originalDoc.duplicate()

   

    // Photoshop must have a visible layer selected to merge visible layers, so we ensure there is one selected.

    var dummyVisibleLayer = newDoc.artLayers.add();

    newDoc.activeLayer = dummyVisibleLayer

   

    // Merge the layers.

    newDoc.mergeVisibleLayers()

   

    // Remove all empty layers.

    for (i = newDoc.layers.length-1; i >=0; i--)

    {

        if (!newDoc.layers.visible)

        {

            newDoc.layers.remove()

        }

    }

   

    // Set up PNG save options.

    pngOptions = new PNGSaveOptions()

    pngOptions.compression = 0

    pngOptions.interlaced = false

   

    // Set up destination path.

    savePath = File(originalDoc.path + "/" + originalDoc.name.replace(/\.[^\.]+$/, '.png'));

   

    // Save!

    newDoc.saveAs(savePath, pngOptions, false, Extension.LOWERCASE)

   

    // Close the duplicate.

    newDoc.close()

   

    // Just in case, make sure the active document is the orignal one.

    app.activeDocument=originalDoc

}

ExportPNG()

This topic has been closed for replies.

6 replies

Stephen Marsh
Community Expert
Community Expert
September 17, 2018

I have hacked the following script to batch convert supported file types found in the first level of an input folder to transparent sRGB PNG-24 files exported using Save for Web. Output files will be in a new folder in the input folder titled “sRGB PNG Batch Output”.

With my minimal scripting knowledge, it is amazing that I am able to get this result – so test, test, test to make sure that it does what you require! Tested in CS6 and CC2018 on the Mac OS.

// Batch Export to sRGB PNG

// 2018: Another script monster hacked together from various parts by Dr Victor Frankenstein... or is that Dr Moreau?

// https://raw.githubusercontent.com/jonahvsweb/Photoshop-Automated-Resize-to-Web.jsx/master/Automated%20Resize%20To%20Web.jsx

#target photoshop

//////// https://forums.adobe.com/message/10604100#1060410  ////////////////////////////////

    displayDialogs = DialogModes.NO

///////////////////////////////////////////////////////////////////////////////////////////

    if (BridgeTalk.appName == "photoshop")

    {

        app.bringToFront;

      

        var inputFolder = Folder.selectDialog ("Select the source folder that contains the files for PNG export:");

      

        if (inputFolder != null) {

            var fileList = inputFolder.getFiles (/\.(jpg|png|tif|psd|)$/i); // line 28 currently on works correctly on 3 character filename extensions

            var outputFolder = new Folder (decodeURI (inputFolder) + "/sRGB PNG Batch Output");

          

            if (outputFolder.exists == false) outputFolder.create ();

          

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

                if (fileList instanceof File) {

                    var document = open (fileList );

                    var documentName = fileList .name.slice (0, -4); // Look into an alternative using split or a regular expression based method to strip 2, 3 or 4 filename extensions

                  

                    while (app.documents.length) {

                        var newFile = new File (decodeURI (outputFolder) + "/" + documentName + ".png");

                      

                        // document.flatten (); // Disable flatten image step

                      

///// https://github.com/LeZuse/photoshop-scripts/blob/master/default/Image%20Processor.jsx ////////////////////////////////

                        function ConvertTosRGBProfile() {

                        app.activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);

    }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                      

                        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);

                    }

                }

                if (i == fileList.length - 1) {

                    alert ("All the files have been exported.");

                }

            }

        }

    }

Instructions for saving, installing and running here:

Prepression: Downloading and Installing Adobe Scripts

P.S. On your original issue, save as PNG can include metadata, while Save for Web will strip metadata. There have been cases of excessive file bloat caused by excessive amounts of photoshop:DocumentAncestors metadata (which may or may not explain the large file sizes).

Known Participant
September 17, 2018

Thanks!

Now I can't find the scripts folder for the manual install. WHAT A PAIN!

Stephen Marsh
Community Expert
Community Expert
September 17, 2018

You really have to read the manual installation instructions for your OS with some care.

Does this help for the general location?

Adobe Photoshop Script Installation Location

Scripts are installed in the /Presets/Scripts folder

Mac OS Example:

/Applications/Adobe Photoshop CC 2018/Presets/Scripts

Win OS Example:

C:\Program Files\Adobe\Adobe Photoshop CC 2018\Presets\Scripts

Mac screenshot:

All that being said, you could always just create a batch action, however that may bring on it’s own set of challenges!

Known Participant
September 17, 2018

AUUUGGGHGGHHGHHH

I tried to install Image Processor Pro with Adobe Extension Manager CC and it WON'T work.... So frustrated.

I get this message,

I am running Photoshop CC.

Any Ideas? 

Thanks.

JJMack
Community Expert
Community Expert
September 17, 2018

Did you follow X install instructions? I always use the manual method.

JJMack
Known Participant
September 17, 2018

Thanks everyone!  I didn't even know there was an image processor PRO.

I will give it a try!

c.pfaffenbichler
Community Expert
Community Expert
September 16, 2018

This Script seems to save a plain png, have you tried a Save for Web PNG?

What are the psd file’s pixel dimensions and size (as indicated in the Image Size dialog)?

JJMack
Community Expert
Community Expert
September 16, 2018

Try changing the png save options to save a smaller file. Looking at the script I do not think it will save you any Time and may actually take longer then just doing as png. In fact it should take longer for it first saves the current document duplicates it. In the dupe it merges visible layer and deletes other layers then save out a png and closes the dupe but does not use the no save option you will most likely have to reply to a dialog that ask if you want to save the modified dupe.  I do not think much of that script

JJMack
Known Participant
September 16, 2018

Thanks!  Bummer. 

I'll keep hunting. I was looking for a script that would open the files I have that are PSD and save them as PNG.

I have a lot of them!

Stephen Marsh
Community Expert
Community Expert
September 16, 2018

That sounds like a job for the Image Processor Pro script or Picture Processor script.