2017-08-24 56 views
3

我有一個包含300多個配置文件的文件夾,我必須按照淺色至深色調的顏色進行分類。我可以創建一個動作來獲取每個skintones的平均顏色,但我無法自動重命名文件以匹配每種顏色來標識。在Photoshop中查找所有圖層/圖像的平均顏色並使用平均顏色的文件名保存的腳本

是否可以創建一個腳本來查找文件夾中每個圖像的平均顏色(整張照片的平均顏色;我通常只是濾鏡>模糊>平均圖層),然後使用RGB保存新圖像或在原始文件名之前添加的平均顏色的十六進制名稱?

EX:腳本過濾器>模糊>平均圖層後。 skintone01.jpg的平均顏色是#ad8475,所以它會將文件重命名爲ad8475-skintone01.jpg

另外,我不確定這是否可行,但是有沒有辦法按照所有圖層進行排列以平均顏色使用腳本。我不認爲它可以,但是因爲我們正在討論這個話題,所以不妨把它放在那裏。

編輯:我剛剛測試了一些照片,發現由HEX排序並不理想,因爲Windows按奇怪的順序對十六進制代碼進行排序。到目前爲止,只要三個數字之間有空格,我發現按RGB數字排序是理想的。 EX:如果平均顏色RGB是110 73 58,那麼腳本會將新文件命名爲「110 73 58 skintone01.jpg」而不是「1107358 skintone01.jpg」。再次,這是由於Windows如何對文件進行排序。

** Bascially,這就是我想要的腳本文件夾中的每個照片的事:

  1. 複製圖層
  2. 濾鏡>模糊>平均
  3. 當前層複製RGB值
  4. 打開當前層(一個具有平均顏色)無形
  5. 與原來的文件名(與每個RBG值之間的空間)之前的RGB值保存圖像。**
+0

這是一個有趣的問題。我想我知道你要去哪裏。爲了澄清,不知道你的源圖像實際上是什麼樣子的:你有平均模糊之前和之後的皮膚石像的例子嗎?您目前是否在手動選擇您製作RGB值的圖像上的位置?除了階段3以外,劇本應該是直截了當的,這是棘手的部分。 –

+0

非常感謝你幫助我。以下是使用模糊的skintone [鏈接](https://imagez.to/i/B6vBxm5x.png)及其平均[鏈接](https://imagez.to/i/5CzOW8g9.jpg)的示例圖像>整個圖像的平均值。因爲我對整個圖像的重複圖層進行了平均處理,所以從哪裏獲取RGB值並不重要。是的,第3部分和其他部分是棘手的部分,因爲它不可能僅用於PSP操作,但我對腳本也不熟悉。 –

回答

2

非常感謝你,食屍鬼傻瓜!

我隨着你的腳本一起玩了一些研究,並設法修復錯誤的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

好了,這裏的你需要什麼:

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

我這樣做盲目的,應該工作,雖然。

+0

哦,非常感謝你!我剛剛完成了測試,Adobe正在拋出一個錯誤「srcDoc.activeLayer.applyAvergeBlur(avg);」不是一個功能。 –

+0

我已將它更改爲applyAverage();代替。立即給它一個 –

+0

修復工程,但現在它扔回這個錯誤:[鏈接](https://imagez.to/i/pT8J29gF.jpg)。我認爲它可能不得不做一些事情,它是模糊底層而不是重複的。 –