• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Photoshop Script Crop Resizes Image, Does Not Crop

New Here ,
Jul 21, 2016 Jul 21, 2016

Copy link to clipboard

Copied

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.

TOPICS
Actions and scripting

Views

3.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 25, 2016 Jul 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.he

...

Votes

Translate

Translate
Adobe
Community Expert ,
Jul 21, 2016 Jul 21, 2016

Copy link to clipboard

Copied

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"

Capture.jpg

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 21, 2016 Jul 21, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 21, 2016 Jul 21, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 21, 2016 Jul 21, 2016

Copy link to clipboard

Copied

Like JJ said, if your desiredWidth/Height is just a number, it's missing the type. Most Photoshop methods take sizes as UnitValue, which is a utility class that can handle a the unit and conversions. If you give a number where UnitValue is needed, it implicitly converts using default value. So you can either

  1. Explicitly convert to UnitValue like desiredWidth = UnitValue(desiredWidth, "px")
  2. Set the default unit
    ​
    app.preferences.rulerUnits = Units.PIXELS
    app.preferences.typeUnits = TypeUnits.PIXELS




Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

I already had the preferences set to pixels. The image came out with the proper dimensions (the ones I specified as pixels), but it was simply resized instead of cropping a section from the larger image.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

If somehow you are using the Photoshop CROP Tool it can resize (resample) the crop.    So if set the crop tool to resample to a particular size.  If you select the entire image it will be resized to the size you set.

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

So how can I set the crop method for the script to actually crop the image and cut that part out instead of resizing it? Is that a different method or not possible?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

Are you using ScriptListener action manager code for Photoshop Crop tool?  If so you may be selection the Whole image.

Image Crop should not resample. Post the Code you using.

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

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);

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines