3 Replies Latest reply on Jun 4, 2016 3:58 AM by JJMack

    Trim screws with cropping and size in general

    Juckaluckabee

      I have some exporting code that generally works except some elements's FX's extend beyond the images creating excess "wasted" space. e.g., a sort of glow effect that extends beyond the main part of the image that I don't really want. Sometimes I do want the effect(depends on what the image is going to be used for).

       

      If I do

       

      rasterizeStyle()

      crop()

       

      Then most of the images come out alright. I get the "full" image(the FX's are kept). This causes problems for the images I don't want. So I change it to

       

      crop()

      rasterizeStyle()

       

      and then I don't get the FX's properly.

       

      I, in theory, should be able to solve the problem by "trimming". Since the images I do not want the FX to be part of are at the edges of the canvas, I can "trim" them.

       

      This works when I don't rasterize the style but when I rasterize the style the images come out very distorted. The width of the images are as big as the canvas while the heights are normal.

       

      I've tried various combinations of trim with the above rasterize and crop and they all pretty much produce the same results.

       

      Can someone please explain EXACTLY(not theory) what crop, rasterizeStyle, and trim do?

       

      I believe they should be:

       

      1. Crop - reduces the canvas size to the layer bounds(didn't include in the above). I use the default layer bounds and do not mess with it. I assume they correctly identify the smallest bounding box that can be applied to the layer and enclose all non-transparent pixels.

       

      2. Rasterize style - converts the algorithmic styling FX into pixels. This is required because layer bounds seems to not include this information(I suppose one could rasterize the layer style, get the bounds, then undo the rasterize but I'm ultimately merging stuff down anyways.

       

      3. Trim - Trims any pixels outside the canvas. Calling it should really have no effect after a crop? It should definitely not affect the "aspect ratio" of an image.

       

      Any ideas? (BTW, I tend to use layer masks to specify the regions I want to keep when merging down sub-layers... not sure if that has anything to do with it or not)

        • 1. Re: Trim screws with cropping and size in general
          JJMack Most Valuable Participant

          Juckaluckabee wrote:

           

           

          If I do

           

          rasterizeStyle()

          crop()

           

          It is hard to follow what you are doing there is no "rasterizeStyle()" there is a  "rasterize(targer) where targer is a RasterizeType ENTIRELAYER. or FILLCONTENT, or LAYERCLIPPINGPATH, or LINKEDLAYERS, or SHAPE, or TEXTCONTENTS. There is also a "rasterizeAllLayers()".

           

           

          And "crop()" would I believe be invalid as well crop requires a bounds to crop to. "crop(bounds[, angle][, width][, height]) where bounds is an array of 4 UnitValue

           

          So how the corp is done depends on the bounds you use. I you crop to the layers bounds any layer style effect that fall outside the layers bounds would be clipped by the document cropped canvas size which is now the same as your layers bounds.. Effects like drop shadows would be lost for there is no canvas for the shadow to fall on.

          • 2. Re: Trim screws with cropping and size in general
            Juckaluckabee Level 1

            Your taking the code too literal. I have defined a rasterizeStyle from the script inspector. It does what one does in PS manually.

             

            Yes, crop requires the bounds.

             

            You are right. I realized too that I simply was passing layer.Bounds to crop after the trim or rasterization and they were probably modified. I will have to create extra code to allow for either case(expanding the bounds or keep it to the original)... depending on the case I want. This should solve my problem but requires me to specify which images will have which approach. (essentially save the bounds before I do any rasterizeStyle and pass to crop)

             

            So effectively I need

             

            bounds = layer.Bounds;

            rasterizeStyle()

            crop(bounds); // before it was crop(layer.Bounds) and layer.Bounds was modified by the rasterizeStyle().

             

            I haven't implement it and it will crop my FX on some images that are "within" the canvas. I think I can write a routine to calculate the correct bounds though and that should fix my problem.

             

            One problem what my layer masks were all white outside the canvas but I didn't know. I have to create correctly by creating the selection for what I want to keep then creating the layer. The selection should be white and everything else black. if I invert the select and fill with black paint then everything outside the canvas is white and won't be clipped(even though it may look the same visual in the layers list).

            • 3. Re: Trim screws with cropping and size in general
              JJMack Most Valuable Participant

              Your title states

              Trim screws with cropping and size in general

               

              To me that is your opinion because of your Idea of what you want. You sometime want style that fall outside of a layers bounds  and sometimes you don't.  You may be able to create both versions for a single layer document.  However when there are many layers that have layer style that fall outside of the layer's bounds some will fall fall on other layers and empty canvas.  Trim and Crop work on the document composite.  Even if you can get the bounds of all the layers that make the composite and crop to that bounds you will not eliminate all styles that are outside the layer they are on.

               

              IMO Trim does cropping the way you check trim away and works correctly.  It will not trim off effects like drop shadows.  If you do not want effects that fall outside layers bounds you need to turn the style effects visibility off..

               

              Capture.jpg