Photoshop CC 2017脚本导出图像质量

时间:2017-01-16 23:43:21

标签: javascript photoshop

我为PhotoShop编写了一个JavaScript,它循环遍历数组并调整大小,附加文件名并保存图像。该脚本有效,但图像质量严重下降,我似乎无法弄清楚如何调整选项以保持图像质量。我的原始图像是专业拍摄的照片@ 3871x2571像素,因此质量差异很明显。

JS:

doc = app.activeDocument;
doc.changeMode(ChangeMode.RGB);
var docName = doc.name;
var docTitle = docName.slice(0, -4);

var options = new ExportOptionsSaveForWeb();
options.quality = 100;
options.format = SaveDocumentType.JPEG;
options.optimized = true;

var sizes = new Array();
sizes[0] = 'sm';
sizes[1] = 'md';
sizes[2] = 'lg';
sizes[3] = 'xl';

//2x
for(var i  = 0; i < 4; i++) {
    var widthVal = '';
    if(sizes[i] == 'sm') { widthVal = '480px'; }
    if(sizes[i] == 'md') { widthVal = '960px'; }
    if(sizes[i] == 'lg') { widthVal = '1440px'; }
    if(sizes[i] == 'xl') { widthVal = '1920px'; }

    var newName  = docTitle+'_'+sizes[i]+'@2x.jpg';

    doc.resizeImage(widthVal,null, null, ResampleMethod.BICUBIC,0);

    doc.exportDocument(File(doc.path+'/'+newName), ExportType.SAVEFORWEB, options);
}

//1x
for(var i  = 0; i < 4; i++) {
    var widthVal = '';
    if(sizes[i] == 'sm') { widthVal = '240px'; }
    if(sizes[i] == 'md') { widthVal = '480px'; }
    if(sizes[i] == 'lg') { widthVal = '720px'; }
    if(sizes[i] == 'xl') { widthVal = '960px'; }
    var newName  = docTitle+'_'+sizes[i]+'.jpg';

    doc.resizeImage(widthVal,null, null, ResampleMethod.BICUBIC,0);

    doc.exportDocument(File(doc.path+'/'+newName), ExportType.SAVEFORWEB, options);
}

1 个答案:

答案 0 :(得分:0)

根据我对OP的评论,这确实是问题所在。通过打开和关闭文件以在每个循环内处理,我能够按预期工作。它使速度变慢,但仍然比逐个保存每个文件更快!

不一定是个问题,但我希望通过首先提示输入文件来改进此代码。 :d

    var options = new ExportOptionsSaveForWeb();
    options.quality = 100;
    options.format = SaveDocumentType.JPEG;
    options.optimized = true;

    var sizes = new Array();
    sizes[3] = 'sm';
    sizes[2] = 'md';
    sizes[1] = 'lg';
    sizes[0] = 'xl';

    //2x
    for(var i  = 0; i < 4; i++) {
        docFile = File("[full system path to file]");
        doc = open(docFile);
        doc.changeMode(ChangeMode.RGB);
        var docName = doc.name;
        var docTitle = docName.slice(0, -4);

        var widthVal = '';
        if(sizes[i] == 'xl') { widthVal = '1920px'; }
        if(sizes[i] == 'lg') { widthVal = '1440px'; }
        if(sizes[i] == 'md') { widthVal = '960px'; }
        if(sizes[i] == 'sm') { widthVal = '480px'; }

        var newName  = docTitle+'_'+sizes[i]+'@2x.jpg';

        doc.resizeImage(widthVal,null, 3000, ResampleMethod.BICUBIC,0);

        doc.exportDocument(File(doc.path+'/'+newName), ExportType.SAVEFORWEB, options);

        doc.close(SaveOptions.DONOTSAVECHANGES);
    }

    //1x
    for(var i  = 0; i < 4; i++) {
        docFile = File("[full system path to file]");
        doc = open(docFile);
        doc.changeMode(ChangeMode.RGB);
        var docName = doc.name;
        var docTitle = docName.slice(0, -4);

        var widthVal = '';
        if(sizes[i] == 'xl') { widthVal = '960px'; }
        if(sizes[i] == 'lg') { widthVal = '720px'; }
        if(sizes[i] == 'md') { widthVal = '480px'; }
        if(sizes[i] == 'sm') { widthVal = '240px'; }
        var newName  = docTitle+'_'+sizes[i]+'.jpg';

        doc.resizeImage(widthVal,null, 3000, ResampleMethod.BICUBIC,0);

        doc.exportDocument(File(doc.path+'/'+newName), ExportType.SAVEFORWEB, options);

        doc.close(SaveOptions.DONOTSAVECHANGES);
    }