I’m trying to resize and crop several images from a folder. First of all, let’s see some parts of the script:
// DOCUMENT SETTINGS
app.preferences.rulerUnits = Units.MM;
app.displayDialogs = DialogModes.NO;
// FUNCTIONS
function processing_f_alta(folder, files, w, h) {
var f_alta = new Folder(folder + "/ALTA");
if ( ! f_alta.exists ) { f_alta.create(); }
for ( var cont = 0; cont < files.length; cont++ ) {
files[cont].copy(decodeURI(f_alta) + "/" + files[cont].displayName);
}
var files = f_alta.getFiles("*.tif");
for ( var cont = 0; cont < files.length; cont++ ) {
var img_file = app.open(files[cont]);
img_file.resizeImage(UnitValue(w, "cm"), UnitValue(h, "cm"), null, ResampleMethod.BICUBIC);
img_file.resizeCanvas(UnitValue(w, "cm"), UnitValue(h, "cm"), AnchorPosition.MIDDLECENTER);
img_file.close(SaveOptions.SAVECHANGES);
}
}
var w =prompt("Width (cm)","","Introduzca valor");
var h =prompt("Height (cm)","","Introduzca valor");
var f_origin_folder = Folder.selectDialog("Select the containing folder of the images");
var folder = new Folder(f_origin_folder);
var files = folder.getFiles("*.tif");
processing_f_alta(folder, files, w/2, h/2);
The script has much more code, but it’s irrelevant.
The idea is to get an hipothetic width (“w”) and height (“h”) from the keyboard and get the folder when the images are (“folder”). So, the script gets all the “.tif” files of this folder and saves them into the variable “files”.
the function processing_f_alta() is called with several params (folder, files, w/2, h/2). Why the last params are divided by 2 is irrelevant.
Into the function, the script creates a new folder into the “folder” called “ALTA” ant all the “.tif” files are copied into it. Then, the script gets all these last “.tif” files and resizes them to the new vales of with (w/2) and height (h/2).
EVERYTHING IS OK UNTIL HERE.
Now comes the problem. I want to crop the file with no distorsion but I don’t know how to do it.
Let’s see a real example (the example that I’m testing).
I’ve got an image of 40x40cm in a folder called “test”. I execute the script with these values: w=30, h=15, folder=”test”.
When I run the script I get a new folder into “test” called “ALTA” with an image resized of 15×7,5cm. THAT’S CORRECT. But when I open the file, it’s not been croped. It’s been deformed vertically. What I wanted to get is this result but with the image cropped vertically, and I get an image deformed.
I’ve tryed crop(), resizeCanvas() functions, but I am not able to get the result that I’m expecting.
Could you help me to solve my problem.
Thanks in advance for your time.
2
Answers
Now that works:
The next step will be cropping from the center of the image (now it does from the left-top point).
PD: I've done w*10 and h*10 because if original w=30 and h=30, for example, it crops the image to w->3 and h->3.
Try:
(also: I don’t have Photoshop on this machine so this is untested code, but basically you need to adjust the height first and then enlarge the width if it is smaller than what you want, before cropping the image)