Skip to main content
Participant
June 22, 2021
Answered

Select Subject trim to the center ratio 3:4 Keep Background

  • June 22, 2021
  • 3 replies
  • 1480 views

Me again.

Another issue.

How do I center this kind of images to a ratio 3:4 keeping the backround color.

 

I found this script here : and it's working in terms of centering the image but it leaves an empty line on the right of the image...

#target photoshop

var oldPref = app.preferences.rulerUnits;//stores old measurement preferences so that they can be reset when the script is done.
app.preferences.rulerUnits = Units.PIXELS;
var doc = activeDocument;

selSub ();
var cropBounds = doc.selection.bounds;
var vertLength = parseInt(cropBounds[3]-cropBounds[1]);
var horLength = parseInt(cropBounds[2]-cropBounds[0]);
var cRatio = 3/4;
var horPad = ((vertLength*cRatio) - horLength)/2;

doc.selection.deselect();

cropImg ();

app.preferences.rulerUnits = oldPref;


function selSub(){
    var idautoCutout = stringIDToTypeID( "autoCutout" );
        var desc4 = new ActionDescriptor();
        var idsampleAllLayers = stringIDToTypeID( "sampleAllLayers" );
        desc4.putBoolean( idsampleAllLayers, false );
    executeAction( idautoCutout, desc4, DialogModes.NO );    
    };

function cropImg(){
      var idCrop = charIDToTypeID( "Crop" );
          var desc4 = new ActionDescriptor();
          var idT = charIDToTypeID( "T   " );
              var desc5 = new ActionDescriptor();
              var idTop = charIDToTypeID( "Top " );
              var idPxl = charIDToTypeID( "#Pxl" );
              desc5.putUnitDouble( idTop, idPxl, cropBounds[1] );
              var idLeft = charIDToTypeID( "Left" );
              var idPxl = charIDToTypeID( "#Pxl" );
              desc5.putUnitDouble( idLeft, idPxl, cropBounds[0]-horPad );
              var idBtom = charIDToTypeID( "Btom" );
              var idPxl = charIDToTypeID( "#Pxl" );
              desc5.putUnitDouble( idBtom, idPxl, cropBounds[3] );
              var idRght = charIDToTypeID( "Rght" );
              var idPxl = charIDToTypeID( "#Pxl" );
              desc5.putUnitDouble( idRght, idPxl, cropBounds[2]+horPad );
          var idRctn = charIDToTypeID( "Rctn" );
          desc4.putObject( idT, idRctn, desc5 );
          var idAngl = charIDToTypeID( "Angl" );
          var idAng = charIDToTypeID( "#Ang" );
          desc4.putUnitDouble( idAngl, idAng, 0.000000 );
          var idDlt = charIDToTypeID( "Dlt " );
          desc4.putBoolean( idDlt, false );
          var idcropAspectRatioModeKey = stringIDToTypeID( "cropAspectRatioModeKey" );
          var idcropAspectRatioModeClass = stringIDToTypeID( "cropAspectRatioModeClass" );
          var idunconstrained = stringIDToTypeID( "unconstrained" );
          desc4.putEnumerated( idcropAspectRatioModeKey, idcropAspectRatioModeClass, idunconstrained );
      executeAction( idCrop, desc4, DialogModes.NO );   
    };

 

This topic has been closed for replies.
Correct answer jazz-y

add line after desc4.putBoolean( idDlt, false );

 desc4.putBoolean( stringIDToTypeID( "autoFill" ), true );

 

3 replies

JJMack
Community Expert
Community Expert
June 22, 2021

The process you want to do can be done by an action with the help of a Plug-in script I wrote.   In CS3 Adobe added Plug-in support into Photoshop Scripting and changed Adobe's Compiled "Fit Image" implementation to a Photoshop Scripted implementation.   I used Adobe Fit Image script to create two plug-in to make cropping images to a aspect ration  and then set the print resolution for the paper  size. "AspectRatioSelection.jsx" and "LongSidePrintLength.jsx."

 

So an Action to do what you want is easy.

 

Step 1 Select  Subject

Step 2 Image>Crop

Step 3 Automate>Aspect Ratio Selection...

Step 4 Image Crop

Step 5 Deselect

 

JJMack
Participant
June 23, 2021

Thank you JJMack, I'll have a look into you Actions !

Kukurykus
Legend
June 22, 2021

Shouldn't you answer anything in Crop/trim image automation 3:4, where you got helped?

Participant
June 23, 2021

Sorry, sure I will. I figure it out how to do it in Python using Face Landmarks. Works very good !

jazz-yCorrect answer
Legend
June 22, 2021

add line after desc4.putBoolean( idDlt, false );

 desc4.putBoolean( stringIDToTypeID( "autoFill" ), true );

 

Participant
June 23, 2021

Fantastic ! one single line ! Spasibo !