Need help! Non-destructive trimming/cropping

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

GearSpinner

Need help! Non-destructive trimming/cropping

Post by GearSpinner »

Hey guys, I'm running into a problem while trying to trim/crop non-destructively in an export script

Basically, I have 3 layers. 2 are layers I'm going to export to png, 1 is a reference layer. I need to crop the document to the size of the reference layer, then resize the document to dimensions I have stored in two variables (x and y)

The problem I'm having is that the 2 art layers are having their pixels cut away during the trim operation. I need to export those layers as PNGs, but right now they are getting cut off by the trim.

I'm using the trim function right now: doc.trim(TrimType.TRANSPARENT,true,true,true,true)

Is there a way I can trim or crop the document but not lose those pixels on the art layers?

Thanks

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

pfaffenbichler

Need help! Non-destructive trimming/cropping

Post by pfaffenbichler »

If I understand you correctly, you could use the method crop instead of trim.
That takes bounds – which you could get from the reference layer.

From the Object Model Viewer:
Document.crop (bounds: UnitRect , angle: number , width: UnitValue , height: UnitValue )
GearSpinner

Need help! Non-destructive trimming/cropping

Post by GearSpinner »

After some more work on this last night, I did start on that path. The one mystifying thing (or among them, anyway) is what exacty .bounds is returning. I get four values, but I'm wondering how to correctly picture that in my mind. Are those the corners?

I was trying to plug them into some action manager script from the crop command, like so:

Code: Select allvar idCrop = charIDToTypeID( "Crop" );
        var desc10 = new ActionDescriptor();
        var idT = charIDToTypeID( "T   " );
            var desc11 = new ActionDescriptor();
            var idTop = charIDToTypeID( "Top " );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc11.putUnitDouble( idTop, idPxl, bounds[1] );

            var idLeft = charIDToTypeID( "Left" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc11.putUnitDouble( idLeft, idPxl, bounds[0] );

            var idBtom = charIDToTypeID( "Btom" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc11.putUnitDouble( idBtom, idPxl, bounds[3] );

            var idRght = charIDToTypeID( "Rght" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc11.putUnitDouble( idRght, idPxl, bounds[2] );

        var idRctn = charIDToTypeID( "Rctn" );
        desc10.putObject( idT, idRctn, desc11 );
        var idAngl = charIDToTypeID( "Angl" );
        var idAng = charIDToTypeID( "#Ang" );
        desc10.putUnitDouble( idAngl, idAng, 0.000000 );
        var idDlt = charIDToTypeID( "Dlt " );
        desc10.putBoolean( idDlt, false );
        var idcropAspectRatioModeKey = stringIDToTypeID( "cropAspectRatioModeKey" );
        var idcropAspectRatioModeClass = stringIDToTypeID( "cropAspectRatioModeClass" );
        var idunconstrained = stringIDToTypeID( "unconstrained" );
        desc10.putEnumerated( idcropAspectRatioModeKey, idcropAspectRatioModeClass, idunconstrained );
    executeAction( idCrop, desc10, DialogModes.NO );

I might be better off using the crop function -- can I just pass that the result of .bounds?
However, since I have to do this on like a hundred layers, maybe the action manager is faster.
GearSpinner

Need help! Non-destructive trimming/cropping

Post by GearSpinner »

Actually, it looks like doc.crop trims the pixels outside the area as well, unless someone knows how to make that not happen. I think the 'Delete Cropped Pixels' option in the crop tool was just introduced in CS6, but I can't seem to find how to flip that switch with the DOM.

So barring that I guess it's action manager or nothing. Can anyone help de-mystify what artLayer.bounds is giving me there?
pfaffenbichler

Need help! Non-destructive trimming/cropping

Post by pfaffenbichler »

A Layer’s bounds represent the position of the leftmost, uppermost, rightmost and lowermost content.

I don’t follow; what good are the pixels that are beyond the canvas in a png?

In any case you could just record a manual Crop with »Delete Cropped Pixels« unchecked with ScriptingListener.plugin and wrap that code in a function.
GearSpinner

Need help! Non-destructive trimming/cropping

Post by GearSpinner »

Cool, thanks! I needed to reveal some of the pixels before the png is saved, but after the crop. That script I was fiddling with up there is from the listener, and it appears to be the way for me to pull it off.

Think I'm good! Thanks again, guys