我有一个包含多个子文件夹的文件夹,其中存储了我的图像。
文件夹结构如下所示:
我想创建一个批处理命令,将“所有产品”文件夹作为源文件夹,使用tinyPNG Photoshop插件(https://tinypng.com/)并存储每个产品的压缩文件,如下所示:所有产品 - >产品X - >压缩图像
这可能吗?
答案 0 :(得分:0)
我实际上选择了不同的东西:脚本现在打开一个文件夹,在那里获取所有jpeg,png和tif图像,用白色画布调整它们的大小(适用于woocommerce和其他电子商务系统),压缩它们tinyPNG插件随后会覆盖现有文件,这样就不必动态创建文件夹并且图片保留在同一个文件夹中,您只需要复制所有原始图像并让它们被覆盖。 / p>
使用该脚本,可以将肖像模式或横向模式图像转换为正方形(通过调整图片大小并扩展画布),使用您选择的尺寸(在本例中为2000x2000),图像在商店系统中看起来相同。在左侧,您可以看到原始的一个,右侧是已调整大小和压缩的一个:
使用方法:首先,您需要购买tinyPNG插件或使用您自己的导出逻辑(比如标准Photoshop Web导出),然后将代码保存为.jsx文件并将其放入Photoshop中 - >预设 - >脚本文件夹。现在你应该在File - >中看到一个新选项。自动部分(如果没有重启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);
}
}
注意:如果你有一个图像,即对象(如sunbrella)没有在中间对齐,最后的图片也不会在中间对齐,如果有人有关于如何修复我的想法#39我会很高兴听。
资料来源:Photoshop JavaScript to resize image and canvas to specific (not square) sizes