【问题标题】:Photoshop CC Batch Action TinyPNG PluginPhotoshop CC 批量操作 TinyPNG 插件
【发布时间】:2017-05-28 16:22:29
【问题描述】:

我有一个文件夹,里面有几个子文件夹,用于存储我的图像。

文件夹结构如下:

我想创建一个批处理命令,将“所有产品”文件夹作为源文件夹,使用 tinyPNG Photoshop 插件 (https://tinypng.com/) 并像这样存储每个产品的压缩文件:所有产品 --> 产品X --> 压缩图像

这可能吗?

【问题讨论】:

  • 到目前为止您尝试过什么? (在脚本方面。)
  • 请将文件夹结构发布为文本而不是图像;文本可以很容易地复制给潜在的回答者用于测试,但图像不能,所以其他人必须重新输入所有内容,这减少了获得答案的变化......
  • @aschipfl 我尝试将其作为文本发布,但 stackoverflow 删除了中断,因此很难看到它应该是什么样子。
  • @treintje 我尝试了批处理自动化向导,但没有实际的代码示例
  • @AlexGogl 如果您真的尝试自己编写脚本,人们会更愿意提供帮助。如果您在此过程中遇到困难或需要帮助,请使用您迄今为止尝试过的内容更新您的帖子。为了帮助您入门,我建议您阅读以下帖子:stackoverflow.com/a/9649214

标签: batch-file automation photoshop


【解决方案1】:

我实际上采用了不同的方法:脚本现在打开一个文件夹,在其中获取所有 jpeg、png 和 tif 图像,使用白色画布将它们调整为正方形(非常适合 woocommerce 和其他电子商务系统),然后使用tinyPNG 插件并随后覆盖现有文件,这样就不必动态创建文件夹并且图片保留在同一个文件夹中,您只需复制所有原始图像并让它们被覆盖。

使用该脚本,可以将纵向模式或横向模式图像转换为具有您选择的尺寸(在本例中为 2000x2000)的正方形(通过调整图片大小并扩展画布来工作),因此每个图像在商店系统。在左侧您可以看到原始的,在右侧是调整大小和压缩后的:

如何使用:首先你需要购买 tinyPNG 插件或者使用你自己的导出逻辑(比如标准 Photoshop Web 导出),然后将代码保存为 .jsx 文件并放入你的 Photoshop -> Presets - > 脚本文件夹。现在您应该在 File -> Automate 部分看到一个新选项(如果没有重新启动 PS)。要批量调整和压缩文件夹(甚至子文件夹)中的所有图片,首先需要在 Photoshop 中打开根文件夹的图片,然后按“将图片大小调整为方形并压缩...”按钮,会出现一个对话框,提示你来选择文件夹。现在让它运行(有很多图像可能需要很长时间)

现在是压缩率:

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

注意:如果您有一个图像,即对象(如太阳伞)没有在中间对齐,最终的图片也不会在中间对齐,如果有人对如何解决这个问题有任何想法,我会很高兴听。

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

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

【讨论】:

    猜你喜欢
    • 2019-03-05
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 2012-04-14
    相关资源
    最近更新 更多