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

How to get Brightness/Contrast levels of a layer, using .jsx Adobe Photoshop scripting?

Explorer ,
Jun 24, 2023 Jun 24, 2023

Copy link to clipboard

Copied

I have a .jsx Photoshop script workflow that begins by first applying a Brightness/Contrast adjustment to the current image, then uses the "Auto" setting.

 

For the next step, I'd like to get the properties of that Brightness/Contrast layer, and then IF the brightness meets a certain criteria? Change the brightness to some other value. Same for the contrast.

 

What's the best way to automate this in my .jsx file?

 

Thanks!

TOPICS
Actions and scripting , Windows

Views

265

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
Adobe
Community Expert ,
Jun 25, 2023 Jun 25, 2023

Copy link to clipboard

Copied

You should be able to get the Layer’s settings via AM-code. 

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 ,
Jun 25, 2023 Jun 25, 2023

Copy link to clipboard

Copied

@c.pfaffenbichler - Easier said than done, at least for me!

 

 

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
Explorer ,
Jun 25, 2023 Jun 25, 2023

Copy link to clipboard

Copied

Any idea what that code would look like? I tried for HOURS yesterday and just couldn't get it to work. Kept throwing errors. Here's one I tried that outputs a Photoshop general error:

    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var desc = executeActionGet(ref);
    var adjs = desc.getList(charIDToTypeID("Adjs"));
    var brConDesc = adjs.getObjectValue(0);
    var brightness = brConDesc.getInteger(charIDToTypeID("Brgh"));
    var contrast = brConDesc.getInteger(charIDToTypeID("Cntr"));
   
    alert("Brightness: " + brightness + "\nContrast: " + contrast);

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
Valorous Hero ,
Jun 25, 2023 Jun 25, 2023

Copy link to clipboard

Copied

quote

Any idea what that code would look like? I tried for HOURS yesterday and just couldn't get it to work. Kept throwing errors.

 
By @antond99983697

 

It would be better if you looked at the topics nearby or used the search.

Untitled-1.jpg

 

 

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
Explorer ,
Jun 25, 2023 Jun 25, 2023

Copy link to clipboard

Copied

adj.brightness is showing up as undefined when I run this function. Not sure why because the active layer should be a Brightness/Contrast layer.

    // Apply auto brightness/contrast adjustment
    var autoDescriptor = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putClass(stringIDToTypeID("adjustmentLayer"));
    autoDescriptor.putReference(stringIDToTypeID("null"), reference);
    var typeDescriptor = new ActionDescriptor();
    var brightnessDescriptor = new ActionDescriptor();
    brightnessDescriptor.putBoolean(stringIDToTypeID("auto"), true);
    brightnessDescriptor.putBoolean(stringIDToTypeID("useLegacy"), false);
    typeDescriptor.putObject(stringIDToTypeID("type"), stringIDToTypeID("brightnessEvent"), brightnessDescriptor);
    autoDescriptor.putObject(stringIDToTypeID("using"), stringIDToTypeID("adjustmentLayer"), typeDescriptor);
    var resultDesc = executeAction(stringIDToTypeID("make"), autoDescriptor, DialogModes.NO);

    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt')); // reference is active layer
    var layerDesc = executeActionGet(ref);
    var layerID = layerDesc.getInteger(stringIDToTypeID('layerID'));

    // Use the function you found to get the brightness descriptor
    var brConDesc = get_contrast_desc(layerID);
 
function get_contrast_desc(id) {
    try {
        var r = new ActionReference();
        var d = new ActionDescriptor();

        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("json"));
        r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        d.putReference(stringIDToTypeID("null"), r);

        d.putInteger(stringIDToTypeID("layerID"), id);
        d.putBoolean(stringIDToTypeID("layerInfo"), true);

        d.putBoolean(stringIDToTypeID( "includeAncestors" ), false);

        eval("var json="+executeAction(stringIDToTypeID("get"), d, DialogModes.NO).getString(stringIDToTypeID("json")));

        var d = new ActionDescriptor();

        if (!json.layers) return d;
        if (json.layers.length != 1) return d;
        if (json.layers[0].id != id) return d;

        var adj = json.layers[0].adjustment;
        alert(adj.brightness); // shows undefined, even though the active layer is a Brightness/Contrast layer

        if (!adj) return d;

        if (adj.brightness != undefined) d.putInteger(stringIDToTypeID("brightness"), adj.brightness);
        if (adj.center     != undefined) d.putInteger(stringIDToTypeID("center"),     adj.center);
        if (adj.useLegacy  != undefined) d.putBoolean(stringIDToTypeID("useLegacy"),  adj.useLegacy);

        return d;
        }
    catch (e) { throw(e); }
}

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
Valorous Hero ,
Jun 25, 2023 Jun 25, 2023

Copy link to clipboard

Copied

LATEST
Yes, cool. Photoshop never ceases to please with its bugs. This is all due to the use of the "Auto" button.
Here is a crutch that fixes a bug.
 

 

// Apply auto brightness/contrast adjustment
    var autoDescriptor = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putClass(stringIDToTypeID("adjustmentLayer"));
    autoDescriptor.putReference(stringIDToTypeID("null"), reference);
    var typeDescriptor = new ActionDescriptor();
    var brightnessDescriptor = new ActionDescriptor();
    brightnessDescriptor.putBoolean(stringIDToTypeID("auto"), true);
    brightnessDescriptor.putBoolean(stringIDToTypeID("useLegacy"), false);
    typeDescriptor.putObject(stringIDToTypeID("type"), stringIDToTypeID("brightnessEvent"), brightnessDescriptor);
    autoDescriptor.putObject(stringIDToTypeID("using"), stringIDToTypeID("adjustmentLayer"), typeDescriptor);
    var resultDesc = executeAction(stringIDToTypeID("make"), autoDescriptor, DialogModes.NO);

    ///////////////////////////////////////////////////////////////////////////////////////////
    // idle correction change so that photoshop removes the "auto" parameter from the adjustment object.
    var d = new ActionDescriptor();
    var r = new ActionReference();
    r.putEnumerated(stringIDToTypeID("adjustmentLayer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
    d.putReference(stringIDToTypeID("null"), r);
    var data = new ActionDescriptor();
    d.putObject(stringIDToTypeID("to"), stringIDToTypeID("brightnessEvent"), data);
    executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
    ///////////////////////////////////////////////////////////////////////////////////////////


    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt')); // reference is active layer
    var layerDesc = executeActionGet(ref);
    var layerID = layerDesc.getInteger(stringIDToTypeID('layerID'));

    // Use the function you found to get the brightness descriptor
    var brConDesc = get_contrast_desc(layerID);
 
function get_contrast_desc(id) {
    try {
        var r = new ActionReference();
        var d = new ActionDescriptor();

        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("json"));
        r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        d.putReference(stringIDToTypeID("null"), r);

        d.putInteger(stringIDToTypeID("layerID"), id);
        d.putBoolean(stringIDToTypeID("layerInfo"), true);

        d.putBoolean(stringIDToTypeID( "includeAncestors" ), false);

        eval("var json="+executeAction(stringIDToTypeID("get"), d, DialogModes.NO).getString(stringIDToTypeID("json")));

        var d = new ActionDescriptor();

        if (!json.layers) return d;
        if (json.layers.length != 1) return d;
        if (json.layers[0].id != id) return d;

        var adj = json.layers[0].adjustment;
        alert(adj.brightness); // shows undefined, even though the active layer is a Brightness/Contrast layer

        if (!adj) return d;

        if (adj.brightness != undefined) d.putInteger(stringIDToTypeID("brightness"), adj.brightness);
        if (adj.center     != undefined) d.putInteger(stringIDToTypeID("center"),     adj.center);
        if (adj.useLegacy  != undefined) d.putBoolean(stringIDToTypeID("useLegacy"),  adj.useLegacy);

        return d;
        }
    catch (e) { throw(e); }
}

 

 

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