6 Replies Latest reply on Jun 5, 2016 7:43 AM by JJMack

    Specify "Center"

    Juckaluckabee Level 1

      Is there a way to specify the center point of a layer and get that in code? I have several items I am exporting and it would be nice if I could get export the center point of the layer too. (For asymmetric images, e.g., something with a drop shadow won't have the center in the middle of the image)

        • 1. Re: Specify "Center"
          JJMack Most Valuable Participant

          Photoshop support any size layer.   Layers bound are rectangular and can have any aspect ratio.  Layers can be any shape and may or may not have a pixel ate the layers center.   You can get the layer bounds and calculate its center.  The center may or may not be over the documents canvas.  The document canvas may act as a clipping mask for a layer. Masking some or all of a layer's pixels off.

           

          You can align layers to wherever you want.

           

          Layer styles are effects they do not have pixels in any layer.  Pixels are rendered for layer style effects by photoshop to the document composit view. Layer style pixels do not exist in any layer.

           

          You were rasterizing a layer style  a layer with a style to convert the layer whatever kind it was to a pixel layer with the layer style effect applied to the layer.  Any layer style effect that fell in or outside the layer bounds now has pixels in the rasterized layer. The layers bounds have changed and the layer has no associated layer style and is a normal pixel layer.

           

           

          Capture.jpg

          // enable double-clicking from Mac Finder or Windows Explorer
          #target photoshop // this command only works in Photoshop CS2 and higher
          
          
          // bring application forward for double-click events
          app.bringToFront();
          
          
          // Save the current preferences
          var startRulerUnits = app.preferences.rulerUnits;
          // Set Photoshop to use pixels 
          app.preferences.rulerUnits = Units.PIXELS;
          
          
          try{
            var LB = activeDocument.activeLayer.bounds;
            var LWidth = (LB[2].value) - (LB[0].value);
            var LHeight = (LB[3].value) - (LB[1].value);
            MarkX((LB[0].value + LWidth/2));
            MarkX(LB[0].value);
            MarkX(LB[2].value);
            MarkY((LB[1].value + LHeight/2));
            MarkY(LB[1].value)
            MarkY(LB[3].value)
            alert("'" + activeDocument.activeLayer.name + "' Layer Bounds\nTop Left " +  LB[0].value + "X," +  LB[1].value + "Y   Bottom Right "  +  LB[2].value + "X," +  LB[3].value
            + "Y\nWidth " +  LWidth + "px   Height " + LHeight +"px" 
            + "\nLayer center relative to canvas " + (LB[0].value + LWidth/2) + "X," +  (LB[1].value + LHeight/2) +"Y" 
            );
          }
          catch(e){alert("Requires a layer targered");}
          
          
          // Return the app preferences
          app.preferences.rulerUnits = startRulerUnits;
          function MarkX(x) {
          // =======================================================
          var idMk = charIDToTypeID( "Mk  " );
              var desc61 = new ActionDescriptor();
              var idNw = charIDToTypeID( "Nw  " );
                  var desc62 = new ActionDescriptor();
                  var idPstn = charIDToTypeID( "Pstn" );
                  var idPxl = charIDToTypeID( "#Pxl" );
                  desc62.putUnitDouble( idPstn, idPxl, x);
                  var idOrnt = charIDToTypeID( "Ornt" );
                  var idOrnt = charIDToTypeID( "Ornt" );
                  var idVrtc = charIDToTypeID( "Vrtc" );
                  desc62.putEnumerated( idOrnt, idOrnt, idVrtc );
              var idGd = charIDToTypeID( "Gd  " );
              desc61.putObject( idNw, idGd, desc62 );
          executeAction( idMk, desc61, DialogModes.NO );
          }
          function MarkY(y) {
          // =======================================================
          var idMk = charIDToTypeID( "Mk  " );
              var desc63 = new ActionDescriptor();
              var idNw = charIDToTypeID( "Nw  " );
                  var desc64 = new ActionDescriptor();
                  var idPstn = charIDToTypeID( "Pstn" );
                  var idPxl = charIDToTypeID( "#Pxl" );
                  desc64.putUnitDouble( idPstn, idPxl, y );
                  var idOrnt = charIDToTypeID( "Ornt" );
                  var idOrnt = charIDToTypeID( "Ornt" );
                  var idHrzn = charIDToTypeID( "Hrzn" );
                  desc64.putEnumerated( idOrnt, idOrnt, idHrzn );
              var idGd = charIDToTypeID( "Gd  " );
              desc63.putObject( idNw, idGd, desc64 );
          executeAction( idMk, desc63, DialogModes.NO );
          }
          

           

          Note I added a drop shadow to the center star then Rasterized the Layer style. Bite the Layer Bounds have changed and the layers center relative to the canvas has changed and the resulting rasterized layer has nor layer style it has been rendered into the layers which changed the layers bounds it is not a shape layer though it shape is that of a star with a shadow.

          Capture.jpg

          • 2. Re: Specify "Center"
            Juckaluckabee Level 1

            No, this is not what I mean... except in the most simple case. Sorry for not being clearer.

             

            Suppose I import a random image into photoshop. It is very symmetric and most of the interesting stuff centers off the middle of the "layer bounds"(so the simple formula x + w/2, y + h/2 won't work).

             

            Now, Photoshop doesn't have a clue what the center is(maybe it could theoretically calculate it using a center of mass type of formula but it doesn't do this AFAIK).

             

            But does the user have any ability to specify such a "center" of the image(I don't mean center of the canvas) then retrieve it through scripting in any shape or form that isn't too convoluted?

             

            Why I think it might be possible is that when you do a transform on a layer, you can set the "center"/anchor point for the axis of rotation.  Maybe Photoshop keeps that anchor around, not just for the transform, or has a similar behavior. Is so, and if I could get the chords of it through scripting, then it would do what I want.

             

            I could, for example, let the user click on the canvas to give them to the script but that would require doing it every time unless I get really creative.... which will be quite a lot of work.

            • 3. Re: Specify "Center"
              JJMack Most Valuable Participant

              A Layer style like a drop shadow will change the layers bounds however it will not change the layers pixels. The layers transparency will not change. If you do a quick export as a png the Layer Pixels and Layer style effects pixels will be exported. The png will have the layer style effect and the pmg trimmed to the drop shadow.  If you then turn off the drop shadow effect visibility and do a second quick export as png  the export will not contain the drop shadow and the canvas will be trimmed to the shape.

              Capture.jpg

              • 4. Re: Specify "Center"
                JJMack Most Valuable Participant

                Layer bonds change with outside layer style effect visibility.  You can align a layer to a section using 9 relative positions. You normality use two align statements.

                 

                Capture.jpg

                Align to a selection any selection  you want to use.

                Capture.jpg

                • 5. Re: Specify "Center"
                  JJMack Most Valuable Participant

                  In my collage scripts I have a dropdown list of the 9 locations relative to the populated image you can stamp the files name on the image.

                  Capture.jpg

                  • 6. Re: Specify "Center"
                    JJMack Most Valuable Participant

                    Aligning is aligning  bounds.  It is hard to align a layer that contains transparency.  It can be any shape and have empty pixels all over the place and may only have a few pixels at the layers bounds. Eight points of the layer bounds are just out side the layer where the ninth point is dead center.   Selections bounds also have nine point you can align a layer bounds point to.  The smalest selection you can make is one pixel. Selection bounds is nine pixels the pixel selected and the eight pixela the border the selected pixel. So you can align one of the 9 layer bounds point to any pixel with two align statments.

                     

                    Capture.jpg