用于在 Photoshop 中查找所有 Layers/Images 的平均颜色并使用平均颜色的文件名保存的脚本
A Script to Find Average Colors of all Layers/Images in Photoshop and Save with Filename of Average Color
我有一个包含 300 多个个人资料肤色的文件夹,我必须按颜色从浅色调到深色调进行分类。我可以创建一个动作来获取每种肤色的平均颜色,但我无法自动重命名文件以匹配每种颜色以进行识别。
是否可以创建一个脚本来为文件夹中的每个图像找到平均颜色(整张照片的;我通常只是滤镜>模糊>平均图层),然后使用 RGB 保存新图像或在原始文件名之前添加的平均颜色的十六进制名称?
EX:在脚本滤镜>模糊>平均层之后。 skintone01.jpg 的平均颜色是#ad8475,因此它将文件重命名为 ad8475-skintone01.jpg
此外,我不确定这是否可行,但有没有办法使用脚本根据其平均颜色排列所有图层。我认为它不能不考虑,因为我们正在讨论这个话题,不妨把它拿出来。
编辑:我刚刚测试了几张照片,发现按十六进制排序并不理想,因为 Windows 以奇怪的顺序对十六进制代码进行排序。到目前为止,我发现只要所有三个数字之间有 spaces,按 RGB 数字排序是理想的。
EX:如果平均颜色 RGB 为 110 73 58,则脚本会将新文件命名为“110 73 58 skintone01.jpg”而不是“1107358 skintone01.jpg”。同样,这是由于 Windows 对文件的排序方式所致。
**基本上,这就是我想用脚本为文件夹中的每张照片做的事情:
- 复制图层
- 滤镜 > 模糊 > 平均
- 复制当前层的 RGB 值
- 隐藏当前层(具有平均颜色的层)
- 保存原始文件名前带有 RGB 值的图像(每个 RBG 值之间有 space)。**
好的,这就是你需要的:
// 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);
}
我所做的只是在对它进行平均之前激活 "blurred" 层,然后更改复制 RGB 值的编码以适应采样代码中的值。然后我添加了代码,以便将结果四舍五入到小数点后 3 位。
在我尝试查看是否可以获得将所有新图像保存到新文件夹的脚本之后,但不知道如何操作。哈哈。但至少我让它工作了。
非常感谢您的帮助。如果没有你,我将无法做到这一点,并且可能会坐在我的电脑前几个小时。 :D
我有一个包含 300 多个个人资料肤色的文件夹,我必须按颜色从浅色调到深色调进行分类。我可以创建一个动作来获取每种肤色的平均颜色,但我无法自动重命名文件以匹配每种颜色以进行识别。
是否可以创建一个脚本来为文件夹中的每个图像找到平均颜色(整张照片的;我通常只是滤镜>模糊>平均图层),然后使用 RGB 保存新图像或在原始文件名之前添加的平均颜色的十六进制名称?
EX:在脚本滤镜>模糊>平均层之后。 skintone01.jpg 的平均颜色是#ad8475,因此它将文件重命名为 ad8475-skintone01.jpg
此外,我不确定这是否可行,但有没有办法使用脚本根据其平均颜色排列所有图层。我认为它不能不考虑,因为我们正在讨论这个话题,不妨把它拿出来。
编辑:我刚刚测试了几张照片,发现按十六进制排序并不理想,因为 Windows 以奇怪的顺序对十六进制代码进行排序。到目前为止,我发现只要所有三个数字之间有 spaces,按 RGB 数字排序是理想的。
EX:如果平均颜色 RGB 为 110 73 58,则脚本会将新文件命名为“110 73 58 skintone01.jpg”而不是“1107358 skintone01.jpg”。同样,这是由于 Windows 对文件的排序方式所致。
**基本上,这就是我想用脚本为文件夹中的每张照片做的事情:
- 复制图层
- 滤镜 > 模糊 > 平均
- 复制当前层的 RGB 值
- 隐藏当前层(具有平均颜色的层)
- 保存原始文件名前带有 RGB 值的图像(每个 RBG 值之间有 space)。**
好的,这就是你需要的:
// 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);
}
我所做的只是在对它进行平均之前激活 "blurred" 层,然后更改复制 RGB 值的编码以适应采样代码中的值。然后我添加了代码,以便将结果四舍五入到小数点后 3 位。
在我尝试查看是否可以获得将所有新图像保存到新文件夹的脚本之后,但不知道如何操作。哈哈。但至少我让它工作了。
非常感谢您的帮助。如果没有你,我将无法做到这一点,并且可能会坐在我的电脑前几个小时。 :D