Skip to content

Instantly share code, notes, and snippets.

@Ashraf-Ali-aa
Last active April 21, 2019 17:38
Show Gist options
  • Save Ashraf-Ali-aa/38a8c0d381c120c2c378 to your computer and use it in GitHub Desktop.
Save Ashraf-Ali-aa/38a8c0d381c120c2c378 to your computer and use it in GitHub Desktop.
trace image to SVG in Illustrator
/**********************************************************
ADOBE SYSTEMS INCORPORATED
Copyright 2005-2012 Adobe Systems Incorporated
All Rights Reserved
NOTICE: Adobe permits you to use, modify, and
distribute this file in accordance with the terms
of the Adobe license agreement accompanying it.
If you have received this file from a source
other than Adobe, then your use, modification,
or distribution of it requires the prior
written permission of Adobe.
*********************************************************/
/**********************************************************
ImageTracing.jsx
DESCRIPTION
This sample gets files specified by the user from the
selected folder and batch processes them and saves them
as AI files in the user desired destination with the same
file name after tracing the raster arts in the files.
**********************************************************/
// Main Code [Execution of script begins here]
// uncomment to suppress Illustrator warning dialogs
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
// Collectable files
var COLLECTABLE_EXTENSIONS = ["bmp", "gif", "giff", "jpeg", "jpg", "pct", "pic", "psd", "png", "tif", "tiff"];
var destFolder, sourceFolder, newdest, oldfolder;
// Select the source folder
//sourceFolder = Folder.selectDialog('Select the SOURCE folder...', '~/Github/Personal/assets/quran-word/');
sourceFolder = new Folder('~/Github/Personal/assets/quran-word/006');
if (sourceFolder != null) {
// Select the destination folder
oldfolder = String(sourceFolder).split("/").pop();
newdest = '~/Github/Personal/assets/SVG/' + oldfolder;
//destFolder = Folder.selectDialog('Select the DESTINATION folder...', newdest);
destFolder = new Folder(newdest);
}
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;
for (var i = 0; i < fileList.length; ++i) {
if (fileList[i] instanceof File) {
try {
var fileExt = String(fileList[i]).split (".").pop();
var destFileName = fileList[i].name.substring(0, fileList[i].name.length - fileExt.length - 1);
if (isTraceable(fileExt) != true)
continue;
var outfile = new File(destFolder + "/" + destFileName + ".svg");
if (outfile.exists)
continue;
// Trace the files by placing them in the document.
// 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[19]);
t.tracing.expandTracing();
app.activeDocument.artboards[0].artboardRect = app.activeDocument.visibleBounds;
app.redraw();
// Call function getSVGOptions get the SVGSaveOptions for the files
svgSaveOpts = getSVGOptions();
// Save as svg
doc.exportFile(outfile, ExportType.SVG, svgSaveOpts);
// doc.saveAs(outfile);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
doc.close();
} 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 getSVGOptions() {
var svgSaveOpts = new ExportOptionsSVG();
//just using defaults aside from what's written below
//see http://cssdk.host.adobe.com/sdk/1.0/docs/WebHelp/references/csawlib/com/adobe/illustrator/ExportOptionsSVG.html
// SVGDocumentEncoding = ASCII UTF8 UTF16
// SVGDTDVersion = SVGTINY1_2 SVGTINY1_1 SVGBASIC1_1 SVG1_1
// STYLEELEMENTS STYLEATTRIBUTES
// svgSaveOpts.documentEncoding = SVGDocumentEncoding.UTF8;
svgSaveOpts.cssProperties = SVGCSSPropertyLocation.STYLEELEMENTS;
// svgSaveOpts.DTD = SVGDTDVersion.SVG1_1;
// svgSaveOpts.fontType = SVGFontType.SVGFONT;
// svgSaveOpts.fontSubsetting = SVGFontSubsetting.None;
svgSaveOpts.compressed = false;
//svgSaveOpts.optimizeForSVGViewer = true;
svgSaveOpts.embedRasterImages = false;
svgSaveOpts.coordinatePrecision = 1;
svgSaveOpts.preserveEditability = false;
return svgSaveOpts;
}
@Ashraf-Ali-aa
Copy link
Author

Modifications:
Line:43 or 44 - select GIF folder to process
Line: 49 - output SVG folder - (manually create the new folder i.e SVG/001/ etc.)
Line: 86 - image trace preset (the custom preset for me was [19] but it would be different for other users - most likely [18] - check out https://forums.adobe.com/message/7058957#7058957)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment