Javascript for “Save for Web” in photoshop not pro

2019-06-11 23:07发布

#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
}

I am trying to automate the behavior of "save for web" in Photoshop. I am successful in creating images but its producing low quality image. I am a newbie here. Not sure what is going wrong. If i do it the usual gui way, it generates the good quality.

Image with low sharpness - Generated using javascript for Photoshop

Image with high sharpness - Generated using Photoshop gui

2条回答
冷血范
2楼-- · 2019-06-11 23:25

Admittedly this is a bit old but below answer above is wrong, as it uses JPG settings for saving a PNG. See below for a commented correction.

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);
查看更多
Luminary・发光体
3楼-- · 2019-06-11 23:36

I've had problems with save for web. It might be easier just to save it as 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);
查看更多
登录 后发表回答