2017-05-28 79 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