在photoshop中“保存为Web”的Java脚本无法生成高质量的图像

时间:2014-06-13 12:21:11

标签: photoshop photoshop-script

#target photoshop    
var doc = app.activeDocument;     
if (doc == null) {
   throw "No Valid document available for export.";
}    
 if (doc.width != doc.height) {
    throw "Image is not square";
}

var startState = doc.activeHistoryState; // save for undo
var initialPrefs = app.preferences.rulerUnits; // will restore at end
app.preferences.rulerUnits = Units.PIXELS; // use pixels

// Folder selection dialog
var destFolder = Folder.selectDialog( "Select Output folder");

// Save icons in PNG using Save for Web.
var sfw = new ExportOptionsSaveForWeb();
sfw.format = SaveDocumentType.PNG;
sfw.PNG8 = false; // use PNG-24
sfw.transparency = true;    
doc.info = null; // delete metadata
var icons = [    
            {"name": "ic_launcher", "folder_name":"drawable-ldpi","size":36},
            {"name": "ic_launcher", "folder_name":"drawable-mdpi","size":48},
            {"name": "ic_launcher", "folder_name":"drawable-hdpi","size":72},
            {"name": "ic_launcher", "folder_name":"drawable-xdpi","size":96},
            {"name": "ic_launcher120x120", "folder_name":"Others","size":120},
            {"name": "ic_launcher144x144", "folder_name":"Others","size":144},
            {"name": "ic_launcher512x512", "folder_name":"Others","size":512},
            ];

var icon;
for (i = 0; i < icons.length; i++) {
    icon = icons[i];
    //Resolution was 72 while creating the psd for the images.
    doc.resizeImage(icon.size, icon.size, null, ResampleMethod.BICUBIC);
    var destFileName = icon.name + ".png";
    var folderPath = new Folder(destFolder + "/" + icon.folder_name);
    if(!folderPath.exists)
       folderPath.create();
    doc.exportDocument(new File( folderPath + "/"
                       + destFileName), ExportType.SAVEFORWEB, sfw);
    doc.activeHistoryState = startState; // undo resize
}
alert("Android Icons created!");

if (doc != null) {
   doc.close(SaveOptions.DONOTSAVECHANGES);
   app.preferences.rulerUnits = initialPrefs; // restore prefs
}

我正在尝试在Photoshop中自动执行“save for web”的行为。我成功地创建了图像,但却产生了低质量的图像。 我是新手。不确定出了什么问题。 如果我用通常的gui方式做它,它会产生良好的质量。

Image with low sharpness - Generated using javascript for Photoshop

Image with high sharpness - Generated using Photoshop gui

2 个答案:

答案 0 :(得分:1)

我在保存网页时遇到了问题。将它保存为png

可能更容易
var pngFile = new File(afilePath);
pngSaveOptions = new PNGSaveOptions();
pngSaveOptions.embedColorProfile = true;
pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
pngSaveOptions.matte = MatteType.NONE; pngSaveOptions.quality = 1;

activeDocument.saveAs(pngFile, pngSaveOptions, false, Extension.LOWERCASE);

答案 1 :(得分:0)

诚然,这有点旧,但是下面的答案是错误的,因为它使用JPG设置保存PNG。请参阅下面的注释更正。

var image = app.activeDocument;

// set Destination
var destination = new Folder("~/Desktop/Output");
if ( ! destination.exists ) {
    destination.create()
}

// Get original filename
var sourceName = decodeURI(image.name).replace(/\.[^\.]+$/, '');

// Set destination path
var destination = File(destination + "/" + sourceName + ".png");

// PNG save options to use below, using defaults
var pngOpts = new PNGSaveOptions();
pngOpts.compression = 0;
pngOpts.interlaced = false;

// Save
image.saveAs(destination, pngOpts, true, Extension.LOWERCASE);