注意1:图片压缩后会覆盖 原始图片
注意2:目录不能带 中文
注意3:56行注译掉 子目录 将不会被压缩(其它说明看代码的注译)
// Open a given folder and compress all PNG and JPEG images with Tinify. // Copyright (c) 2015 Voormedia B.V. All rights reserved. function compressFile(file, percentage) { try { // 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; // 色彩类型,默认:RGB if (document.mode == DocumentMode.INDEXEDCOLOR) { document.changeMode(ChangeMode.RGB); } // 图像位深,默认:8位 if (document.bitsPerChannel == BitsPerChannelType.SIXTEEN) { convertBitDepth(8); } // 压缩百份比,默认:100 if (percentage === undefined || percentage < 10 || percentage > 100) { percentage = 100; } // Compress the document var tinify = new ActionDescriptor(); tinify.putPath(charIDToTypeID("In "), file); // 覆盖原始图片 tinify.putUnitDouble(charIDToTypeID("Scl "), charIDToTypeID("#Prc"), percentage ); var compress = new ActionDescriptor(); compress.putObject(charIDToTypeID("Usng"), charIDToTypeID("tinY"), tinify); executeAction(charIDToTypeID("Expr"), compress, DialogModes.NO); document.close(SaveOptions.DONOTSAVECHANGES); } catch (e) {} } 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 { if ((child.name.slice(-4).toLowerCase() == ".png")||(child.name.slice(-5).toLowerCase() == ".jpeg")||(child.name.slice(-4).toLowerCase() == ".jpg")) { compressFile(child); } } } } if (confirm("Warning. You are about to compress all JPEG and PNG 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 images to compress with TinyPNG/JPG")); alert("All JPEG and PNG files compressed."); } catch(error) { alert("Error while processing: " + error); } }
压缩完后,可能你会需要一个图片重命名脚本。
本文最后更新于:2023-11-11 at 16:57:04
1 2
原文链接:https://junkai.cc/454.html,转载请注明出处~~~
评论0