Javascript 在Photoshop中查找所有层/图像的平均颜色并使用平均颜色文件名保存的脚本

Javascript 在Photoshop中查找所有层/图像的平均颜色并使用平均颜色文件名保存的脚本,javascript,photoshop,photoshop-script,Javascript,Photoshop,Photoshop Script,我有一个300+个人资料的皮肤色调文件夹,我必须按颜色从浅色调到深色调排序。我可以创建一个动作来获取每个肤色的平均颜色,但我无法自动重命名文件以匹配要识别的每个颜色 是否可以创建一个脚本来查找文件夹中每个图像的平均颜色(整张照片的平均颜色;我通常只是过滤>模糊>平均层),然后在原始文件名之前添加平均颜色的RGB或十六进制名称来保存新图像 例如:在脚本过滤器>模糊>平均层之后。skintone01.jpg的平均颜色为#ad8475,因此它会将文件重命名为ad8475-skintone01.jpg

我有一个300+个人资料的皮肤色调文件夹,我必须按颜色从浅色调到深色调排序。我可以创建一个动作来获取每个肤色的平均颜色,但我无法自动重命名文件以匹配要识别的每个颜色

是否可以创建一个脚本来查找文件夹中每个图像的平均颜色(整张照片的平均颜色;我通常只是过滤>模糊>平均层),然后在原始文件名之前添加平均颜色的RGB或十六进制名称来保存新图像

例如:在脚本过滤器>模糊>平均层之后。skintone01.jpg的平均颜色为#ad8475,因此它会将文件重命名为ad8475-skintone01.jpg

此外,我不确定这是否可行,但是否有一种方法可以使用脚本根据其平均颜色排列所有层。我不认为它可以,但我想既然我们是在讨论这个话题,不妨把它说出来

编辑:我刚刚测试了一些照片,发现按十六进制排序并不理想,因为Windows按奇怪的顺序排序十六进制代码。到目前为止,我发现按RGB编号排序是理想的,只要这三个编号之间都有空格

例如:如果平均颜色RGB为110 73 58,则脚本将新文件命名为“110 73 58 skintone01.jpg”,而不是“1107358 skintone01.jpg”。同样,这是由于Windows如何对文件进行排序

**基本上,这就是我想要对文件夹中每个照片的脚本执行的操作:

  • 复制层
  • 过滤>模糊>平均
  • 复制当前图层的RGB值
  • 使当前层(具有平均颜色的层)不可见
  • 在原始文件名之前使用RGB值保存图像(每个RBG值之间留有空格)**

  • 好的,以下是您需要的:

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

    我在瞎做这件事,不过应该管用。

    非常感谢你,食尸鬼傻瓜

    我研究了一下你的脚本,并设法修复了Photoshop在我身上的错误。我还根据自己的喜好做了一些调整。这是我使用的结果代码:

    // 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);
    }
    
    我所做的只是在平均之前激活“模糊”层,然后更改用于复制RGB值的编码,以适应采样代码中的值。然后我添加了代码,以便将结果四舍五入到小数点后3位

    在我尝试查看是否可以让脚本将所有新图像保存到一个新文件夹后,但无法找到方法。哈哈。但至少我让它工作起来了


    非常感谢你的帮助。如果没有你,我将无法做到这一点,而且可能会在我的电脑前坐上几个小时这是一个有趣的问题。我想我知道你要做什么。为了澄清,不知道你的源图像实际上是什么样子:你有一个皮肤石头图像的例子之前和之后的平均模糊?您当前是否在生成RGB值的图像上手动选择位置?除了第三阶段,剧本应该是直接的,这是一个棘手的部分。非常感谢你在这方面的帮助。以下是使用模糊>整个图像的平均值的肤色图像及其平均值的示例。因为我对整个图像的复制层求平均值,所以我从哪里得到RGB值并不重要。是的,第三部分和第三部分是棘手的部分,因为它不可能只用PSP操作,但我也不熟悉脚本。哦,非常感谢!我刚刚完成了测试,Adobe抛出了一个错误,“srcDoc.activeLayer.applyAvergeBlur(avg);”不是函数。我已将其更改为applyAverage();相反立即尝试修复程序可以工作,但现在它将抛出此错误:。我认为这可能是因为它模糊了底层而不是复制层。在玩了一段时间后,我在复制层部分后添加了一段代码,以便脚本选择“模糊”层
    activeDocument.activeLayer=activeDocument.artLayers.getByName(“模糊”)