Photoshop JavaScript 调整图像和画布大小以特定尺寸(非正方形)

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

1
你需要像询问别人一样提出问题,包括任何相关的关键词,以帮助未来的其他人(否则可能会被投票关闭)。 - dsgriffin
1个回答

34

我在网上搜索到了许多可能的解决方案,但是它们全部转换成了正方形尺寸。因此,我收集了一些并自己解决了这个问题。

将以下代码保存为 .jsx 文件,并放到你的 Photoshop \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);

我获取大部分代码的地方:https://coffeeshopped.com/2008/11/conditional-image-resizing-with-photoshop-and-javascript - tibelchior

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接