illustrator插件-将位图矢量化-转为SVG-AI插件-临摹-范例

1.illustrator脚本

  脚本由JavaScript语言形式表达,包括一系列表示illustrator内部对象的属性和方法。通过调用这些对象,来实现illustrator内部功能的自动化处理和批处理。脚本分为两部分,一部分是常规的ExtendScript,另一部分是illustrator内置对象。学习如何开发illustrator脚本插件,必须对涉及到的这两块内容有充分的了解,前者作者推荐,JavaScript Tools Guide CC这个文档来学习,下载地址:https://download.csdn.net/download/m0_67316550/86502055,后者推荐,Illustrator_JavaScript_Scripting_Reference_2017,这个文档来了解illustrator软件常用的对象,掌握其调用方式,编写作业功能。下载地址:https://download.csdn.net/download/m0_67316550/86502072

1.1.app.activeDocument

  app是脚本运行环境的内置对象,代表整个应用程序。在Illustrator_JavaScript_Scripting_Reference_2017这个文档中可以查到。如下所示:

app是Adobe®Illustrator®应用程序对象,使用预定义的全局应用程序对象引用,其中包含所有其他Illustrator对象。

  activeDocument是一个Document对象。表示当前AI文档。文档包含在Application对象中。

那些以单词“default”开头的属性是默认的文档设置。 确保只在文档打开时修改这些默认属性。 注意,如果在创建新对象之前将默认属性设置为所需的值,就可以简化脚本,消除了指定具有默认属性的特定属性(如fillColor和stroke)的需要。 文档的颜色空间、高度和宽度只能在创建文档时设置。 不能在现有文档中修改这些属性。

1.2.PlacedItem.trace()

  该函数将置入对象转为矢量对象。使用默认选项将此对象的栅格图形转换为矢量图形。 将放置的图像重新排序到插件组的源图像中,并将其转换为一组与原始图像相似的填充或描边路径。 创建并返回引用tracingObject对象的pluginItem对象。 该矢量化函数,相当于菜单命令临摹,如下图所示:
在这里插入图片描述

2.位图矢量化-范例1

本文展示一个批量将位图转换为SVG矢量图的脚本过程,供读者参考,代码如下所示:

// 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;
}

/** 导出函数参数设置
 */
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;
}

3.位图临摹-范例2

将文件夹下图像文件转为SVG文件。

function LinMOScript() {
    var COLLECTABLE_EXTENSIONS = ["bmp", "gif", "giff", "jpeg", "jpg", "pct", "pic", "psd", "png", "tif", "tiff"];
    var destFolder, sourceFolder;
    sourceFolder = Folder.selectDialog('Select the SOURCE folder...', '~');
    if (sourceFolder != null) {
        destFolder = Folder.selectDialog('Select the DESTINATION folder...', '~');
    }

    if (sourceFolder != null && destFolder != null) {
        var fileList = sourceFolder.getFiles();
        var errorList;
        var tracingPresets = app.tracingPresetsList;

        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;

                    // Add a document in the app
                    var doc = app.documents.add();

                    // Add a placed item
                    var p = doc.placedItems.add();
                    p.file = new File(fileList[i]);

                    // Trace the placed item
                    var t = p.trace();
                    t.tracing.tracingOptions.loadFromPreset(tracingPresets[3]);
                    app.redraw();

                    var destFileName = fileList[i].name.substring(0, fileList[i].name.length - fileExt.length - 1) + "_" + fileExt;
                    var outfile = new File(destFolder + "/" + destFileName);
                    doc.saveAs(outfile);
                    doc.close();
                } catch (err) {
                    var errorStr = ("Error while tracing " + fileList[i].name + ".\n" + (err.number & 0xFFFF) + ", " + err.description);
                    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;
    }
}
插件开发联系
 Q Q 312117271
 手 机 18928899728
 W X 18928899728

4.作者答疑

  如有疑问,敬请留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值