Photoshop CC 批处理动作 TinyPNG 插件
Photoshop CC Batch Action TinyPNG Plugin
我有一个包含多个子文件夹的文件夹,其中存储了我的图像。
文件夹结构如下所示:
我想创建一个批处理命令,以 "All Products" 文件夹作为源文件夹,使用 tinyPNG Photoshop 插件 (https://tinypng.com/) 并存储每个产品的压缩文件,如下所示: 所有产品 --> 产品 X --> 压缩图像
这可能吗?
我实际上做了一些不同的事情:脚本现在打开一个文件夹,在其中获取所有 jpeg、png 和 tif 图像,将它们调整为带有白色 canvas 的正方形(非常适合 woocommerce 和其他电子商务系统),使用 tinyPNG 插件压缩它们并随后覆盖现有文件,这样就不必即时创建文件夹并且图片保留在同一个文件夹中,您只需要复制所有原始图像并让它们被覆盖。
使用脚本可以将纵向模式或横向模式图像转换为具有您选择的尺寸(在本例中为 2000x2000)的正方形(通过调整图片大小和扩展 canvas 来工作),所以每张图片在商店系统中看起来都是一样的。在左侧您可以看到原始的,在右侧您可以看到调整大小和压缩后的:
使用方法:首先你需要购买tinyPNG插件或者使用你自己的导出逻辑(比如Standard Photoshop Web Export),然后将代码保存为.jsx文件并放入你的Photoshop ->预设 -> 脚本文件夹。现在您应该在文件 -> 自动化部分看到一个新选项(如果不重新启动 PS)。要批量调整和压缩文件夹(甚至子文件夹)中的所有图片,您首先需要在 photoshop 中打开根文件夹的图片,然后按 "Resize images to Square and Compress..." 按钮,将出现一个对话框,提示您选择文件夹。现在让它 运行(如果有很多图像可能会花费很长时间)
现在压缩率:
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
我有一个包含多个子文件夹的文件夹,其中存储了我的图像。
文件夹结构如下所示:
我想创建一个批处理命令,以 "All Products" 文件夹作为源文件夹,使用 tinyPNG Photoshop 插件 (https://tinypng.com/) 并存储每个产品的压缩文件,如下所示: 所有产品 --> 产品 X --> 压缩图像
这可能吗?
我实际上做了一些不同的事情:脚本现在打开一个文件夹,在其中获取所有 jpeg、png 和 tif 图像,将它们调整为带有白色 canvas 的正方形(非常适合 woocommerce 和其他电子商务系统),使用 tinyPNG 插件压缩它们并随后覆盖现有文件,这样就不必即时创建文件夹并且图片保留在同一个文件夹中,您只需要复制所有原始图像并让它们被覆盖。
使用脚本可以将纵向模式或横向模式图像转换为具有您选择的尺寸(在本例中为 2000x2000)的正方形(通过调整图片大小和扩展 canvas 来工作),所以每张图片在商店系统中看起来都是一样的。在左侧您可以看到原始的,在右侧您可以看到调整大小和压缩后的:
使用方法:首先你需要购买tinyPNG插件或者使用你自己的导出逻辑(比如Standard Photoshop Web Export),然后将代码保存为.jsx文件并放入你的Photoshop ->预设 -> 脚本文件夹。现在您应该在文件 -> 自动化部分看到一个新选项(如果不重新启动 PS)。要批量调整和压缩文件夹(甚至子文件夹)中的所有图片,您首先需要在 photoshop 中打开根文件夹的图片,然后按 "Resize images to Square and Compress..." 按钮,将出现一个对话框,提示您选择文件夹。现在让它 运行(如果有很多图像可能会花费很长时间)
现在压缩率:
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