7 Replies Latest reply on Apr 26, 2015 12:06 PM by JJMack

    Resizing a layer

    cwodwrth Level 1

      I'm trying to resize a layer to increase its size by a random size between 100-120%, save the file, then resize it back to its original size, but I can't seem to get it working (the transform hangs). Any thoughts?

       

       

       

      var rNum = randomRange(100, 120)/100;

       

      var startRulerUnits = preferences.rulerUnits;

      preferences.rulerUnits = Units.PIXELS;

      var LB = theLayer.bounds;

      var lWidth = LB[2].value - LB[0].value;

      var lHeight = LB[3].value - LB[1].value;   

       

      //var msg = rNum + " " + lWidth + " " + lHeight;    <---- coming back as [1.0-1.2]  2697 2654

      //alert(msg);       

       

      theLayer.resize(Number(lWidth*rNum),Number(lHeight*rNum),AnchorPosition.MIDDLECENTER);

       

      myDocument.saveAs((new File("F:/test.jpg")),jpgopts,true);

       

      theLayer.resize(Number(lWidth),Number(lHeight),AnchorPosition.MIDDLECENTER);

      app.preferences.rulerUnits = startRulerUnits;

       

      ...

       

      function randomRange(minNum, maxNum)

      {

          return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);

      }

        • 1. Re: Resizing a layer
          JJMack Most Valuable Participant

          I do see where the var therLayer gets set it or a test to see it its an art layer and is not a background layer.  Background layers can not be resize they are canvas size they do not support transparency its locked.

           

          What I think you should do is duple the layer transform the layer save our the new jpeg file the delete the dupped layer. Leave the original image quality untouched rather then being interpolated twice.

          • 2. Re: Resizing a layer
            cwodwrth Level 1

            Sorry, I'm doing that check a little higher up -- the layer I'm transforming is a smart object, so interpolation changes shouldn't affect it.

             

            var theLayer = myDocument.artLayers.getByName ("Replace");

             

            // check if layer is smart object;

             

            if (theLayer.kind != "LayerKind.SMARTOBJECT") {alert ("selected layer is not a smart object")}

            ...

            • 3. Re: Resizing a layer
              JJMack Most Valuable Participant

              If its a smart a smart object layer there is an associated transform for the smart object. The layers bonds may be for the transform size not the objects size.  The layers current transform size.

               

              Your layer resize look like wrong.  You stare you want to resize to be random in the range 100% to 120% of the current size and you came up with

               

              theLayer.resize(Number(lWidth*rNum),Number(lHeight*rNum),AnchorPosition.MIDDLECENTER);


              where your rNum is in the rand of 1 to 1.2


              Your first resize should then be I believe

              theLayer.resize(rNum*100,rNum*100,AnchorPosition.MIDDLECENTER);


              the second one  I think may be

              theLayer.resize(100/rNum,100/rNum,AnchorPosition.MIDDLECENTER);


              However because a smart object layer has an associated trans You may need to take that into consideration.   I know I did when I created my collage toolkit back in CS2 timeframe.   After I placed in an image I always did  a transform to resize 100% in case the Place scale the image down in size to fit the canvas.   I needed the get that image's actual size so I could calculate the percentage change I need to fit the image to the location it needed to fill.


              In the following screen capture I'm placing a 8.2 Raw file into a 4"x6" note the default Place Transform setting scaling the image to fit the 4"x6" 300dpi canvas. I had trouble doing the  resize I wanted to make if I use the current layer size I need the actual image so I did a .resize(100,100,center). I needed to get to image to its actual object size so I coil calculate the percentage I needed to change its size to fill the pixels area I needed to fill.

              Capture.jpg

              • 4. Re: Resizing a layer
                cwodwrth Level 1

                You are exactly right - I never knew that was why I could never resize a smart object by its width and height, and now it makes complete sense. This is working now, except you were right again - when I run this 100's of times, I do begin to get some rounding error factored in (I'm also rotating the image each time now). Could you show me how you suggest duplicating the layer and deleting it?

                • 5. Re: Resizing a layer
                  JJMack Most Valuable Participant

                  There are two ways one may duplicate a smart object layer.   Menu Layer>Duplicate Layer...  and menu Layer>Smart Objects>New Smart Object via Copy.  The first way the two later will share a single object,  The second way the new smart object is independent from the original smart object layer.  However I doe not know what the second way would to if the object itself is a linked object not an embedded object.  Old release of Photoshop only had menu File>Image Place...  this is now menu File>Image Place Embedded... If the object is a linked object the only difference may be that the links can be replaced independently. Then have two different images.   One of the reason to use embedded object is with RAW files when new smart object via Copy is used you can have layer with different RAW conversions.  Each layer has a copy of the original RAW file and its own set of ACR settings as well as an associated transform. If all the transforma are the same the layers are located in the same area over the canvas and can be blended together.  Each layer can also have its own layer mask to aid blending.

                   

                  In Any case if a transform make a layer larger then canvas size or position part or all of the layers off canvas the document canvas size will act like a clipping mask for all the layers. 

                  • 6. Re: Resizing a layer
                    cwodwrth Level 1

                    Oh, so I know how to duplicate the layer within photoshop, I thought you were talking about doing it within the script - is there a way to do that? The script is doing at least 1000 passes on this layer and 3 transformations on each pass.

                    • 7. Re: Resizing a layer
                      JJMack Most Valuable Participant

                      You do it the same way in a script.  If there  is no DOM method you would use scripted action manager code. You would modify Scriptlistener javascript recorded code.