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

Need script to crop to path without deleting pixels

Community Beginner ,
Sep 12, 2021 Sep 12, 2021

Copy link to clipboard

Copied

Hi All,

I need some help finding a script that I'm sure must be out there (or easy to create for those who actually know hot to write scripts..)
I'm trying to batch crop multiple photos (PSDs) from a path that is already in the files (named "Crop (Capture One)".

With actions I can only go so far, because:

-When converting path to selection and using crop tool, the crop tool records specific XY coordinates, not the path coordinates.

-Using Image/Crop makes the right crop but deletes the pixels outside the crop area.

I would like to find a way to directly crop according to the path, without deleting the pixels outside the crop zone...

 

Any Ideas out there?

Thanks a million,

J

TOPICS
Actions and scripting

Views

659

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 , Sep 12, 2021 Sep 12, 2021

Try here:

 

Crop to Selection Action

 

 

@jog16137815 

 

Here is the full code:

 

/*
Crop to Named Path Retaining Pixels.jsx
v1.1, Stephen Marsh, 13th September 2021

https://community.adobe.com/t5/photoshop-ecosystem-discussions/need-script-to-crop-to-path-without-deleting-pixels/td-p/12378330
Need script to crop to path without deleting pixels
*/

if (app.documents.length) {
    var doc = app.activeDocument;
    var savedRuler = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Unit
...

Votes

Translate

Translate
Adobe
Community Expert ,
Sep 12, 2021 Sep 12, 2021

Copy link to clipboard

Copied

Try here:

 

Crop to Selection Action

 

 

@jog16137815 

 

Here is the full code:

 

/*
Crop to Named Path Retaining Pixels.jsx
v1.1, Stephen Marsh, 13th September 2021

https://community.adobe.com/t5/photoshop-ecosystem-discussions/need-script-to-crop-to-path-without-deleting-pixels/td-p/12378330
Need script to crop to path without deleting pixels
*/

if (app.documents.length) {
    var doc = app.activeDocument;
    var savedRuler = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;
    try {
        doc.pathItems.getByName("Crop (Capture One)");
    } catch (e) {
        alert('There is no path named "Crop (Capture One)"');
    }
    loadCropPath("Crop (Capture One)");
    try {
        var bound = doc.selection.bounds;
        cropToSelection(bound[1], bound[0], bound[3], bound[2], false); // delete cropped pixels
    } catch (e) {
        alert('There was an unexpected error when cropping');
    }
    app.preferences.rulerUnits = savedRuler;
} else {
    alert('A document must be open to use this script!');
}

/******************** FUNCTIONS ********************/

// Courtesy of Chuck Uebele
function cropToSelection(top, left, bottom, right, retainPixels) {
    var idCrop = charIDToTypeID("Crop");
    var desc11 = new ActionDescriptor();
    var idT = charIDToTypeID("T   ");
    var desc12 = new ActionDescriptor();
    var idTop = charIDToTypeID("Top ");
    var idPxl = charIDToTypeID("#Pxl");
    desc12.putUnitDouble(idTop, idPxl, top);
    var idLeft = charIDToTypeID("Left");
    var idPxl = charIDToTypeID("#Pxl");
    desc12.putUnitDouble(idLeft, idPxl, left);
    var idBtom = charIDToTypeID("Btom");
    var idPxl = charIDToTypeID("#Pxl");
    desc12.putUnitDouble(idBtom, idPxl, bottom);
    var idRght = charIDToTypeID("Rght");
    var idPxl = charIDToTypeID("#Pxl");
    desc12.putUnitDouble(idRght, idPxl, right);
    var idRctn = charIDToTypeID("Rctn");
    desc11.putObject(idT, idRctn, desc12);
    var idAngl = charIDToTypeID("Angl");
    var idAng = charIDToTypeID("#Ang");
    desc11.putUnitDouble(idAngl, idAng, 0.000000);
    var idDlt = charIDToTypeID("Dlt ");
    desc11.putBoolean(idDlt, retainPixels); // delete cropped pixels = true | false
    var idcropAspectRatioModeKey = stringIDToTypeID("cropAspectRatioModeKey");
    var idcropAspectRatioModeClass = stringIDToTypeID("cropAspectRatioModeClass");
    var idtargetSize = stringIDToTypeID("targetSize");
    desc11.putEnumerated(idcropAspectRatioModeKey, idcropAspectRatioModeClass, idtargetSize);
    executeAction(idCrop, desc11, DialogModes.NO);
}

function loadCropPath(pathName) {
    var idset = stringIDToTypeID("set");
    var desc1606 = new ActionDescriptor();
    var idnull = stringIDToTypeID("null");
    var ref418 = new ActionReference();
    var idchannel = stringIDToTypeID("channel");
    var idselection = stringIDToTypeID("selection");
    ref418.putProperty(idchannel, idselection);
    desc1606.putReference(idnull, ref418);
    var idto = stringIDToTypeID("to");
    var ref419 = new ActionReference();
    var idpath = stringIDToTypeID("path");
    ref419.putName(idpath, pathName); // path name
    desc1606.putReference(idto, ref419);
    var idversion = stringIDToTypeID("version");
    desc1606.putInteger(idversion, 1);
    var idvectorMaskParams = stringIDToTypeID("vectorMaskParams");
    desc1606.putBoolean(idvectorMaskParams, true);
    executeAction(idset, desc1606, DialogModes.NO);
}

 

Downloading and Installing Adobe Scripts

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 Beginner ,
Sep 12, 2021 Sep 12, 2021

Copy link to clipboard

Copied

Amazing!

A million thanks, I was looking for this since for ever, you're a saver 🙂

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 ,
Sep 12, 2021 Sep 12, 2021

Copy link to clipboard

Copied

Your welcome!

 

EDIT: Original v1.0 code slightly modified to include error checking for target path.

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 ,
Nov 11, 2021 Nov 11, 2021

Copy link to clipboard

Copied

LATEST
quote

Need script to crop 300 Dpi  to path without deleting pixels (without pixel change 300 DPI crop ) pliz Help

 

 

sr

script to crop  to path without deleting pixels

only convert 72 resolution to 300 Dpi

help me sr


By @Nir Photo Art

 

 

I'm not following... What part of the script posted above would you like to modify for your specific needs?

 

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