skip to Main Content

I have a folder of 300+ profile skintones that I must sort by color from light tones to dark tone. I can create an action to get the average colors for each of the skintones, but I won’t be able to rename the file automatically to match each color to identifies with.

Is it possible to create a script that will find the average color (of the whole photo; I usually just Filter>Blur>Average the layer) for each image in the folder and then save the new image with the RGB or Hex name of the average color added before the original file name?

EX: After the script Filter>Blur>Average the layer. The average color for skintone01.jpg is #ad8475 so it will rename the file to ad8475-skintone01.jpg

Also, I’m not sure if this is possible, but is there a way to arrange all the layers in accordance to its average color using script. I don’t think it could but figure since we’re on that topic, might as well get it out there.

EDIT: I just tested a few of the photos and found that sorting by HEX is not ideal as Windows sort the hex code in a weird order. So far, I found that sorting it by RGB number is ideal as long as there is spaces between all three numbers.

EX: If average color RGB is 110 73 58, then the script will name the new file “110 73 58 skintone01.jpg” and not “1107358 skintone01.jpg”. Again, this is due to how Windows sort the files.

**Bascially, this is what I want to do with the script for each photos in the folder:

  1. Duplicate Layer
  2. Filter > Blur > Average
  3. Copy RGB Values of current layer
  4. Turn current layer (one with the average color) invisible
  5. Save image with RGB values before the original filename (with a space between each RBG value).**

2

Answers


  1. Chosen as BEST ANSWER

    Thank you so much, Ghoul Fool!

    I played around with your script along with some research and managed to fix the error Photoshop thew at me. I also made some tweaks to my liking. This is the resulting codes I'm using:

    // Set reference for active document
    var srcDoc = app.activeDocument;
    
    // get filename
    myFileName = srcDoc.name;
    //docName = myFileName.substring(0,myFileName.lastIndexOf("."));  
    
    //Duplicate Layer and call it "blurred"
    var layerName = "blurred";
    srcDoc.activeLayer.duplicate().name = layerName;
    
    //Select "Blurred" Layer
    activeDocument.activeLayer = activeDocument.artLayers.getByName("blurred");  
    
    // Filter > Blur > Average
    srcDoc.activeLayer.applyAverage();
    
    // remove any sample first
    srcDoc.colorSamplers.removeAll();
    
    // get width and height of image
    var w = srcDoc.width.value;
    var h = srcDoc.height.value;
    
    // get positions of the center of the image 
    //var x = 0;
    //var y = 0;
    var x = Math.round(w/2);
    var y = Math.round(h/2);
    
    // will pick a sample from the middle of the image
    var px = [UnitValue(x) , UnitValue(y)]; 
    var skinSampler = srcDoc.colorSamplers.add(px);
    
    // Copy RGB Values of current layer  with 3 decimal spaces
    var myColor = skinSampler.color; 
    var rgb_R = Math.round(myColor.rgb.red*1000)/1000; 
    var rgb_G = Math.round(myColor.rgb.green*1000)/1000; 
    var rgb_B = Math.round(myColor.rgb.blue*1000)/1000; 
    
    // remove that sample no we know it's value
    srcDoc.colorSamplers.removeAll();
    
    // Turn current layer (one with the average color) invisible
    srcDoc.activeLayer.visible = false;
    
    // Save image with RGB values before the original filename (with a space between each RBG value).
    mySaveName = rgb_R + " " + rgb_G +  " " + rgb_B +  " "  + myFileName;
    
    // Set filePath and fileName to source path
    var filePath = srcDoc.path + "/"  + mySaveName;
    
    // save as jpeg
    jpegIt(filePath, 12);
    
    // function for saving as jpeg
    function jpegIt(filePath, myJpgQuality)
    {
      if(! myJpgQuality) myJpgQuality = 12;
    
      // Flatten the jpg
      activeDocument.flatten();
    
      // jpg file options
      var jpgFile = new File(filePath);
      jpgSaveOptions = new JPEGSaveOptions();
      jpgSaveOptions.formatOptions = FormatOptions.OPTIMIZEDBASELINE;
      jpgSaveOptions.embedColorProfile = true;
      jpgSaveOptions.matte = MatteType.NONE;
      jpgSaveOptions.quality = myJpgQuality;
    
      activeDocument.saveAs(jpgFile, jpgSaveOptions, true, Extension.LOWERCASE);
    
      //close without saving
      app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    }
    

    All I did was make the "blurred" layer active before averaging it, then change the coding for Copying RGB Value to fit the values from sampling code. Then I added the codes so that it will round the result up to 3 decimal points.

    After I tried to see if I can get the script to save all new image to a new folder, but couldn't figured out how. LOL. But at least I got it working.

    Thank you so much for your help. I wouldn't be able to to this without you and would probably be sitting in front of my computer for hours. :D


  2. Okay here’s the what you need:

    // Set reference for active document
    var srcDoc = app.activeDocument;
    
    // get filename
    myFileName = srcDoc.name;
    //docName = myFileName.substring(0,myFileName.lastIndexOf("."));  
    
    //Duplicate Layer and call it "blurred"
    var layerName = "blurred";
    srcDoc.activeLayer.duplicate().name = layerName;
    
    // Filter > Blur > Average
    srcDoc.activeLayer.applyAverage();
    
    
    // remove any sample first
    srcDoc.colorSamplers.removeAll();
    
    // get width and height of image
    var w = srcDoc.width.value;
    var h = srcDoc.height.value;
    
    // get positions of the center of the image 
    //var x = 0;
    //var y = 0;
    var x = Math.round(w/2);
    var y = Math.round(h/2);
    
    // will pick a sample from the middle of the image
    var px = [UnitValue(x) , UnitValue(y)]; 
    var skinSampler = srcDoc.colorSamplers.add(px);
    
    // Copy RGB Values of current layer
    var myColour = myColorSampler.color; 
    var rgb_R = myColor.rgb.red; 
    var rgb_G = myColor.rgb.green; 
    var rgb_B = myColor.rgb.blue; 
    
    // remove that sample no we know it's value
    srcDoc.colorSamplers.removeAll();
    
    // Turn current layer (one with the average color) invisible
    srcDoc.activeLayer.visible = false;
    
    // Save image with RGB values before the original filename (with a space between each RBG value).
    mySaveName = rgb_R + " " + rgb_G + rgb_B + myFileName;
    
    // Set filePath and fileName to source path
    var filePath = srcDoc.path + "/"  + mySaveName;
    
    // save as jpeg
    jpegIt(filePath, 12);
    
    // function for saving as jpeg
    function jpegIt(filePath, myJpgQuality)
    {
      if(! myJpgQuality) myJpgQuality = 12;
    
      // Flatten the jpg
      activeDocument.flatten();
    
      // jpg file options
      var jpgFile = new File(filePath);
      jpgSaveOptions = new JPEGSaveOptions();
      jpgSaveOptions.formatOptions = FormatOptions.OPTIMIZEDBASELINE;
      jpgSaveOptions.embedColorProfile = true;
      jpgSaveOptions.matte = MatteType.NONE;
      jpgSaveOptions.quality = myJpgQuality;
    
      activeDocument.saveAs(jpgFile, jpgSaveOptions, true, Extension.LOWERCASE);
    
      //close without saving
      app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    }
    

    I’m doing this blind, should work though.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search