2 Replies Latest reply on Apr 14, 2011 1:12 AM by Paul Riggott

    Change resolution of an image

    GWDAUS

      I have found a script that I am using to scale an image to a specified size using the line tool. Is there a way of changing the the resolution of the image by adding additional script prior to the scaling script. I am using Java Script and prefer if the image isn't resampled.

       

      Also as this script will be used by several people can you include user instruction that will require a user action during the process of the script. In this case it would require the user to use the line tool to define the length of the object that required scaling.

       

       

      Thanks

       

       

      Gary

        • 1. Re: Change resolution of an image
          JJMack Most Valuable Participant

          Resample none ie activeDocument.resizeImage(null, null, savedResolution, ResampleMethod.NONE);

          you just need to set  one value width or height or dpi

           

          Here is a script I use within actions to save and restore an image resolution without resampling

           

          /* ======================================================================================

          // 2009  John J. McAssey (JJMack)  http://www.mouseprints.net/

          // 

          // This script is supplied as is. It is provided as freeware.

          // The author accepts no liability for any problems arising from its use.

          //

          // This script is designed to be used by a Photoshop Action twice

          // A good pratice to use when creating an actions that use this scipt is for the action

          // not to do a save or play some other action between its two useages of this Script.

          //

          // The first time this script is used by an action the documents current resolution

          // and ruler units are saved into the document's meta-data Info Instructions field.

          //

          // The second time this script used by the action the script retreives what was

          // saved in the meta-data during the first usage, resolution and ruler units.

          // The document is resized to the saved resolution,

          // Photoshop's ruler units are set to saved units

          // and the saved data is removed from the document's meta-data Info Instructions field.

          //

          // ===================================================================================== */

           

          /*

          <javascriptresource>

          <about>$$$/JavaScripts/SaveAndRestoreResolution/About=JJMack's SaveAndRestoreResolution.^r^rCopyright 2009 Mouseprints.^r^rRun twice script utility for action.^rNOTE:Don't play other actions between runs!^r^rFirst Run records Photoshop's preferences Units and documents DPI resolution.^rSecond Run restores the recorded setting and removes the recording.</about>

          <category>JJMack's Action Run Twice Utility</category>

          </javascriptresource>

          */

           

          if (app.documents.length > 0) {

          if (app.activeDocument.info.instructions.indexOf("<Resolution>") == -1 ){ // no footprint fisrt useage

          //alert("first");

          // Retreive Document information for Foot Print

                          var units = app.preferences.rulerUnits;

          app.preferences.rulerUnits = Units.PIXELS; // set ruler units to PIXELS

                          var typeunits = app.preferences.typeUnits;

          var res = app.activeDocument.resolution;

           

          // put footprint in metadata info instructions

          app.activeDocument.info.instructions = app.activeDocument.info.instructions + "<Units>" + units + "</Units>"  + "<Tunits>" + typeunits + "</Tunits>" + "<Resolution>" + res + "</Resolution>";

          //alert( "Saved ="  + "<Units>" + units + "</Units>" + "<Tunits>" + typeunits + "</Tunits>" + "<Resolution>" + res + "</Resolution>" );

           

          app.preferences.rulerUnits = units; // restore ruler units

          }

          else {

          //alert("second");

           

          // Retreive saved information

          unitsOffset = app.activeDocument.info.instructions.indexOf("<Units>") + "<Units>".length;

          unitsLength = app.activeDocument.info.instructions.indexOf("</Units>") -unitsOffset;

          savedUnits = app.activeDocument.info.instructions.substr(unitsOffset, unitsLength);

           

          tunitsOffset = app.activeDocument.info.instructions.indexOf("<Tunits>") + "<Tunits>".length;

          tunitsLength = app.activeDocument.info.instructions.indexOf("</Tunits>") -tunitsOffset;

          savedTunits = app.activeDocument.info.instructions.substr(tunitsOffset, tunitsLength);

           

          resOffset = app.activeDocument.info.instructions.indexOf("<Resolution>") + "<Resolution>".length;

          resLength = app.activeDocument.info.instructions.indexOf("</Resolution>") + -resOffset;

          savedResolution = app.activeDocument.info.instructions.substr(resOffset, resLength);

          //alert("Resolution = " + savedResolution + " Units = " + savedUnits );

           

          // Restore resolution

          app.preferences.rulerUnits = Units.PIXELS;

          activeDocument.resizeImage(null, null, savedResolution, ResampleMethod.NONE);

           

          // Restore ruler units

          // I get a message Enumerated value expected if I try to use var savedUnits app.preferences.rulerUnits = savedUnits;

          // perhaps if I knew Javascript I would not need to use the following if else if .....

          if ( savedUnits == "Units.INCHES" ){ app.preferences.rulerUnits = Units.INCHES;}

          else if ( savedUnits == "Units.CM" ){ app.preferences.rulerUnits = Units.CM;}

          else if ( savedUnits == "Units.PERCENT" ){ app.preferences.rulerUnits = Units.PERCENT;}

          else if ( savedUnits == "Units.MM" ){ app.preferences.rulerUnits = Units.MM;}

          else if ( savedUnits == "Units.PIXELS" ){ app.preferences.rulerUnits = Units.PIXELS;}

          else if ( savedUnits == "Units.POINTS" ){ app.preferences.rulerUnits = Units.POINTS;}

          else if ( savedUnits == "Units.PICAS" ){ app.preferences.rulerUnits = Units.PICAS;}

           

          // Restore Type units

          if ( savedTunits == "TypeUnits.PIXELS" ){ app.preferences.typeUnits = TypeUnits.PIXELS;}

          else if ( savedTunits == "TypeUnits.POINTS" ){ app.preferences.typeUnits = TypeUnits.POINTS;}

          else if ( savedTunits == "TypeUnits.MM" ){ app.preferences.typeUnits = TypeUnits.MM;}

           

           

          // Remove footprint from metadata info instructions

          before = app.activeDocument.info.instructions.substr(0,app.activeDocument.info.instructions.indexO f("<Units>"));

          afterOffset = app.activeDocument.info.instructions.indexOf("</Resolution>") + "</Resolution>".length;

          after = app.activeDocument.info.instructions.substr(afterOffset, app.activeDocument.info.instructions.length - afterOffset);

          //alert ("before = " + before + " after = " + after);

          app.activeDocument.info.instructions = before + after;

          }

          }

          else { alert("You must have at least one open document to run this script!"); }

          • 2. Re: Change resolution of an image
            Paul Riggott Level 6

            To change the resolution, only a one line script is required or you could add the line to your existing script.

            This example changes the document to 72PPI

             

            activeDocument.resizeImage(undefined, undefined, 72, ResampleMethod.NONE);