每次我想将图像创建为不同的分辨率时,都必须单击“保存为网络”并更改分辨率,这很乏味。有没有办法编写一个脚本来自动实现这一点,多个分辨率为一个?
问问题
3183 次
3 回答
4
为此我自己使用这个功能
function saveDocumentAsPng(d, width, height) {
var fileName = d.fullName.toString();
if(fileName.lastIndexOf(".") >= 0) { fileName = fileName.substr(0, fileName.lastIndexOf("."));}
fileName += "_wxh.png".replace("w", width).replace("h", height);
var exportOptions = new ExportOptionsPNG24();
exportOptions.horizontalScale = exportOptions.verticalScale = 100 * Math.max(width/d.width, height/d.height);
var file = new File(fileName);
app.activeDocument = d;
d.exportFile(file, ExportType.PNG24, exportOptions);
}
请注意,它使用“_[width]x[height]”后缀命名生成的 png。
我根据 adobe 的 javascript 参考http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/pdf/illustrator/scripting/illustrator_scripting_reference_javascript_cs5.pdf(第 59 页)中的示例编写了我的函数)。
于 2013-11-13T12:03:02.340 回答
1
这是一篇旧文章,但它启发了我为我想分享的 Android 项目创建一个不同的版本。只需创建一个 144x144 像素的文档并运行它:
var doc = app.activeDocument;
// This is the scale factor that you can see in "Percent" textbox of
// the "Save for Web..." dialog box while changing the size of the image
var scale = [100, 66.67, 50, 33.33]
// This is the image size for the PNGs files names
var size = [144, 96, 72, 48]
for(var i = 0; i<4; i++)
{
var fileName = doc.fullName.toString();
if (fileName.lastIndexOf(".") >= 0) {
// Get the file name without the extension
fileName = fileName.substr(0, fileName.lastIndexOf("."));
}
// Name each file with yours size to avoid overwrite
fileName += "_wxh.png".replace("w", size[i]).replace("h", size[i]);
var exportOptions = new ExportOptionsPNG24();
exportOptions.horizontalScale = exportOptions.verticalScale = scale[i];
exportOptions.artBoardClipping = true;
var file = new File(fileName);
doc.exportFile(file, ExportType.PNG24, exportOptions);
}
Window.alert ("Successfully exported!", "Success");
谢谢!
于 2016-05-28T22:21:04.960 回答
0
这是将所选文件夹的所有文件导出为 PNG 的脚本。您可以在此代码中指定分辨率,图像质量会很好。在此代码中,默认分辨率为 600
var folder = Folder.selectDialog();
if (folder) {
var files = folder.getFiles("*.ai");
for (var i = 0; i < files.length; i++) {
var currentFile = files[i];
app.open(currentFile);
var activeDocument = app.activeDocument;
var pngFolder = Folder(currentFile.path + "/PNG");
if (!pngFolder.exists)
pngFolder.create();
var fileName = activeDocument.name.split('.')[0] + ".png";
var destinationFile = File(pngFolder + "/" + fileName);
// Export Artboard where you can set resolution for an image. Set to 600 by default in code.
var opts = new ImageCaptureOptions();
opts.resolution = 600;
opts.antiAliasing = true;
opts.transparency = true;
try {
activeDocument.imageCapture(new File(destinationFile), activeDocument.geometricBounds, opts);
} catch (e) {
}
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
currentFile = null;
}
}
如果要导出为 jpg 格式,只需在以下行的代码中更改文件的扩展名
var fileName = activeDocument.name.split('.')[0] + ".png";
改成
var fileName = activeDocument.name.split('.')[0] + ".jpg"; // For jpg
此外,您可以根据需要更改文件的名称以及导出文件的存储位置。
希望我的回答对你有所帮助。
于 2017-09-07T11:21:53.733 回答