【记录】Adobe AI CC 图片批量描摹脚本

Adobe AI CC 图片批量描摹(批量转换为SVG)脚本

三种描摹预设:
黑白
灰度
16色


批量描摹脚本
/*
    By Blackkitty 20170923

    将图像分别描摹为预设的黑白,灰度,16色三种svg并保存
    黑白:black
    灰度:gray
    16色:color
*/

// Main Code [Execution of script begins here]

// Collectable files
var COLLECTABLE_EXTENSIONS = ["bmp", "gif", "giff", "jpeg", "jpg", "pct", "pic", "psd", "png", "tif", "tiff"];

var destFolder, sourceFolder;

// Select the source folder
sourceFolder = Folder.selectDialog('Select the SOURCE folder...', '~');
//sourceFolder = new Folder("C:/Users/<Username>/Desktop/1");

if (sourceFolder != null) {
    // Select the destination folder
    destFolder = Folder.selectDialog('Select the DESTINATION folder...', '~');
    //destFolder = new Folder("C:/Users/<Username>/Desktop/2");
}

if (sourceFolder != null && destFolder != null) {
    //getting the list of the files from the input folder
    var fileList = sourceFolder.getFiles();
    var errorList;
    var tracingPresets = app.tracingPresetsList;

    var TraceList = [
        { TracePreset: "ImgDraw_黑白", SaveName: "black" },
        { TracePreset: "ImgDraw_灰度", SaveName: "gray" },
        { TracePreset: "ImgDraw", SaveName: "color" }
    ];

    // 新建一个文档
    var doc = app.documents.add();

    for (var i = 0; i < fileList.length; ++i) {
        if (fileList[i] instanceof File) {
            try {
                var fileExt = String(fileList[i]).split(".").pop();
                if (isTraceable(fileExt) != true)
                    continue;

                var destFileName = fileList[i].name.substring(0, fileList[i].name.length - fileExt.length - 1) + "_";
                var options = getExpertOption();
                // 将图片导入
                var p = doc.placedItems.add();
                p.file = new File(fileList[i]);

                //  新建一个与图片相同大小的临时文档用于保存
                var tmpdoc = app.documents.add(DocumentColorSpace.RGB, p.width, p.height);
                var p2 = tmpdoc.placedItems.add();
                p2.file = new File(fileList[i]);
                // 描摹
                var t = p2.trace();  
                for (var j = 0; j < TraceList.length; j++) {
                    t.tracing.tracingOptions.loadFromPreset(TraceList[j].TracePreset);
                    app.redraw();
                    var outfile = new File(destFolder + "/" + destFileName + TraceList[j].SaveName);
                    tmpdoc.exportFile(outfile, ExportType.SVG, options);
                }

                p.remove();
                // 关闭临时文档
                tmpdoc.close(SaveOptions.DONOTSAVECHANGES);
            }
            catch (err) {
                errorStr = ("Error while tracing " + fileList[i].name + ".\n" + (err.number & 0xFFFF) + ", " + err.description);
                // alert(errorStr);
                errorList += fileList[i].name + " ";
            }
        }
    }
    fileList = null;
    alert("Batch process complete.");
}
else {
    alert("Batch process aborted.");
}

sourceFolder = null;
destFolder = null;

function isTraceable(ext) {
    var result = false;
    for (var i = 0; i < COLLECTABLE_EXTENSIONS.length; ++i) {
        if (ext == COLLECTABLE_EXTENSIONS[i]) {
            result = true;
            break;
        }
    }
    return result;
}

/** Returns the options to be used for the generated files.
    @return ExportOptionsSVG object
*/
function getExpertOption() {
    // Create the required options object
    var options = new ExportOptionsSVG();

    // 精度2位
    options.coordinatePrecision = 2;

    // 使用UTF8编码
    options.documentEncoding = SVGDocumentEncoding.UTF8;

    //导出字体为SVG字体
    options.fontType = SVGFontType.SVGFONT;

    return options;
}

猜你喜欢

转载自blog.csdn.net/c_duoduo/article/details/78068126
今日推荐