Java script for “Save for Web” in photoshop not producing good quality image

北慕城南 提交于 2020-01-03 05:48:12

问题


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


回答1:


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);



回答2:


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);


来源:https://stackoverflow.com/questions/24205168/java-script-for-save-for-web-in-photoshop-not-producing-good-quality-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!