Skip to main content
daniele75791357
Participating Frequently
July 21, 2016
Answered

Photoshop Script Crop Resizes Image, Does Not Crop

  • July 21, 2016
  • 2 replies
  • 3699 views

Recently I have been trying to make a script that will find the center of an image, and crop the desired width and height. I have tried a few different tricks, but have had no luck.

One thing I tried was settomg the bounds to be the full document

var bounds = [0,0,doc.width,doc.height];

And then I try to crop it by the desired dimensions with no angle, but the image is scaled down instead of being cropped.

doc.crop(bounds, undefined, desiredWidth, desiredHeight);

I have tried something to identify the center of the file and then set the dimensions by going to the left by half of the desired width, up by half the desired height, to the right by half the desired with, and then down half the desired height to make the area and then crop it.

var bounds = [(centerX-halfWidth),(centerY+halfHeight),(centerX+halfWidth),(centerY-halfHeight)]; 
doc
.crop(bounds);

When I try do his, I get a warning that I do not have enough RAM. I was unsure if this is simply due to the laptop, or because I am making a ridiculous request.

How exactly does the crop function work? And for the bounds property, do I need to set the amount I want cropped by making the dimensions here, or is that within the crop function?

Any help or clarification would be greatly appreciated.

This topic has been closed for replies.
Correct answer JJMack

Bounds Top  Left Bottrom Right....  (x,y)  so        Left Top  Right bottom

var orig_ruler_units = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

var dir = new Folder('~/Desktop/Test');

var files = dir.getFiles("*.jpg");

for (var i = 0; i < files.length; i++) {

 

    var doc = app.open(files);

    var desiredWidth = 842;

    var desiredHeight = 382;

    var halfWidth = (desiredWidth/2);

    var halfHeight = (desiredHeight/2);

    var centerX = (doc.width/2);

    var centerY = (doc.height/2);

    //var bounds = [(centerX-halfWidth),(centerY+halfHeight),(centerX+halfWidth),(centerY-halfHeight)];

   var bounds = [(centerX-halfWidth),(centerY-halfHeight),(centerX+halfWidth),(centerY+halfHeight)];

    doc.crop(bounds);

    var saveJPEG = new ExportOptionsSaveForWeb();

    saveJPEG.optimized = true;

    saveJPEG.quality = 50;

    saveJPEG.format = SaveDocumentType.JPEG;

      

    //Folder path for new images

    var jpegFolder = new Folder("~/Desktop/Test2");

 

    doc.exportDocument (jpegFolder, ExportType.SAVEFORWEB, saveJPEG);

    doc.close(SaveOptions.DONOTSAVECHANGES);

}

app.preferences.rulerUnits = orig_ruler_units;

2 replies

daniele75791357
Participating Frequently
July 25, 2016

Full code:

var dir = new Folder('/c/Users/dellman/Desktop/Test');

var files = dir.getFiles("*.jpg");

app.preferences.rulerUnits = Units.PIXELS;

//app.preferences.typeUnits = Units.PIXELS;

for (var i = 0; i < files.length; i++) {

   

        var doc = app.open(files);

        var desiredWidth = 842;

        var desiredHeight = 382;

        var halfWidth = (desiredWidth/2);

        var halfHeight = (desiredHeight/2);

        var centerX = (doc.width/2);

        var centerY = (doc.height/2);

        //var bounds = [0,0,doc.width,doc.height];

        var bounds = [(centerX-halfWidth),(centerY+halfHeight),(centerX+halfWidth),(centerY-halfHeight)];

        $.writeln(bounds);

        //doc.crop(bounds, 0, desiredWidth, desiredHeight);

        //var bounds = [0,0,doc.width,doc.height];

        //doc.crop(bounds, undefined, desiredWidth, desiredHeight);

        doc.crop(bounds);

         var saveJPEG = new ExportOptionsSaveForWeb();

         saveJPEG.optimized = true;

         saveJPEG.quality = 50;

         saveJPEG.format = SaveDocumentType.JPEG;

        

         //Folder path for new images

         var jpegFolder = new Folder("/c/Users/dellman/Desktop/Test2");

   

        doc.exportDocument (jpegFolder, ExportType.SAVEFORWEB, saveJPEG);

        doc.close(SaveOptions.DONOTSAVECHANGES);

}

JJMack
Community Expert
JJMackCommunity ExpertCorrect answer
Community Expert
July 25, 2016

Bounds Top  Left Bottrom Right....  (x,y)  so        Left Top  Right bottom

var orig_ruler_units = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

var dir = new Folder('~/Desktop/Test');

var files = dir.getFiles("*.jpg");

for (var i = 0; i < files.length; i++) {

 

    var doc = app.open(files);

    var desiredWidth = 842;

    var desiredHeight = 382;

    var halfWidth = (desiredWidth/2);

    var halfHeight = (desiredHeight/2);

    var centerX = (doc.width/2);

    var centerY = (doc.height/2);

    //var bounds = [(centerX-halfWidth),(centerY+halfHeight),(centerX+halfWidth),(centerY-halfHeight)];

   var bounds = [(centerX-halfWidth),(centerY-halfHeight),(centerX+halfWidth),(centerY+halfHeight)];

    doc.crop(bounds);

    var saveJPEG = new ExportOptionsSaveForWeb();

    saveJPEG.optimized = true;

    saveJPEG.quality = 50;

    saveJPEG.format = SaveDocumentType.JPEG;

      

    //Folder path for new images

    var jpegFolder = new Folder("~/Desktop/Test2");

 

    doc.exportDocument (jpegFolder, ExportType.SAVEFORWEB, saveJPEG);

    doc.close(SaveOptions.DONOTSAVECHANGES);

}

app.preferences.rulerUnits = orig_ruler_units;

JJMack
daniele75791357
Participating Frequently
July 26, 2016

Ah, the Y value goes up farther down the page, so you would subtract the value to get the top instead of adding it.

This is helping me get closer to what I am trying to achieve. It is not perfect yet, but that small error should help me get closer to perfecting it.

JJMack
Community Expert
Community Expert
July 21, 2016

An action or action manages code should do set the units of choice. Use Percent, Pixels, or some unit size that is relative to resolution like  5"x7"

JJMack
daniele75791357
Participating Frequently
July 21, 2016

I'm trying to do this as a jsx file, not an action. I am also trying to do it by using the W x H Resolution Crop option.

The goal is to have the user choose the dimensions after running the script to affect a folder full of images, and to have them all cropped from the center by their desired dimensions. The picture should end up cropped with just those dimensions in place.

JJMack
Community Expert
Community Expert
July 22, 2016

Do you know anything about the Scriptlistener plug-in and action manager script code? If you do not you should look into it.  You will find information about it in Adobe's scripting documentation,

You can always calculate the bounds you want in a script set a selection and crop your document using script  DOM code.  If you use DOM code or Action manger code it still a script that crops your document the way you want to crop it..

Without Action manager code Photoshop scripting would be severely limited because Adobe Photoshop DOM does not cover all of Photoshop features.  With Action manager code in a script you can do all an Action can do and add logic into the process. That Actions can not do. This makes Adobe's Photoshop scripting feature more powerful than Adobe Photoshop Actions feature.

Your Process could also  be done using the Image Processor script or Image processor Pro Plug-in script and have them include a simple cropping action in processing output files.  It is not hard to do.

Yes resize image does not corp the image  it resizes the image document.It can also distort the image in the process.

Image crop should crop your image.   Photoshop's Cropping tool can also resample the crop to some resolution.  Photoshop's Crop tool can crop and  resize.

JJMack