Copy link to clipboard
Copied
Hello everyone,
I’m currently looking to write a script in Photoshop that would automatically adjust the brightness of an image so that its histogram matches a standard histogram. The idea is for the script to either increase or decrease the overall brightness until the current histogram resembles the predefined target.
If anyone has experience with scripting for histogram adjustments in Photoshop or can provide some guidance, I would greatly appreciate it! I’ve attached an image for reference.
Thanks in advance for your help!
Copy link to clipboard
Copied
Or you can contact me via email: <email address removed>
I am willing to pay for this.
Copy link to clipboard
Copied
If your predefined highlight/shadow clipping threshold can be expressed as a percentage, it might be possible to achieve it by customizing Auto Levels values. Then it wouldn’t even need to be scripted, it could be automated by incorporating it into a batch Action. Even if you require a script, the fact that this is a part of the Auto Levels feature means it might be accessible by scripting. (I do not have scripting expertise, so someone else will need to figure out that part.)
How Auto Levels works is described in this article by Bruce Fraser:
Copy link to clipboard
Copied
Thanks for your support, your information is very good.
But my problem is to automatically adjust in all cases of over or under light to the standard graph (in the highlight area).
I tried but it did not work.
Copy link to clipboard
Copied
I would suggest a scripted Auto tone adjustment via Filter > Camera Raw Filter (you could apply to a smart object if you need editability):
/*
https://community.adobe.com/t5/Photoshop/Is-there-any-jsx-to-execute-the-auto-tone-of-camera-raw/m-p/10167490#M207367
*/
/*
ACR and raw files would be covered by this topic post:
https://community.adobe.com/t5/photoshop/how-to-record-button-click-on-action/td-p/10858184
*/
autoCRF();
function autoCRF() {
// Camera Raw Filter - Auto
var desc1 = new ActionDescriptor();
desc1.putBoolean(charIDToTypeID("AuTn"), true); // AuTn = Auto
executeAction(stringIDToTypeID('Adobe Camera Raw Filter'), desc1, DialogModes.NO);
}
P.S. If I had code for levels/curves auto then I would have posted that... Perhaps somebody else can do so?
Copy link to clipboard
Copied
Thanks for your help!
The automatic of CR in some cases does not work.
Copy link to clipboard
Copied
Perhaps an Auto Brightness/Contrast adjustment layer script from @r-bin
/*
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/13890435
*/
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);
// Merge adjustment layer
//executeAction( stringIDToTypeID( "mergeLayersNew" ), new ActionDescriptor(), DialogModes.NO );
Or:
/*
Auto Brightness-Contrast Smart Object Filter.jsx
v1.0 25th September 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/assistance-needed-for-automating-histogram-adjustment/td-p/14875387
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/13890435
*/
#target photoshop
if (app.documents.length) {
// New layer
app.activeDocument.artLayers.add();
// Merge visible
var idmergeVisible = stringIDToTypeID( "mergeVisible" );
var desc6726 = new ActionDescriptor();
var idduplicate = stringIDToTypeID( "duplicate" );
desc6726.putBoolean( idduplicate, true );
executeAction(idmergeVisible, desc6726, DialogModes.NO);
// Convert layer to smart object
executeAction( stringIDToTypeID( "newPlacedLayer" ), undefined, DialogModes.NO );
// Auto brightness/contrast by r-bin
var idbrightnessEvent = stringIDToTypeID( "brightnessEvent" );
var desc6767 = new ActionDescriptor();
var idauto = stringIDToTypeID( "auto" );
desc6767.putBoolean( idauto, true );
var iduseLegacy = stringIDToTypeID( "useLegacy" );
desc6767.putBoolean( iduseLegacy, false );
executeAction( idbrightnessEvent, desc6767, DialogModes.NO );
} else {
alert('A document must be open to run this script!');
}
Copy link to clipboard
Copied
Or perhaps an auto tone/levels:
/*
Auto Levels Merged Layer.jsx
v1.0 25th September 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/assistance-needed-for-automating-histogram-adjustment/td-p/14875387
https://community.adobe.com/t5/photoshop-ecosystem-discussions/can-we-use-quot-auto-quot-options-in-actions/m-p/13475793
*/
#target photoshop
if (app.documents.length) {
// New layer
app.activeDocument.artLayers.add();
// Merge visible
var idmergeVisible = stringIDToTypeID( "mergeVisible" );
var desc6726 = new ActionDescriptor();
var idduplicate = stringIDToTypeID( "duplicate" );
desc6726.putBoolean( idduplicate, true );
executeAction(idmergeVisible, desc6726, DialogModes.NO);
// Auto tone by jefbr
var desc500 = new ActionDescriptor();
desc500.putBoolean( charIDToTypeID("Auto"), true );
executeAction(charIDToTypeID("Lvls"), desc500, DialogModes.NO);
} else {
alert('A document must be open to run this script!');
}
Copy link to clipboard
Copied
A general comment, especially because you wrote that some of the suggestions did not work well for all images…
Current software applications, especially those by Adobe, are moving away from simple math-based exposure correction and toward exposure correction assisted by machine learning. One of the disadvantages of traditional math-based exposure correction is that it can’t tell the difference between a highlight with detail (which should probably be made darker and more visible to avoid clipping and overexposure) and a specular highlight (which can be allowed to blow out, partly because darkening a specular highlight usually darkens the rest of the image too much without restoring any image detail).
If I wanted to do bulk histogram optimization today, I would not use Photoshop or a script. Instead, I would load and select all of the images into Adobe Lightroom Classic, Lightroom, or Camera Raw, then apply the Auto Settings command. Auto Settings uses machine learning trained on many images, and usually produces a result that looks better than any of the old auto levels algorithms, and is more consistent across variations of image types.
Using those applications for bulk processing is also faster than in Photoshop, because all of the applications above can process and export selected images in parallel; Photoshop can only process images serially (one after the other).