Skip to main content
Known Participant
February 7, 2025
Answered

script set contrast value

  • February 7, 2025
  • 2 replies
  • 1204 views

hi,

i use the following code to create the brightness/contrast layer

https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-automate-photoshop-s-quot-auto-quot-brightness-contrast-functionality-on-a-batch-of-images/m-p/13890434

Code: 

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

///////////////////////

 

but the contrast value often makes the image too contrasty, i want to set the contrast value = 0,

please give me the command to do it.

 

Correct answer Ghoul Fool

Ok this it a bit of a hack, but it'll do: it will add an auto contrast adjustment layer - set the values and then set contrast to 0 regardless.

 

auto_brightness();
set_contrast_to_zero();

function auto_brightness()
{
    var d = new ActionDescriptor();
    var r = new ActionReference();
    r.putClass(stringIDToTypeID("adjustmentLayer"));
    d.putReference(stringIDToTypeID("null"), r);
    var d1 = new ActionDescriptor();
    var d2 = new ActionDescriptor();
    d2.putBoolean(stringIDToTypeID("auto"), true);
    d2.putBoolean(stringIDToTypeID("useLegacy"), false);
    d1.putObject(stringIDToTypeID("type"), stringIDToTypeID("brightnessEvent"), d2);
    d.putObject(stringIDToTypeID("using"), stringIDToTypeID("adjustmentLayer"), d1);
    executeAction(stringIDToTypeID("make"), d, DialogModes.NO);
}

function set_contrast_to_zero()
{
        // =======================================================
        var idsetd = charIDToTypeID( "setd" );
        var desc1848 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
        var ref354 = new ActionReference();
        var idAdjL = charIDToTypeID( "AdjL" );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref354.putEnumerated( idAdjL, idOrdn, idTrgt );
        desc1848.putReference( idnull, ref354 );
        var idT = charIDToTypeID( "T   " );
        var desc1849 = new ActionDescriptor();
        // var idBrgh = charIDToTypeID( "Brgh" );
        // desc1849.putInteger( idBrgh, 0 );
        var idCntr = charIDToTypeID( "Cntr" );
        desc1849.putInteger( idCntr, 0 );
        var iduseLegacy = stringIDToTypeID( "useLegacy" );
        desc1849.putBoolean( iduseLegacy, false );
        var idBrgC = charIDToTypeID( "BrgC" );
        desc1848.putObject( idT, idBrgC, desc1849 );
        executeAction( idsetd, desc1848, DialogModes.NO );
}

2 replies

Inspiring
February 7, 2025

You can add a Brightness and Contrast adjustment layer with this function;

 

 

brightness_contrast(50, 0, false);

// function  brightnessAndContrast (brightness, contrast)
// --------------------------------------------------------
function brightness_contrast(b, c, legacy)
{
  if(legacy == undefined) legacy = false;

  // create new Brightness contrast adjustment layer
  // =======================================================
  var idMk = charIDToTypeID( "Mk  " );
  var desc834 = new ActionDescriptor();
  var idnull = charIDToTypeID( "null" );
  var ref206 = new ActionReference();
  var idAdjL = charIDToTypeID( "AdjL" );
  ref206.putClass( idAdjL );
  desc834.putReference( idnull, ref206 );
  var idUsng = charIDToTypeID( "Usng" );
  var desc835 = new ActionDescriptor();
  var idType = charIDToTypeID( "Type" );
  var desc836 = new ActionDescriptor();
  var iduseLegacy = stringIDToTypeID( "useLegacy" );
  desc836.putBoolean( iduseLegacy, false );
  var idBrgC = charIDToTypeID( "BrgC" );
  desc835.putObject( idType, idBrgC, desc836 );
  var idAdjL = charIDToTypeID( "AdjL" );
  desc834.putObject( idUsng, idAdjL, desc835 );
  executeAction( idMk, desc834, DialogModes.NO );

    // =======================================================
    var idsetd = charIDToTypeID( "setd" );
    var desc826 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
    var ref201 = new ActionReference();
    var idAdjL = charIDToTypeID( "AdjL" );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref201.putEnumerated( idAdjL, idOrdn, idTrgt );
    desc826.putReference( idnull, ref201 );
    var idT = charIDToTypeID( "T   " );
    var desc827 = new ActionDescriptor();
    var idBrgh = charIDToTypeID( "Brgh" );
    desc827.putInteger( idBrgh, b ); //brightness
    var idCntr = charIDToTypeID( "Cntr" );
    desc827.putInteger( idCntr, c ); //Contrast
    var iduseLegacy = stringIDToTypeID( "useLegacy" );
    desc827.putBoolean( iduseLegacy, legacy );
    var idBrgC = charIDToTypeID( "BrgC" );
    desc826.putObject( idT, idBrgC, desc827 );
    executeAction( idsetd, desc826, DialogModes.NO );

}

 

Known Participant
February 7, 2025

thank @Ghoul Fool 

At the moment, this code runs exactly as i want but unfortunately i dont know how to program in photoshop so this program is the result of patching together parts of code. i think it is not optimized.

 

Goal:
Apply auto brightness from "auto" Brightness/Contrast function in photoshop but keep only brightness value and remove contrast value.

Step 1:
Run the code to create "auto" brightness/contrast as default (not legacy).
Step 2: Get brightness value and remove layer.
Step 3: Create new Brightness/Contrast layer with brightness value obtained in step 2 and contrast value set to 0.

 

Is there any way to set contrast value (set contrast = 0 or 1,2 ..) right after step 1?

 

CODE:

// STEP 1 - make Auto Brightness/contrast default
// 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("brightness="+adj.brightness); // shows undefined, even though the active layer is a Brightness/Contrast layer
        //alert("contrast="+adj.center); // shows undefined, even though the active layer is a Brightness/Contrast layer
 
// STEP 2 - remove first Brightness layer
var AutoBrightness1 = app.activeDocument.artLayers.getByName("Brightness/Contrast 1");
//AutoBrightness1.visible = false;
AutoBrightness1.remove();
 
// STEP 3 - Add new brightness layer with Brightness value and without Contrast value.
brightness_contrast(adj.brightness, 0, false);
 
        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); }
}
 
 
// function  brightnessAndContrast (brightness, contrast)
// --------------------------------------------------------
function brightness_contrast(b, c, legacy)
{
  if(legacy == undefined) legacy = false;
 
  // create new Brightness contrast adjustment layer
  // =======================================================
  var idMk = charIDToTypeID( "Mk  " );
  var desc834 = new ActionDescriptor();
  var idnull = charIDToTypeID( "null" );
  var ref206 = new ActionReference();
  var idAdjL = charIDToTypeID( "AdjL" );
  ref206.putClass( idAdjL );
  desc834.putReference( idnull, ref206 );
  var idUsng = charIDToTypeID( "Usng" );
  var desc835 = new ActionDescriptor();
  var idType = charIDToTypeID( "Type" );
  var desc836 = new ActionDescriptor();
  var iduseLegacy = stringIDToTypeID( "useLegacy" );
  desc836.putBoolean( iduseLegacy, false );
  var idBrgC = charIDToTypeID( "BrgC" );
  desc835.putObject( idType, idBrgC, desc836 );
  var idAdjL = charIDToTypeID( "AdjL" );
  desc834.putObject( idUsng, idAdjL, desc835 );
  executeAction( idMk, desc834, DialogModes.NO );
 
    // =======================================================
    var idsetd = charIDToTypeID( "setd" );
    var desc826 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
    var ref201 = new ActionReference();
    var idAdjL = charIDToTypeID( "AdjL" );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref201.putEnumerated( idAdjL, idOrdn, idTrgt );
    desc826.putReference( idnull, ref201 );
    var idT = charIDToTypeID( "T   " );
    var desc827 = new ActionDescriptor();
    var idBrgh = charIDToTypeID( "Brgh" );
    desc827.putInteger( idBrgh, b ); //brightness
    var idCntr = charIDToTypeID( "Cntr" );
    desc827.putInteger( idCntr, c ); //Contrast
    var iduseLegacy = stringIDToTypeID( "useLegacy" );
    desc827.putBoolean( iduseLegacy, legacy );
    var idBrgC = charIDToTypeID( "BrgC" );
    desc826.putObject( idT, idBrgC, desc827 );
    executeAction( idsetd, desc826, DialogModes.NO );
}

 

 

Known Participant
February 7, 2025

Ok this it a bit of a hack, but it'll do: it will add an auto contrast adjustment layer - set the values and then set contrast to 0 regardless.

 

auto_brightness();
set_contrast_to_zero();

function auto_brightness()
{
    var d = new ActionDescriptor();
    var r = new ActionReference();
    r.putClass(stringIDToTypeID("adjustmentLayer"));
    d.putReference(stringIDToTypeID("null"), r);
    var d1 = new ActionDescriptor();
    var d2 = new ActionDescriptor();
    d2.putBoolean(stringIDToTypeID("auto"), true);
    d2.putBoolean(stringIDToTypeID("useLegacy"), false);
    d1.putObject(stringIDToTypeID("type"), stringIDToTypeID("brightnessEvent"), d2);
    d.putObject(stringIDToTypeID("using"), stringIDToTypeID("adjustmentLayer"), d1);
    executeAction(stringIDToTypeID("make"), d, DialogModes.NO);
}

function set_contrast_to_zero()
{
        // =======================================================
        var idsetd = charIDToTypeID( "setd" );
        var desc1848 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
        var ref354 = new ActionReference();
        var idAdjL = charIDToTypeID( "AdjL" );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref354.putEnumerated( idAdjL, idOrdn, idTrgt );
        desc1848.putReference( idnull, ref354 );
        var idT = charIDToTypeID( "T   " );
        var desc1849 = new ActionDescriptor();
        // var idBrgh = charIDToTypeID( "Brgh" );
        // desc1849.putInteger( idBrgh, 0 );
        var idCntr = charIDToTypeID( "Cntr" );
        desc1849.putInteger( idCntr, 0 );
        var iduseLegacy = stringIDToTypeID( "useLegacy" );
        desc1849.putBoolean( iduseLegacy, false );
        var idBrgC = charIDToTypeID( "BrgC" );
        desc1848.putObject( idT, idBrgC, desc1849 );
        executeAction( idsetd, desc1848, DialogModes.NO );
}

thank you,

in the set_contrast function, how to get Brightness value?

c.pfaffenbichler
Community Expert
Community Expert
February 7, 2025

Comment out the line 

brightnessDescriptor.putBoolean(stringIDToTypeID("auto"), true);
Known Participant
February 7, 2025

no, I mean just change the value of contrast = 0, but keep the value of brightness the same.

Inspiring
February 7, 2025

 

From here you can adjust both the opacity and the contrast. 

app.activeDocument.activeLayer.adjustBrightnessContrast(-20, 80);