Copy link to clipboard
Copied
Does anyone know of, or have a script that could help with the following problem:
I have a bunch of images (thousands) that are black and white greyscale, but that have been saved as RGB images. ie. they should really only have 1 channel, but have 3 identical channels. I need to be able to detect these images and reduce them down to single channel images.
Any help would be appreciated.
Thanks !
var ColourMode = app.activeDocument.mode;
if( ColourMode == 'DocumentMode.RGB' ){
alert( ColourMode );
}
Copy link to clipboard
Copied
Get the 3 channel histograms: are they all equal? If so, it's probalby grayscale.
There is a small chance that some test images might also meet that test (same gradient repeated different directions).
Copy link to clipboard
Copied
If you have a folder of images that you know should all be grayscale but some may have been saved as RGB you can convert them with a script like the one below
// checks a folder of jpg, tif, or psd images for RGB color space. If any are found they are opened,
// converted to greyscale, and resaved overwriting the RGB versions
// requires Photoshop CS4 because it used the XMPLibrary
#target Photoshop
if( loadXMPLibrary() ){// if XMPLib error don't do anything
var targetFolder = Folder.selectDialog("Select a folder of documents to process");
var fList = targetFolder.getFiles(/\.(jpg|tif|psd)$/i);// get a list of files
for( var i = 0; i < fList.length; i++ ){// loop list
var mode = getColourMode( fList );// check mode
if( mode == 'RGB' ){// if RGB open and convert
app.open( fList );
app.activeDocument.changeMode( ChangeMode.GRAYSCALE );
app.activeDocument.close( SaveOptions.SAVECHANGES );
}
}
unloadXMPLibrary();
}
function getColourMode( file ){
try{
loadXMPLibrary();
var xmpf = new XMPFile( file.fsName, XMPConst.UNKNOWN,
XMPConst.OPEN_FOR_READ );
var xmp = xmpf.getXMP();
xmpf.closeFile();
var ColourMode = xmp.getProperty(XMPConst.NS_PHOTOSHOP,'ColorMode', XMPConst.INTEGER);
if(ColourMode == undefined) ColourMode = 99;
switch (Number(ColourMode)){
case 0: ColourMode = "Bitmap"; break;
case 1: ColourMode = "Grayscale"; break;
case 2: ColourMode = "Indexed Color"; break;
case 3: ColourMode = "RGB"; break;
case 4: ColourMode = "CMYK"; break;
case 7: ColourMode = "Multichannel"; break;
case 8: ColourMode = "Duotone"; break;
case 9: ColourMode = "Lab"; break;
default : ColourMode = "Untagged"; break;
}
return ColourMode
}catch(e){}
unloadXMPLibrary();
}
function loadXMPLibrary(){
if ( !ExternalObject.AdobeXMPScript ){
try{
ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
}catch (e){
return false;
}
}
return true;
}
function unloadXMPLibrary(){
if( ExternalObject.AdobeXMPScript ) {
try{
ExternalObject.AdobeXMPScript.unload();
ExternalObject.AdobeXMPScript = undefined;
}catch (e){}
}
}
If you are not sure they should be grayscale and need to check you could open the image, dupe and convert to Lab then check the mid point of the a and b channels histogram. If they match the total number of pixels it's a grayscale image. Something like
app.activeDocument.duplicate();
app.activeDocument.changeMode( ChangeMode.LAB );
var totalPixels = Number( app.activeDocument.width.as( 'px' ) ) * Number( app.activeDocument.height.as( 'px' ) );
var aMid = Number( app.activeDocument.channels[1].histogram[128] );
var bMid = Number( app.activeDocument.channels[2].histogram[128] );
if( aMid == totalPixels && bMid == totalPixels ){
alert('looks gray to me');
}
Copy link to clipboard
Copied
I'm in CS3 right now but those are good starting points for me to work with. I'll see what I can do with that
Copy link to clipboard
Copied
Ok - I've stripped it down to work only on the file that is open for the time being (I'm still learning PS scripting, so one step at a time for me).
The bottom section of my code works - converts the file to lab colour, checks it, then converts it to gray and saves if necessary. The bug is with the top part - checking if the current image is actually an RGB to start with. Can you see what's wrong with my topmost 'IF' section? (colormodel)
// Convert active document to lab colour then
// check the mid point of the a and b channels histogram.
// If they match the total number of pixels it's a grayscale image.// Only do this function if the opened file is an RGB image
var ColourMode = Number ( app.activeDocument.ColorModel);
if( ColourMode == '3')
{
alert('Checking an RGB image');
app.activeDocument.changeMode( ChangeMode.LAB );
var totalPixels = Number( app.activeDocument.width.as( 'px' ) ) * Number( app.activeDocument.height.as( 'px' ) );
var aMid = Number( app.activeDocument.channels[1].histogram[128] );
var bMid = Number( app.activeDocument.channels[2].histogram[128] );// If the midpoints match, this is a grayscale image that
// needs to be converted and saved as such
if( aMid == totalPixels && bMid == totalPixels ){
alert('Changing image to gray');
app.activeDocument.changeMode( ChangeMode.GRAYSCALE );
activeDocument.close(SaveOptions.SAVECHANGES);
}
// If not, just close the document and move on
else
{
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
Copy link to clipboard
Copied
var ColourMode = app.activeDocument.mode;
if( ColourMode == 'DocumentMode.RGB' ){
alert( ColourMode );
}
Copy link to clipboard
Copied
Thanks Michael - that's done the trick 🙂
Copy link to clipboard
Copied
For the sake of completeness when this topic turns up in a google search in later years, here's my completed script with comments, prompts and a counter. Because my application only involves TIF images, it's pretty simple:
var counter=0;
var yesorno = confirm ("This script will search the chosen texture folder for greyscale TIF images that have been saved as RGBs by mistake.\nThose images will be converted to greyscale and re-saved. Pure RGB and RGBA images will not be touched.\nScript by Chris Longhurst.\n\nDo you want to run this script?","Grey texture converter");
if (yesorno == true)
{
//Select the folder with the textures we need to check
var texturefolder = Folder.selectDialog("Select the folder containing the images to be checked");
//Get a list of the files
var textureList=texturefolder.getFiles(/\.(tif)$/i);
//Create loop to open and process files
for (var a = 0; a<textureList.length; a++)
{
// open file
app.open(textureList);
// Only perform the actions in this script if the opened file is an RGB image
...
Find more inspiration, events, and resources on the new Adobe Community
Explore Now