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

script set contrast value

Explorer ,
Feb 06, 2025 Feb 06, 2025

Copy link to clipboard

Copied

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...

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.

 

TOPICS
Actions and scripting

Views

371
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 2 Correct answers

Explorer , Feb 07, 2025 Feb 07, 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 n

...

Votes

Translate
Engaged , Feb 07, 2025 Feb 07, 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("
...

Votes

Translate
Adobe
Community Expert ,
Feb 06, 2025 Feb 06, 2025

Copy link to clipboard

Copied

Comment out the line 

brightnessDescriptor.putBoolean(stringIDToTypeID("auto"), true);

Votes

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 ,
Feb 07, 2025 Feb 07, 2025

Copy link to clipboard

Copied

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

set contrast to 0expand image

Votes

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
Engaged ,
Feb 07, 2025 Feb 07, 2025

Copy link to clipboard

Copied

 

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

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

 

Votes

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 ,
Feb 07, 2025 Feb 07, 2025

Copy link to clipboard

Copied

Can I use a command to change the contrast value in the contrast value input field of the Brightness/Contrast control panel?

Votes

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
Engaged ,
Feb 07, 2025 Feb 07, 2025

Copy link to clipboard

Copied

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

}

 

Votes

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 ,
Feb 07, 2025 Feb 07, 2025

Copy link to clipboard

Copied

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 );
}
2.PNGexpand image

 

 

Votes

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
Engaged ,
Feb 07, 2025 Feb 07, 2025

Copy link to clipboard

Copied

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

Votes

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 ,
Feb 07, 2025 Feb 07, 2025

Copy link to clipboard

Copied

thank you,

in the set_contrast function, how to get Brightness value?

Votes

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
Engaged ,
Feb 10, 2025 Feb 10, 2025

Copy link to clipboard

Copied

Sorry, I should have explained in the initial function 

brightness_contrast(50, 0, false);

The first parameter is brightness, the second is contrast the third is optional and is legacy mode

Votes

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 ,
Feb 10, 2025 Feb 10, 2025

Copy link to clipboard

Copied

i mean, i want to get the value of brightness.
i tried getting the value this way but it didn't work
var brightness desc1849.brightness;
var brightness desc1849.Brgh;

it returns undefined

Votes

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 ,
Feb 10, 2025 Feb 10, 2025

Copy link to clipboard

Copied

Could you please post screenshots with the pertinent Panels (Toolbar, Layers, Properties, Options Bar, …) visible to clarify what the Layer situation is? 

Votes

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 ,
Feb 10, 2025 Feb 10, 2025

Copy link to clipboard

Copied

To get the values of an existing Brightness/Contrast Layer you can try this: 

// check brightness/contrast composite settings;
// based on code by michael l hale;
// 2025, use it at your own risk;
if (app.documents.length > 0) {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var layerDesc = executeActionGet(ref);
try {
var aList = layerDesc.getList(stringIDToTypeID("adjustment"));
var bright = aList.getObjectValue(0).getInteger(stringIDToTypeID("brightness"));
var center = aList.getObjectValue(0).getInteger(stringIDToTypeID("center"));
var useLegacy = aList.getObjectValue(0).getBoolean(stringIDToTypeID("useLegacy"));
alert ("brightness/contrast\nbrightness: "+bright+"\ncenter: "+center+"\nlegacy: "+useLegacy
)
} catch (e) {
alert ("either not a brightness/contrast adjustment layer or at unedited default settings")
}
};

Votes

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 ,
Feb 10, 2025 Feb 10, 2025

Copy link to clipboard

Copied

LATEST

thank you, it's works.

Votes

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