用于“保存为Web"的Java脚本.在Photoshop中不能产生高质量的图像 [英] Java script for "Save for Web" in photoshop not producing good quality image

查看:255
本文介绍了用于“保存为Web"的Java脚本.在Photoshop中不能产生高质量的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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中自动执行为网络保存"的行为.我成功创建了图像,但产生的图像质量低下. 我是这里的新手.不知道出了什么问题. 如果我按惯用的gui方式进行操作,它将产生良好的质量.

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.

推荐答案

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

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

这篇关于用于“保存为Web"的Java脚本.在Photoshop中不能产生高质量的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆