16 Replies Latest reply on Aug 15, 2013 2:00 AM by Philip Cord

    How to "change" resolution from DPI to l/cm?

    d.stoeck

      Hi Adobe community,

       

      right now I am transferring a huge and slow photoshop action to a javascript cause I needed some kind of dynamic variable an action couldn't offer. And on the other hand: a script is sooo much faster than an action. I had some points where the scripting API didn't give me enough possibilities and I had to use those annoying "action descriptors" I got from the ScriptingListener Plugin... so far so good.

       

      But now I am at a point where neither the API nor action descriptors seem to help. I am trying to switch the resolution of the image from "dots per image" to "lines per cm" without really resampling it. I just want to transform the "measurement unit", for example from 304.8 DPI to 120 l/cm or from 72 DPI to 28.346 l/cm.

       

      Does anyone know a way how to do this? Obviously it is possible through manual usage of the "image size" menu. And yes, I am aware of the fact that this is just cosmetic. But the client that needs this script sort of insists of the "lines per cm" measurement unit.

       

      I would really appreciate any help.

       

      Greetings,

      Daniel

        • 1. Re: How to "change" resolution from DPI to l/cm?
          Michael L Hale Level 5

          In Photoshop 'dots' or 'lines' means pixels. DPI normally means dots per inch( or pixels per inch ). So it sounds to me like all you need to do is change the resolution from inches to cm. That can be done with imageResize().

          • 2. Re: How to "change" resolution from DPI to l/cm?
            d.stoeck Level 1

            the API says:

             

            resizeImage([width] [, height] [, resolution] [, resampleMethod])

             

            width is a UnitValue

            height is a UnitValue

            resolution is a number

            resampleMethod is from type ResampleMethod

             

            I might be wrong, but I don't see the possibility here to change from inches to cm cause resolution is just a number? :-/

             

            taken from: here

            • 3. Re: How to "change" resolution from DPI to l/cm?
              Michael L Hale Level 5

              Maybe I mis-understood your question. Yes the resoluton arugment is pixels. So when the ppi doesn't match the ppcm you do the math to resize the image. If the ppi = 72 then ppcm = 28.346. It's just a matter of what ruler you use.

              • 4. Re: How to "change" resolution from DPI to l/cm?
                d.stoeck Level 1

                Doing the math is not the issue at all. But what u say either doesn't work or I am too dumb.

                 

                ...

                app.preferences.rulerUnits = Units.CM;

                app.preferences.typeUnits = TypeUnits.MM;

                ...

                doc.resizeImage(null, null, 120, ResampleMethod.NONE);

                ...

                 

                -> wenn I reopen the resulting image in Photoshop, then Photoshop says it has 120 DPI... which is absolutely not what I want :-/



                • 5. Re: How to "change" resolution from DPI to l/cm?
                  d.stoeck Level 1

                  Can anybody help? Did anybody face the same problem already?

                   

                  I mean... what I want is fairly easy if u do it via the Photoshop menu... but via javascript it just doesn't want to work :-/

                  • 6. Re: How to "change" resolution from DPI to l/cm?
                    Michael L Hale Level 5

                    I now think that you are tying to set the resolution unit dropdown in the Resize Image dialog. That is a dialog preference and I don't know of a way to change that with a script.

                    • 7. Re: How to "change" resolution from DPI to l/cm?
                      d.stoeck Level 1

                      sorry, but *bump*

                       

                      anyone around who can help here? this still is an issue :-/

                      • 8. Re: How to "change" resolution from DPI to l/cm?
                        Michael L Hale Level 5

                        Maybe I did not explain this well so I will try again.

                         

                        The resolution argument for resizeImage is a number. That number is ppi regardless of the ruler setting.

                         

                        You can set the resolution in other units if you do the math to convert that unit into inches.

                         

                        This will give you a 120 pixel per cm image.

                         

                        app.activeDocument.resizeImage(undefined, undefined, 120*2.54, ResampleMethod.NONE);

                        • 9. Re: How to "change" resolution from DPI to l/cm?
                          d.stoeck Level 1

                          Hi Michael,

                           

                          your answer is 100% correct in its meaning. But it doesn't help me.

                           

                          app.activeDocument.resizeImage(undefined, undefined, 120*2.54, ResampleMethod.NONE);

                          app.activeDocument.resizeImage(undefined, undefined, 304.8, ResampleMethod.NONE);

                           

                          The two commands seen above give the exact same result. And ofcourse, I could do the math and multiply with 2.54 but the result would still be the same. I would get an image with 304.8 dots per inch and not with 120 pixels per centimeter. Yes, I know this is confusing and 99% of the people would say this is exactly the same.

                           

                          Pixel-wise this actually is the same. But I am talking about the measurement unit Photoshop shows u when u open an image and open the resize dialog. It shows either 120 lpcm or 304.8 dpi regarding the setting of the dropdown menu. And as I wrote before: when u save it manually with Photoshop this setting gets saved within the image.

                           

                          The difference must be somewhere in the metadata?

                           

                          (Btw... customers are always right... if I could decide it, I would just take the 304.8 DPI version. But the customer insists on 120 lines per centimeter and thus we have to save any picture manually with Photoshop.

                           

                          Regards,

                          Daniel

                           


                          • 10. Re: How to "change" resolution from DPI to l/cm?
                            Muppet Mark Level 5

                            I made a couple of files one using cm and one inch… If I resize without resample it does require a save and when the resize dialog is reopened it uses the option that was last used in the image. So I think it must be stored in the image… I don't see it anywhere in the metadata. I also had a quick glance thru the photoshop file spec and didn't notice anything there either…

                            • 11. Re: How to "change" resolution from DPI to l/cm?
                              Philip Cord Level 4

                              I think you will find it is the tiff schema, ResolutionUnit and set to 3 for CM.

                              • 12. Re: How to "change" resolution from DPI to l/cm?
                                Michael L Hale Level 5

                                Yes, I miss-understood your question. The metadata you are looking for is tiff:ResolutionUnit. That holds an integer 1, 2, or 3. 2 is for inch and 3 is for cm.

                                 

                                But it is not part of the metadata you can change in an open document. You can get the current value, which is read-only. It is part of the array returned by this property

                                 

                                app.activeDocument.info.exif

                                • 13. Re: How to "change" resolution from DPI to l/cm?
                                  d.stoeck Level 1

                                  Ooooooh nice to see that we are getting closer.

                                   

                                  I don't see the point why you can change this via Photoshop when u use the resize image dialog as a user, but u can't change it with scripting? Why is this part of the metadata read-only? :-/

                                   

                                  I will try some researching about how to write such metadata entries tomorrow. Or am I happy enough to find someone here in the scripting forums that actually knows if or how this can be achieved?

                                   

                                  Thank you all for your help.

                                  Daniel

                                  • 14. Re: How to "change" resolution from DPI to l/cm?
                                    Michael L Hale Level 5

                                    I could be wrong of course but I said you can't change that setting in a document metadata. You could close the document and update the file.

                                     

                                    var file = new File('~/desktop/test/a-06.psd');
                                    alert(getResolutionUnit( file ));
                                    setResolutionUnit( file, 3 );
                                    alert(getResolutionUnit( file ));
                                    function getResolutionUnit( file ){
                                        try{
                                            loadXMPLibrary();
                                            var xmpf = new XMPFile( file.fsName, XMPConst.UNKNOWN, 
                                                                XMPConst.OPEN_FOR_READ );
                                            var xmp = xmpf.getXMP();
                                            xmpf.closeFile();
                                            var res = xmp.getProperty
                                                        (XMPConst.NS_TIFF, "ResolutionUnit", XMPConst.INTEGER );
                                        }catch(e){return false;}
                                        unloadXMPLibrary();
                                        if( res == undefined ) res = false;
                                        return res;
                                    };
                                    function setResolutionUnit( file, unit ){
                                        try{
                                            loadXMPLibrary();
                                            var xmpf = new XMPFile( file.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
                                            var xmp = xmpf.getXMP();
                                            xmp.setProperty(XMPConst.NS_TIFF, "ResolutionUnit", unit );
                                            xmpf.putXMP( xmp );
                                            xmpf.closeFile();
                                        }catch(e){
                                            unloadXMPLibrary();
                                            return -1;
                                        }
                                        unloadXMPLibrary();
                                    };
                                    function loadXMPLibrary(){
                                        if ( !ExternalObject.AdobeXMPScript ){
                                            try{
                                                ExternalObject.AdobeXMPScript = new ExternalObject
                                                                                    ('lib:AdobeXMPScript');
                                            }catch (e){
                                                return false;
                                            }
                                        }
                                        return true;
                                    };
                                    function unloadXMPLibrary(){
                                       if( ExternalObject.AdobeXMPScript ) {
                                          try{
                                             ExternalObject.AdobeXMPScript.unload();
                                             ExternalObject.AdobeXMPScript = undefined;
                                          }catch (e){}
                                       }
                                    };
                                    
                                    • 15. Re: How to "change" resolution from DPI to l/cm?
                                      d.stoeck Level 1

                                      Michael, thank you very much for your code. I tried it and played around with it. It kinda works... the file itself gets modified and when I inspect the metadata of the image, I see all the values I changed. Especially ResolutionUnit = 3...

                                       

                                      ...but when I reopen the image in Photoshop, it still has the old values. Photoshop seems to ignore the changes.

                                       

                                       

                                      See this screenshot of the metadata information: (the old value of ResolutionUnit actually was 2... just dont have a screenshot of that right now)

                                      Bildschirmfoto 2013-08-14 um 10.59.37.png

                                       

                                      and compare it with the information in the "resize image dialog":

                                      Bildschirmfoto 2013-08-14 um 10.59.51.png

                                       

                                      These are the complete metadata Photoshop shows me...

                                      Bildschirmfoto 2013-08-14 um 11.10.54.png

                                       

                                       

                                      ... where the hell does Photoshop get the "wrong & old" information seen in the 2nd screenshot? Any ideas? :-/

                                       

                                      Regards,

                                      Daniel

                                      • 16. Re: How to "change" resolution from DPI to l/cm?
                                        Philip Cord Level 4

                                        It seems that if you save the document for Save For Web, then apply the metadata, it seems to work. As an example create a docment and run the following code.

                                         

                                         

                                        var f = File(Folder.desktop + "/300PPI.jpg");
                                        SaveForWeb(f,60);
                                        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); 
                                        setRes(f,300);
                                        open(f);
                                        
                                        function setRes( file, newRes ){
                                        if ( !ExternalObject.AdobeXMPScript ) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
                                                var xmpf = new XMPFile( File(file).fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE );
                                                var xmp = xmpf.getXMP();
                                                xmp.deleteProperty( XMPConst.NS_TIFF, 'XResolution');
                                                xmp.setProperty( XMPConst.NS_TIFF, 'XResolution', newRes+"/1");
                                                        xmp.deleteProperty( XMPConst.NS_TIFF, 'YResolution');
                                                 xmp.setProperty( XMPConst.NS_TIFF, 'YResolution', newRes+"/1");
                                                  var PerInch = xmp.setProperty( XMPConst.NS_TIFF, 'ResolutionUnit',3); //Pixels per cm
                                              if (xmpf.canPutXMP( xmp )) {
                                                 xmpf.putXMP( xmp );
                                              }
                                              xmpf.closeFile( XMPConst.CLOSE_UPDATE_SAFELY );
                                        }
                                        function SaveForWeb(saveFile,jpegQuality) {
                                        var sfwOptions = new ExportOptionsSaveForWeb(); 
                                           sfwOptions.format = SaveDocumentType.JPEG; 
                                           sfwOptions.includeProfile = false; 
                                           sfwOptions.interlaced = 0; 
                                           sfwOptions.optimized = true; 
                                           sfwOptions.quality = jpegQuality; 
                                        activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
                                        }