我有一个300多个配置文件的文件夹,我必须按颜色从浅色调到深色调。我可以创建一个动作来获取每个skintones的平均颜色,但我无法自动重命名该文件以匹配每个颜色以识别。
是否可以创建一个脚本,该脚本将为文件夹中的每个图像找到(整张照片的平均颜色;我通常只是滤镜> Blur>平均图层),然后使用RGB或Hex保存新图像在原始文件名之前添加的平均颜色的名称?
EX:脚本过滤器>模糊>平均图层后。 skintone01.jpg的平均颜色是#ad8475所以它会将文件重命名为ad8475-skintone01.jpg
另外,我不确定这是否可行,但有没有办法根据脚本的平均颜色排列所有图层。我不认为它可以解决,因为我们关注那个话题,不妨把它拿出来。
编辑:我刚刚测试了一些照片,发现按HEX排序并不理想,因为Windows以奇怪的顺序对十六进制代码进行排序。到目前为止,我发现只要三个数字之间有空格,用RGB数字对它进行排序是理想的。EX:如果平均颜色RGB是110 73 58,则脚本将命名新文件" 110 73 58 skintone01.jpg"而不是" 1107358 skintone01.jpg"。同样,这是由于Windows如何对文件进行排序。
**基本上,这就是我想对文件夹中每张照片的脚本做的事情:
答案 0 :(得分:2)
非常感谢,Ghoul Fool!
我和你的剧本一起玩了一些研究,并设法修复了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个小数点。
在我试图查看是否可以将脚本保存到新文件夹的所有新图像后,但无法弄清楚如何。大声笑。但至少我让它发挥作用。
非常感谢你的帮助。没有你我就无法做到这一点,并且可能会在我的电脑前坐几个小时。 :d
答案 1 :(得分:1)
好的,这就是你需要的:
// 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);
}
我这样做是盲目的,应该工作。