2017-05-28 77 views
0

我有一個文件夾,其中有幾個子文件夾,其中存儲了我的圖像。Photoshop CC批量操作TinyPNG插件

的文件夾結構如下所示:

Folder structure

我想創建一個批處理命令,是以「所有產品」文件夾作爲源文件夾,使用tinyPNG Photoshop的插件(https://tinypng.com/)和商店每個產品的壓縮文件都是這樣的:所有產品 - >產品X - >壓縮圖像

這可能嗎?

+0

你嘗試過這麼遠嗎? (在腳本方面。) – treintje

+0

請將文件夾結構作爲文本而不是圖像發佈;文本可以很容易地複製潛在的答覆者用於測試,但圖像不能,所以其他必須重新輸入的一切,這減少了獲得答案的變化... – aschipfl

+0

@aschipfl我試圖發佈它作爲文本,但stackoverflow刪除因此很難看到它應該是什麼樣子。 –

回答

0

其實我去不同的東西:該腳本現在打開一個文件夾,得到所有的JPEG,PNG和TIF圖片在那裏,他們重新調整到一個白色帆布(尼斯woocommerce和其他電子商務系統)廣場,壓縮它們使用tinyPNG插件並隨後覆蓋現有文件,這種方式不需要在運行中創建文件夾,並且圖片保留在同一個文件夾中,您只需複製所有原始圖像並讓它們被覆蓋即可。

通過腳本,可以將肖像模式或風景模式圖像轉換爲正方形(通過調整圖片大小和擴展畫布進行工作),使用您選擇的尺寸(在此情況下爲2000x2000),因此每張圖片看起來都相同在商店系統中。在左邊,你可以看到原來的一個並在調整的權利,並壓縮一個:enter image description here enter image description here

使用方法:所有你需要購買tinyPNG插件或使用自己的出口邏輯的第一個(比如標準Photoshop Web Export),然後將代碼保存爲.jsx文件並將其放入Photoshop - > Presets - > Scripts文件夾中。現在,您應該在文件 - >自動化部分中看到一個新選項(如果不重新啓動PS)。要批量調整大小並壓縮文件夾中的所有圖片(甚至是子文件夾),首先需要打開photoshop中根文件夾的圖片,然後按「調整圖片大小並調整大小...」按鈕,會出現一個對話框,提示你選擇文件夾。現在讓它運行(可能需要相當長着大量的圖片)

現在的壓縮率:在壓縮圖像enter image description here

TinyPNG真的做得很好。

這裏最終腳本:

/* 

// Get images from a folder recursively resize to square and compress with tinyPNG 
// Copyright (c) 2017 Alex Gogl 

<javascriptresource> 
<menu>automate</menu> 
<name>$$$/JavaScripts/ToSquareCompress/Menu=Resize images to Square and Compress...</name> 
<eventid>7b078a04-ba43-4214-8eda-4026a5d2bd33</eventid> 
</javascriptresource> 

*/ 

function compressFile(file, percentage) { 

    // Open the file without dialogs like Adobe Camera Raw 
    var opener = new ActionDescriptor(); 
    opener.putPath(charIDToTypeID("null"), file); 
    executeAction(charIDToTypeID("Opn "), opener, DialogModes.NO); 

    // Select the opened document 
    var document = app.activeDocument; 

    // Change the color space to RGB if needed 
    if (document.mode == DocumentMode.INDEXEDCOLOR) { 
     document.changeMode(ChangeMode.RGB); 
    } 

    // Switch to 8 bit RGB if the image is 16 bit 
    if (document.bitsPerChannel == BitsPerChannelType.SIXTEEN) { 
     convertBitDepth(8); 
    } 

    // Choose the scale percentage 
    if (percentage === undefined || percentage < 10 || percentage > 100) { 
     percentage = 100; 
    } 

    //-----------START RESIZE LOGIC----------- 
    // these are our values for the END RESULT width and height (in pixels) of our image 
    var fWidth = 2000; 
    var fHeight = 2000; 

    // do the resizing. if height > width (portrait-mode) resize based on height. otherwise, resize based on width. if height equals width do nothing 
    //ResamlpleMethod set to BICUBICSHARPER due to most images being resized to a smaller resolution 
    if (document.height > document.width) { 
     document.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBICSHARPER); 
    } 
    else { 
     document.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBICSHARPER); 
    } 

    // Makes the default background white 
    var white = new SolidColor(); 
    white.rgb.hexValue = "FFFFFF"; 
    app.backgroundColor = white; 

    // Convert the canvas size as informed above for the END RESULT 
    app.activeDocument.resizeCanvas(UnitValue(fWidth,"px"),UnitValue(fHeight,"px")); 
    //-----------END RESIZE LOGIC----------- 

    // Compress the document 
    var tinify = new ActionDescriptor(); 
    tinify.putPath(charIDToTypeID("In "), file); /* Overwrite original! */ 
    tinify.putUnitDouble(charIDToTypeID("Scl "), charIDToTypeID("#Prc"), percentage); 
    tinify.putEnumerated(charIDToTypeID("FlTy"), charIDToTypeID("tyFT"), charIDToTypeID("tyJP")); /* Force JPEG */ 

    var compress = new ActionDescriptor(); 
    compress.putObject(charIDToTypeID("Usng"), charIDToTypeID("tinY"), tinify); 
    executeAction(charIDToTypeID("Expr"), compress, DialogModes.NO); 

    document.close(SaveOptions.DONOTSAVECHANGES); 
} 

function convertBitDepth(bitdepth) { 
    var id1 = charIDToTypeID("CnvM"); 
    var convert = new ActionDescriptor(); 
    var id2 = charIDToTypeID("Dpth"); 
    convert.putInteger(id2, bitdepth); 
    executeAction(id1, convert, DialogModes.NO); 
} 

function compressFolder(folder) { 
    // Recursively open files in the given folder 
    var children = folder.getFiles(); 
    for (var i = 0; i < children.length; i++) { 
     var child = children[i]; 
     if (child instanceof Folder) { 
      compressFolder(child); 
     } else { 
      /* Only attempt to compress PNG,JPG,TIFF files. */ 
      if ((child.name.slice(-5).toLowerCase() == ".jpeg")||(child.name.slice(-4).toLowerCase() == ".jpg" || ".png" || ".tif")) { 
       compressFile(child); 
      } 
     } 
    } 
} 

if (confirm("Warning. You are about to compress all JPEG files in the chosen folder. This cannot be undone.\n\rAre you sure you want to continue?")) { 
    try { 
     // Let user select a folder 
     compressFolder(Folder.selectDialog("Choose a folder with JPEG/PNG/TIF images to compress with TinyJPG")); 
     alert("All JPEG/PNG/TIF files compressed."); 
    } catch(error) { 
     alert("Error while processing: " + error); 
    } 
} 

注:如果您擁有的,即物體(如的Sunbrella)的圖像,中間沒有對齊,最終的圖像也不會被中間對齊,如果任何人有想法如何解決,我會很樂意傾聽。

來源:Photoshop JavaScript to resize image and canvas to specific (not square) sizes

https://tinypng.com/photoshop/support#tips-tricks