7 Replies Latest reply on Aug 22, 2010 10:51 AM by Iain H

    Saving JPG to PNG using Save for the Web

    Iain H

      Morning

       

      I came across Paul Riggott's script for saving PSD to PNG which I changed to JPG to PSD.

       

      The only problem with this is that the file size in most cases is larger than the original.

       

      Is there a way to do a save as for the web as well?

       

      Here is the  original code:

       

      #target photoshop

      var imageFolder = Folder.selectDialog("Select top folder to process");

      var folderList=[];

      if (imageFolder != null)  {

      processFolder(imageFolder);

      folderList.unshift(imageFolder);

      createPNGfromJPG(folderList);

      }

      function createPNGfromJPG(folderList){

      for(var a in folderList){

        var fileList = folderList[a].getFiles ("*.jpg");

         for (var z in fileList){

          var file = fileList[z];

          open(file);

          var Name = fileList[z].name.replace(/\.[^\.]+$/, '');

          var saveFile = File(decodeURI(fileList[z].path+"/"+Name+".png"));

          SavePNG(saveFile);

          app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

          }

        }

      }

      function processFolder(folder) {

          var fileList = folder.getFiles()

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

              var file = fileList[i];

      if (file instanceof Folder) {

        folderList.push(file); 

             processFolder(file);

          }

         }

      }

      function SavePNG(saveFile){

          pngSaveOptions = new PNGSaveOptions();

          pngSaveOptions.embedColorProfile = true;

          pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;

          pngSaveOptions.matte = MatteType.NONE;

          pngSaveOptions.quality = 1;

      pngSaveOptions.PNG8 = false;

          pngSaveOptions.transparency = true;

      activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);

      }

      Any help or support to head me in the right direction would be appreciated.
      Iain

        • 1. Re: Saving JPG to PNG using Save for the Web
          Paul Riggott Level 6

          Please try this..

          #target photoshop
          var imageFolder = Folder.selectDialog("Select top folder to process"); 
          var folderList=[];
          if (imageFolder != null)  {
          processFolder(imageFolder);
          folderList.unshift(imageFolder);
          createPNGfromJPG(folderList);
          }
          function createPNGfromJPG(folderList){
          for(var a in folderList){
            var fileList = folderList[a].getFiles ("*.jpg");
             for (var z in fileList){
              var file = fileList[z];
              open(file);
              var Name = fileList[z].name.replace(/\.[^\.]+$/, '');
              var saveFile = File(decodeURI(fileList[z].path+"/"+Name+".png"));
              saveAsPNG24SFW(saveFile);
              app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
              }
            }
          }
          function processFolder(folder) {
              var fileList = folder.getFiles()
               for (var i = 0; i < fileList.length; i++) {
                  var file = fileList[i];
          if (file instanceof Folder) {
            folderList.push(file);  
                 processFolder(file);
              }
             }
          }
          function saveAsPNG24SFW(fileName) {
              var desc3 = new ActionDescriptor();
                  var desc4 = new ActionDescriptor();
                  desc4.putEnumerated( charIDToTypeID('Op  '), charIDToTypeID('SWOp'), charIDToTypeID('OpSa') );
                  desc4.putEnumerated( charIDToTypeID('Fmt '), charIDToTypeID('IRFm'), charIDToTypeID('PN24') );
                  desc4.putBoolean( charIDToTypeID('Intr'), false );
                  desc4.putBoolean( charIDToTypeID('Trns'), true );
                  desc4.putBoolean( charIDToTypeID('Mtt '), false );
                  desc4.putInteger( charIDToTypeID('MttR'), 255 );
                  desc4.putInteger( charIDToTypeID('MttG'), 255 );
                  desc4.putInteger( charIDToTypeID('MttB'), 255 );
                  desc4.putBoolean( charIDToTypeID('SHTM'), false );
                  desc4.putBoolean( charIDToTypeID('SImg'), true );
                  desc4.putBoolean( charIDToTypeID('SSSO'), false );
                      var list1 = new ActionList();
                  desc4.putList( charIDToTypeID('SSLt'), list1 );
                  desc4.putBoolean( charIDToTypeID('DIDr'), false );
                  desc4.putPath( charIDToTypeID('In  '), new File( fileName ) );
              desc3.putObject( charIDToTypeID('Usng'), stringIDToTypeID('SaveForWeb'), desc4 );
              executeAction( charIDToTypeID('Expr'), desc3, DialogModes.NO );
          };
          
          • 2. Re: Saving JPG to PNG using Save for the Web
            Iain H Level 1

            Hi Paul

             

            Thanks for the quick response.

             

            The files are being converted no problem, but the size of the files has not changed.

             

            I did a quick test - if I save to PNG 8 using the Save for the Web option in Photoshop, I got from 475kb to 172kb.

             

            Using the script goes from 475kb to 530kb

             

            Any other ideas?

             

            Iain

            • 3. Re: Saving JPG to PNG using Save for the Web
              Iain H Level 1

              Hi Paul

               

              Is there also a way to reduce the image dimensions as well in the script as this will also reduce the file size.

               

              Iain

              • 4. Re: Saving JPG to PNG using Save for the Web
                Paul Riggott Level 6

                If you want PNG8 this should do it...

                #target photoshop
                var imageFolder = Folder.selectDialog("Select top folder to process"); 
                var folderList=[];
                if (imageFolder != null)  {
                processFolder(imageFolder);
                folderList.unshift(imageFolder);
                createPNGfromJPG(folderList);
                }
                function createPNGfromJPG(folderList){
                for(var a in folderList){
                  var fileList = folderList[a].getFiles ("*.jpg");
                   for (var z in fileList){
                    var file = fileList[z];
                    open(file);
                    var Name = fileList[z].name.replace(/\.[^\.]+$/, '');
                    var saveFile = File(decodeURI(fileList[z].path+"/"+Name+".png"));
                    savePNG8SFW(saveFile);
                    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                    }
                  }
                }
                function processFolder(folder) {
                    var fileList = folder.getFiles()
                     for (var i = 0; i < fileList.length; i++) {
                        var file = fileList[i];
                if (file instanceof Folder) {
                  folderList.push(file);  
                       processFolder(file);
                    }
                   }
                }
                function savePNG8SFW(saveFile) {
                    var desc8 = new ActionDescriptor();
                        var desc9 = new ActionDescriptor();
                        desc9.putEnumerated( charIDToTypeID('Op  '), charIDToTypeID('SWOp'), charIDToTypeID('OpSa') );
                        desc9.putEnumerated( charIDToTypeID('Fmt '), charIDToTypeID('IRFm'), charIDToTypeID('PNG8') );
                        desc9.putBoolean( charIDToTypeID('Intr'), false );
                        desc9.putEnumerated( charIDToTypeID('RedA'), charIDToTypeID('IRRd'), charIDToTypeID('Sltv') );
                        desc9.putBoolean( charIDToTypeID('RChT'), false );
                        desc9.putBoolean( charIDToTypeID('RChV'), false );
                        desc9.putBoolean( charIDToTypeID('AuRd'), false );
                        desc9.putInteger( charIDToTypeID('NCol'), 256 ); //Number of colours.
                        desc9.putEnumerated( charIDToTypeID('Dthr'), charIDToTypeID('IRDt'), charIDToTypeID('Dfsn') );
                        desc9.putInteger( charIDToTypeID('DthA'), 100 );
                        desc9.putInteger( charIDToTypeID('DChS'), 0 );
                        desc9.putInteger( charIDToTypeID('DCUI'), 0 );
                        desc9.putBoolean( charIDToTypeID('DChT'), false );
                        desc9.putBoolean( charIDToTypeID('DChV'), false );
                        desc9.putInteger( charIDToTypeID('WebS'), 0 );
                        desc9.putEnumerated( charIDToTypeID('TDth'), charIDToTypeID('IRDt'), charIDToTypeID('None') );
                        desc9.putInteger( charIDToTypeID('TDtA'), 100 );
                        desc9.putBoolean( charIDToTypeID('Trns'), true );
                        desc9.putBoolean( charIDToTypeID('Mtt '), false );
                        desc9.putInteger( charIDToTypeID('MttR'), 255 );
                        desc9.putInteger( charIDToTypeID('MttG'), 255 );
                        desc9.putInteger( charIDToTypeID('MttB'), 255 );
                        desc9.putBoolean( charIDToTypeID('SHTM'), false );
                        desc9.putBoolean( charIDToTypeID('SImg'), true );
                        desc9.putBoolean( charIDToTypeID('SSSO'), false );
                            var list3 = new ActionList();
                        desc9.putList( charIDToTypeID('SSLt'), list3 );
                        desc9.putBoolean( charIDToTypeID('DIDr'), false );
                        desc9.putPath( charIDToTypeID('In  '),File( saveFile) );
                    desc8.putObject( charIDToTypeID('Usng'), stringIDToTypeID('SaveForWeb'), desc9 );
                    executeAction( charIDToTypeID('Expr'), desc8, DialogModes.NO );
                };
                
                • 5. Re: Saving JPG to PNG using Save for the Web
                  Iain H Level 1

                  Hi Paul

                   

                  You are a genius - the file size is now the same as my test.

                   

                  You must have been working on this during my last email to you.

                   

                  Is is possible to add in a piece that will set the size of the image to what you want it to be 640 x 480 so that I can then change in the script to suti.

                   

                  So if the original is 640 x 480 and I want 128 x 80 and PNG8, i can do that.

                   

                  Iain

                  • 6. Re: Saving JPG to PNG using Save for the Web
                    Paul Riggott Level 6

                    This uses "Fitimage"....

                    #target photoshop
                    var imageFolder = Folder.selectDialog("Select top folder to process"); 
                    var folderList=[];
                    if (imageFolder != null)  {
                    processFolder(imageFolder);
                    folderList.unshift(imageFolder);
                    createPNGfromJPG(folderList);
                    }
                    function createPNGfromJPG(folderList){
                    for(var a in folderList){
                      var fileList = folderList[a].getFiles ("*.jpg");
                       for (var z in fileList){
                        var file = fileList[z];
                        open(file);
                        var Name = fileList[z].name.replace(/\.[^\.]+$/, '');
                        var saveFile = File(decodeURI(fileList[z].path+"/"+Name+".png"));
                        FitImage(640,640); //Resizes  to the longest size 
                        //FitImage(128,128);
                        savePNG8SFW(saveFile);
                        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                        }
                      }
                    }
                    function processFolder(folder) {
                        var fileList = folder.getFiles()
                         for (var i = 0; i < fileList.length; i++) {
                            var file = fileList[i];
                    if (file instanceof Folder) {
                      folderList.push(file);  
                           processFolder(file);
                        }
                       }
                    }
                    function savePNG8SFW(saveFile) {
                        var desc8 = new ActionDescriptor();
                            var desc9 = new ActionDescriptor();
                            desc9.putEnumerated( charIDToTypeID('Op  '), charIDToTypeID('SWOp'), charIDToTypeID('OpSa') );
                            desc9.putEnumerated( charIDToTypeID('Fmt '), charIDToTypeID('IRFm'), charIDToTypeID('PNG8') );
                            desc9.putBoolean( charIDToTypeID('Intr'), false );
                            desc9.putEnumerated( charIDToTypeID('RedA'), charIDToTypeID('IRRd'), charIDToTypeID('Sltv') );
                            desc9.putBoolean( charIDToTypeID('RChT'), false );
                            desc9.putBoolean( charIDToTypeID('RChV'), false );
                            desc9.putBoolean( charIDToTypeID('AuRd'), false );
                            desc9.putInteger( charIDToTypeID('NCol'), 256 ); //Number of colours.
                            desc9.putEnumerated( charIDToTypeID('Dthr'), charIDToTypeID('IRDt'), charIDToTypeID('Dfsn') );
                            desc9.putInteger( charIDToTypeID('DthA'), 100 );
                            desc9.putInteger( charIDToTypeID('DChS'), 0 );
                            desc9.putInteger( charIDToTypeID('DCUI'), 0 );
                            desc9.putBoolean( charIDToTypeID('DChT'), false );
                            desc9.putBoolean( charIDToTypeID('DChV'), false );
                            desc9.putInteger( charIDToTypeID('WebS'), 0 );
                            desc9.putEnumerated( charIDToTypeID('TDth'), charIDToTypeID('IRDt'), charIDToTypeID('None') );
                            desc9.putInteger( charIDToTypeID('TDtA'), 100 );
                            desc9.putBoolean( charIDToTypeID('Trns'), true );
                            desc9.putBoolean( charIDToTypeID('Mtt '), false );
                            desc9.putInteger( charIDToTypeID('MttR'), 255 );
                            desc9.putInteger( charIDToTypeID('MttG'), 255 );
                            desc9.putInteger( charIDToTypeID('MttB'), 255 );
                            desc9.putBoolean( charIDToTypeID('SHTM'), false );
                            desc9.putBoolean( charIDToTypeID('SImg'), true );
                            desc9.putBoolean( charIDToTypeID('SSSO'), false );
                                var list3 = new ActionList();
                            desc9.putList( charIDToTypeID('SSLt'), list3 );
                            desc9.putBoolean( charIDToTypeID('DIDr'), false );
                            desc9.putPath( charIDToTypeID('In  '),File( saveFile) );
                        desc8.putObject( charIDToTypeID('Usng'), stringIDToTypeID('SaveForWeb'), desc9 );
                        executeAction( charIDToTypeID('Expr'), desc8, DialogModes.NO );
                    };
                    function FitImage( inWidth, inHeight ) {
                     if ( inWidth == undefined || inHeight == undefined ) {
                      alert( "FitImage requires both Width & Height!");
                      return;
                     }
                     var desc = new ActionDescriptor();
                     var unitPixels = charIDToTypeID( '#Pxl' );
                     desc.putUnitDouble( charIDToTypeID( 'Wdth' ), unitPixels, inWidth );
                     desc.putUnitDouble( charIDToTypeID( 'Hght' ), unitPixels, inHeight );
                     var runtimeEventID = stringIDToTypeID( "3caa3434-cb67-11d1-bc43-0060b0a13dc4" ); 
                     executeAction( runtimeEventID, desc, DialogModes.NO );
                    }
                    
                    • 7. Re: Saving JPG to PNG using Save for the Web
                      Iain H Level 1

                      Thanks Paul - this works perfectly.

                       

                      Iain