7

如何使用 PhotoShop 的 JavaScript 功能将一组数百张图像转换为特定的非正方形尺寸(例如 320x350 像素)?

4

1 回答 1

33

在网上搜索我发现了许多潜在的解决方案,但其中 100% 转换为方形。所以,我收集了一些并自己解决了这个问题。

将以下代码另存为.jsxPhotoshop\Presets\Scripts文件夹中的文件。然后,如果想将它与许多文件一起使用,请进行操作。

// get a reference to the current (active) document and store it in a variable named "doc"
doc = app.activeDocument;  

// change the color mode to RGB.  Important for resizing GIFs with indexed colors, to get better results
doc.changeMode(ChangeMode.RGB);  

// these are our values for the END RESULT width and height (in pixels) of our image
var fWidth = 320;
var fHeight = 350;

// do the resizing.  if height > width (portrait-mode) resize based on height.  otherwise, resize based on width
if (doc.height > doc.width) {
    doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
}
else {
    doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}

// 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"));

// our web export options
var options = new ExportOptionsSaveForWeb();
options.quality = 70;
options.format = SaveDocumentType.JPEG;
options.optimized = true;

var newName = 'web-'+doc.name+'.jpg';

doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);
于 2013-07-10T21:09:03.997 回答