4 Replies Latest reply on Jun 2, 2016 7:59 PM by Juckaluckabee

    Cropping, triming, and bounds not matching

    Juckaluckabee Level 1

      I have some source code that exports images from a PS project.


      The problem is that when I do a


      doc.trim() and doc.crop()


      the exported image size and the layer bounds sizes do not match. The images seem to be cropped to much. There is some opaque pixels that are clearly cut out(they come from the layer style's outer glow FX). My guess is cropping isn't taking into account the FX. How does one rasterize the layer style?


      (n.Layer as ps.ArtLayer).Rasterize(Photoshop.PsRasterizeType.psEntireLayer);


      does not rasterize the style and I cannot find any info on how to do that to test if this will solve the problem.


      I need the cropping size and the image size to be exactly identical


      The specific code is




      n.X = layer.Bounds[0];

      n.Y = layer.Bounds[1];





         // Set Width and Height after trim and crop just in case were modified.
         n.Width = layer.Bounds[2] - layer.Bounds[0];
         n.Height = layer.Bounds[3] - layer.Bounds[1];    



      n is essentially a rect. layer is the current PS layer. I'm expecting the Width and Height from layer bounds to match the exported image.


      Any ideas?



        • 1. Re: Cropping, triming, and bounds not matching
          c.pfaffenbichler Level 9
          How does one rasterize the layer style?

          Record the operation with ScriptingListener-plugin.

          • 2. Re: Cropping, triming, and bounds not matching
            Juckaluckabee Level 1
               public static void RasterizeLayerStyle(ps.Document doc)
               var idrasterizeLayer = doc.Application.StringIDToTypeID("rasterizeLayer");  
               var desc5 = new Photoshop.ActionDescriptor();
               var idnull = doc.Application.CharIDToTypeID("null");
               var ref4 = new ps.ActionReference();
               var idLyr = doc.Application.CharIDToTypeID("Lyr ");
               var idOrdn = doc.Application.CharIDToTypeID("Ordn");
               var idTrgt = doc.Application.CharIDToTypeID("Trgt");
               ref4.PutEnumerated(idLyr, idOrdn, idTrgt);
               desc5.PutReference(idnull, ref4);
               var idWhat = doc.Application.CharIDToTypeID("What");
               var idrasterizeItem = doc.Application.StringIDToTypeID("rasterizeItem");
               var idlayerStyle = doc.Application.StringIDToTypeID("layerStyle");
               desc5.PutEnumerated(idWhat, idrasterizeItem, idlayerStyle);
               doc.Application.ExecuteAction(idrasterizeLayer, desc5, ps.PsDialogModes.psDisplayNoDialogs);
            • 3. Re: Cropping, triming, and bounds not matching
              Pedro Cortez Marques Level 3

              What you need to define is the ratio ( Height/Width ) after you have rasterized effects. I assume you really need to get and use the bounds of a layer to the final image you want to get.

              After the Trim, you need to add canvas (not only resize) if the ratio has changed. I've used javascript. Can you adapt this to your needs?

              Hope it helps.


              var lastUnits = app.preferences.rulerUnits;
              app.preferences.rulerUnits = Units.PIXELS;
              // you can rasterize effects here:
              var desc5 = new ActionDescriptor();
              var ref4 = new ActionReference();
              ref4.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
              desc5.putReference(charIDToTypeID("null"), ref4);
              desc5.putEnumerated(charIDToTypeID("What"), stringIDToTypeID("rasterizeItem"), stringIDToTypeID("layerStyle"));
              executeAction(stringIDToTypeID("rasterizeLayer"), desc5, DialogModes.NO);
              var newImgW = activeDocument.activeLayer.bounds[2]-activeDocument.activeLayer.bounds[0];
              var newImgH = activeDocument.activeLayer.bounds[3]-activeDocument.activeLayer.bounds[1];
              var ratio = newImgH/newImgW;
              var res = 72; // 72 ppi
              // faz o trim rente ao objecto
              var desc9 = new ActionDescriptor();
              desc9.putEnumerated( stringIDToTypeID( "trimBasedOn" ), stringIDToTypeID( "trimBasedOn" ), stringIDToTypeID( "topLeftPixelColor" ) );
              desc9.putBoolean( charIDToTypeID( "Top " ), true );
              desc9.putBoolean( charIDToTypeID( "Btom" ), true );
              desc9.putBoolean( charIDToTypeID( "Left" ), true );
              desc9.putBoolean( charIDToTypeID( "Rght" ), true );
              executeAction( stringIDToTypeID( "trim" ), desc9, DialogModes.NO );
              // If you are growing canvas, you need to be sure what color to be on background (I used white)
              if (app.activeDocument.activeLayer.isBackgroundLayer) {
                  var white = new SolidColor();
                  white.rgb.red = 255;
                  white.rgb.green = 255;
                  white.rgb.blue = 255;
                  app.backgroundColor = white;
              if(app.activeDocument.height < app.activeDocument.width * ratio) {
                  // shorter images
                  app.activeDocument.resizeImage(new UnitValue(newImgW,'px'), undefined, res, ResampleMethod.BICUBICSHARPER);
                  app.activeDocument.resizeCanvas( undefined, new UnitValue(newImgH,'px'), AnchorPosition.MIDDLECENTER);
              } else {
                  // taller images
                  app.activeDocument.resizeImage( undefined, new UnitValue(newImgH,'px'), res, ResampleMethod.BICUBICSHARPER);
                  app.activeDocument.resizeCanvas( new UnitValue(newImgW,'px'), undefined, AnchorPosition.MIDDLECENTER);
              app.preferences.rulerUnits = lastUnits;
              • 4. Re: Cropping, triming, and bounds not matching
                Juckaluckabee Level 1

                Thanks. I will try to see if it helps. Rasterizing the style made a difference but after some modifications trim and crop are not doing the same as they were. I'm going to have to track down a few things and get my exporting code in a bit better shape then I'll start messing with the size and location problems.