Copy link to clipboard
Copied
Hey guys,
Wondering if I can automate the cropping of this images using a script instead of an action...
I need to crop it to 900 x 1200px full body
Thank you !
Here is a script that does what I described in the last message. It requires a path for the subject (currently "Path 1", name editable on line 15) and at least one horizontal guide to indicate top crop. Values for result (width, height, bottom margin) can be adjusted on lines 12-14.
Code below, or download here:
https://www.marspremedia.com/software/download?asset=crop-model.zip
Also note: the script could have more error protection (perhaps I'll add some when time allows).
At this point, two pos
...Copy link to clipboard
Copied
See this if it's okay.
resizeCanvas(900, 1200);
function resizeCanvas(newWidth, newHeight) {
var desc1 = new ActionDescriptor();
desc1.putUnitDouble(charIDToTypeID('Wdth'), charIDToTypeID('#Pxl'), newWidth);
desc1.putUnitDouble(charIDToTypeID('Hght'), charIDToTypeID('#Pxl'), newHeight);
desc1.putEnumerated(charIDToTypeID('Vrtc'), charIDToTypeID('VrtL'), charIDToTypeID('Cntr'));
desc1.putEnumerated(charIDToTypeID('Hrzn'), charIDToTypeID('HrzL'), charIDToTypeID('Cntr'));
desc1.putEnumerated(stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("canvasExtensionColorType"), charIDToTypeID('BckC'));
executeAction(stringIDToTypeID('canvasSize'), desc1, DialogModes.NO);
}
Copy link to clipboard
Copied
Thank you, almost there but it's not the full body
Copy link to clipboard
Copied
While that image can be cropped like that. How would a script be able to do that? There is no Photoshop feature align selection to subject's full body. Or a Content aware serarch find and select full body.
Copy link to clipboard
Copied
Change these values
resizeCanvas (900, 1200);
to get what you need
Copy link to clipboard
Copied
Your script had those numbers which would crop the image relative the the center anchor pount. The 900x1200px crop produced is not the crop wanted it is what the op posted
They want
Changing the anchor point to the top center in your script would come closer
var idCnvS = charIDToTypeID( "CnvS" );
var desc119 = new ActionDescriptor();
var idWdth = charIDToTypeID( "Wdth" );
var idPxl = charIDToTypeID( "#Pxl" );
desc119.putUnitDouble( idWdth, idPxl, 900.000000 );
var idHght = charIDToTypeID( "Hght" );
var idPxl = charIDToTypeID( "#Pxl" );
desc119.putUnitDouble( idHght, idPxl, 1200.000000 );
var idHrzn = charIDToTypeID( "Hrzn" );
var idHrzL = charIDToTypeID( "HrzL" );
var idCntr = charIDToTypeID( "Cntr" );
desc119.putEnumerated( idHrzn, idHrzL, idCntr );
var idVrtc = charIDToTypeID( "Vrtc" );
var idVrtL = charIDToTypeID( "VrtL" );
var idTop = charIDToTypeID( "Top " );
desc119.putEnumerated( idVrtc, idVrtL, idTop );
executeAction( idCnvS, desc119, DialogModes.NO );
Copy link to clipboard
Copied
Guys, Thank you for your ideas but I need the full body shoes included
Copy link to clipboard
Copied
Copy link to clipboard
Copied
The body height is much larger than 1200. You could fit the image to 900x1200 then add canvas to insure the result is 900x1200. that could be scripted. Or you could record a simple action.
Copy link to clipboard
Copied
Yes, this is it ! A script would be very helpful if I don't ask too much
Copy link to clipboard
Copied
If you do not know how to script Phtoshop you can easily record an Action to to that and if you want it as a script you can convert the action atn file into a script usinf xtool
Step 1 record Menu File>Automats>Fit Image set with 901 height 1201
Step 2 Menu Image>image Size uncheck relative set width 900 px height 1200 px
Copy link to clipboard
Copied
Thank you very much for detailed explanation ! I'll try to convert it into a script ! Greetings from Germany !
Copy link to clipboard
Copied
See if that's okay
// SCRIPT
/// CROP
var desc73 = new ActionDescriptor();
var desc74 = new ActionDescriptor();
desc74.putUnitDouble( charIDToTypeID( "Top " ), charIDToTypeID( "#Pxl" ), 0.000000 );
desc74.putUnitDouble( charIDToTypeID( "Left" ), charIDToTypeID( "#Pxl" ), 49.500000 );
desc74.putUnitDouble( charIDToTypeID( "Btom" ), charIDToTypeID( "#Pxl" ), 1200 );
desc74.putUnitDouble( charIDToTypeID( "Rght" ), charIDToTypeID( "#Pxl" ), 900 );
desc73.putObject( charIDToTypeID( "T " ), charIDToTypeID( "Rctn" ), desc74 );
desc73.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ), 0.000000 );
desc73.putBoolean( charIDToTypeID( "Dlt " ), true );
desc73.putEnumerated( stringIDToTypeID( "cropAspectRatioModeKey" ), stringIDToTypeID( "cropAspectRatioModeClass" ), stringIDToTypeID( "targetSize" ));
desc73.putUnitDouble( charIDToTypeID( "Wdth" ), charIDToTypeID( "#Pxl" ), 900 );
desc73.putUnitDouble( charIDToTypeID( "Hght" ), charIDToTypeID( "#Pxl" ), 1200 );
executeAction( charIDToTypeID( "Crop" ), desc73, DialogModes.ALL );
Exsampl
Copy link to clipboard
Copied
It's working ! But I was thinking something that would do it without my interraction...I mean moving the cropping thing and so...I think I was wrong when asking about cropping maybe trimming is the right term ? Thank you very much again guys ! And sorry for causing confusion.
Copy link to clipboard
Copied
As far as I know, it cannot be done automatically
I'm sorry I can't help you.
Copy link to clipboard
Copied
Yes you were wrong. What you wanted was a resize and aspect ratio change. Photoshop does have a feature to do that it call content aware resize. However that will distort the image some. That is why I use fit image to resize the image without adding distortion then I added canvas to add borders to change image to have the 3:4 aspect ratio you wanted. 900:1200 = 9:12 = 3:4 the image you posted was 902x2339 a much narrower aspect ratio.
Copy link to clipboard
Copied
You could batch the action and there would be no interaction needed.
Copy link to clipboard
Copied
As JJMack said, an action can easily do this:
However as you ask for a script, my minor “contribution” would be to simply trim white space based on the upper left pixel, fit image and change canvas size:
// Trim White Space & Fit Image & Resize to 900x1200px
// Mostly based on a script posted here: https://stackoverflow.com/questions/17580923/photoshop-javascript-to-resize-image-and-canvas-to-spec...
// https://coffeeshopped.com/2008/11/conditional-image-resizing-with-photoshop-and-javascript
// https://forums.adobe.com/thread/2603328
#target photoshop
// Get a reference to the current (active) document and store it in a variable named "doc"
doc = app.activeDocument;
// Store the current ruler units for restoration
var savedRuler = app.preferences.rulerUnits;
// Set the ruler units to pixels
app.preferences.rulerUnits = Units.PIXELS;
// Trim based on the upper left pixel which is presumed to be pure white
doc.trim(TrimType.TOPLEFT)
// These are our values for the END RESULT width and height (in pixels) of our image
var fWidth = 900;
var fHeight = 1200;
// Do the resizing. if height > width (portrait-mode) resize based on height. otherwise, resize based on width
if (doc.height > doc.width) {
doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
}
else {
doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}
// Save the original background color
var savedBackgroundColor = app.backgroundColor;
// Makes the default background white
var white = new SolidColor();
white.rgb.hexValue = "FFFFFF";
app.backgroundColor = white;
// Convert the canvas size as informed above for the END RESULT
app.activeDocument.resizeCanvas(UnitValue(fWidth,"px"),UnitValue(fHeight,"px"));
// Restore the original background color
app.backgroundColor = savedBackgroundColor
// Restore the saved ruler units
app.preferences.rulerUnits = savedRuler;
Copy link to clipboard
Copied
Yes a script can do what *I think* you want. Totally possible. However, the thread is a bit confusing about 1. what you start with, and 2. what you want to end up with exactly.
I have made scripts for clients who deal in similar images. Here is how most of them work:
First off there is a path for the model. This really helps. Usually "Path 1" is the primary subject path. Having this makes it easy to automate positioning the model centered and the shoe a defined distance from the bottom.
For an example, let's say the image has "Path 1" (outline of the model), and the desired result is 900x1200, model centered and shoe 50px from bottom.
The only part a script doesn't know is where to crop at top. Clients of mine crop across the neck. Looks like you want to crop just below the eyes.
How my clients and I have solved this is to have retouch operators add a ruler guide where they want the top crop.
Given all those conditions met, here is what a script can do:
Get the first guide and get its Y position.
Crop the image using...
the guide value for top
Path 1 bottom bounds for bottom
Path 1 left and right bounds for left and right.
Scale image so it is 1150px tall. Then anchored top, add 50px to bottom of canvas. Now it's 1200 tall, shoe 50px from bottom.
Anchored center, resize canvas to 900 wide (centers the model).
Does this sound like what you are trying to accomplish? I ask before I post code to avoid wasting time if I've missed the mark and don't correctly understand what you're trying to do.
Copy link to clipboard
Copied
Here is a script that does what I described in the last message. It requires a path for the subject (currently "Path 1", name editable on line 15) and at least one horizontal guide to indicate top crop. Values for result (width, height, bottom margin) can be adjusted on lines 12-14.
Code below, or download here:
https://www.marspremedia.com/software/download?asset=crop-model.zip
Also note: the script could have more error protection (perhaps I'll add some when time allows).
At this point, two possible errors are:
1. there are no horizontal guides set.
2. the subject path doesn't exist (there is none or it's named different).
If the script throws an error, first check a horizontal guide is set and the subject path name matches what the script expects to find.
// Crop Model.jsx
#target photoshop
var b = []; // cropping bounds
var doc;
var guides = [];
var i;
var isBackgroundLayer;
var layerBottom;
var path;
var visible;
// Target values for result (in pixels).
// Adjust these values as desired.
var w = 900; // width
var h = 1200; // height
var m = 50; // bottom margin
var pathSubject = "Path 1"; // crop to this path name
if (!app.documents.length) {
alert("Open a document.");
return;
}
doc = app.activeDocument;
// Create array of horizontal guides, sorted from top to bottom.
for (i = 0; i < doc.guides.length; i++) {
if (doc.guides[i].direction === Direction.HORIZONTAL) {
guides.push(parseInt(doc.guides[i].coordinate, 10));
}
}
guides.sort(function (a, b) {
return (a > b) ? 1 : ((a < b) ? -1 : 0);
});
app.preferences.rulerUnits = Units.PIXELS;
// Ensure bottom layer is a layer, not background.
// (to preserve pixels while resizing canvas)
//
// Get bottom layer.
layerBottom = doc.layers[doc.layers.length - 1];
// Preserve 'visible' property to reset when done.
visible = layerBottom.visible;
// Preserve 'isBackgroundLayer' property to reset when done.
isBackgroundLayer = layerBottom.isBackgroundLayer;
// Make bottom layer a layer, not background.
layerBottom.isBackgroundLayer = false;
// SET BOUNDS OF DESIRED CROPPING
// Get subject path.
path = doc.pathItems.getByName(pathSubject);
// Make selection of subject path.
path.makeSelection(0, true, SelectionType.REPLACE);
// Copy selection bounds to b (cropping bounds).
b = doc.selection.bounds;
// Update top bounds to position of highest guide.
b[1] = guides[0];
// Deselect.
doc.selection.deselect();
// 'b' array now set to desired cropping.
// ( array = [left, top, right, bottom] )
// CROP IMAGE
// Using resize canvas to preserve pixels outside bounds.
//
// Resize canvas to crop out above first horizontal guide.
doc.resizeCanvas(null, doc.height - b[1], AnchorPosition.BOTTOMCENTER);
// Resize canvas to crop out below subject.
doc.resizeCanvas(null, b[3] - b[1], AnchorPosition.TOPCENTER);
// Resize canvas to crop out left of subject.
doc.resizeCanvas(doc.width - b[0], null, AnchorPosition.MIDDLERIGHT);
// Resize canvas to crop out right of subject.
doc.resizeCanvas(b[2] - b[0], null, AnchorPosition.MIDDLELEFT);
// RESIZE IMAGE
// Target height less bottom margin.
doc.resizeImage(null, h - m, null, ResampleMethod.AUTOMATIC);
// Resize canvas height to add bottom margin.
// At same time resize width to center subject.
doc.resizeCanvas(w, h, AnchorPosition.TOPCENTER);
// Restore original bottom layer properties.
layerBottom.isBackgroundLayer = isBackgroundLayer;
layerBottom.visible = visible;