• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
6

How can I toggle the "Ignore Color" button with a script?

Community Beginner ,
Oct 25, 2023 Oct 25, 2023

Copy link to clipboard

Copied

Hello there!

I'm still quite new to both scripting and Adobe Illustrator, but I would like to optimize my time by writing a script that can take full black and white images and remove the white.

My Frankenstein code starts by opening a test folder (I removed the location from the image Debug1, but I know where it points to) and grabbing an array of all files.

Then it converts these images to svg by the end of the function. In the midst of this transition, it places the image on a new document, traces it using a preset I call "Ignore White", and then redraws the image with the new paths (or at least that's how I think it works).

Anyway, my main problem is that when I call "options.loadFromPreset(tracingPresets[19])" (image in Debug2, line 67), the alert shows that my preset does load and that it has the correct name, but when I run the script, I can clearly see in Debug3 that the Image Trace settings did not update properly to "Ignore Color" (aka Ignore White), AND the preset is called "Custom" and not "Ignore White".

 

Does anyone know how I can toggle the "Ignore Color" button in real time so that my SVG will actually remove the white and be fully transparent?

P.S. (I already use "options.ignoreWhite = true;" on line 71, and that definitely doesn't work NOR does it show in Debug3 when the script is paused due to the alert on line 74)

 

Any and all help would be greatly appreciated!

TOPICS
Bug , Experiment , Import and export , Scripting

Views

840

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Oct 27, 2023 Oct 27, 2023

I'm also having this issue with the ignore white option no longer working. The most recent version of illustrator has swapped the ignore white tik box with Ignore Color and an eyedropper which has to be the issue. I've tried adding the method for abutting and trying to add an option for ignoreColor into the script. but ignore white still doesnt work. There has to be another option that isnt in the documentation out there yet.

The only thing I've found that somewhat works is:
-creating a preset for

...

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 25, 2023 Oct 25, 2023

Copy link to clipboard

Copied

Hi @VMPrinting, it might help to set all the tracing options yourself. Have a look at the Tracing Object, and more importantly, TracingOptions.

 

Here is a script for example:

(function () {

    var doc = app.activeDocument,
        item = doc.selection[0];

    if (typeof item.trace !== 'function') {
        alert('Please select a traceable page item.');
        return;
    }

    var itemTrace = item.trace(),
        tracingOptions = itemTrace.tracing.tracingOptions;

    tracingOptions.tracingMode = TracingModeType.TRACINGMODEBLACKANDWHITE;
    tracingOptions.ignoreWhite = true;
    tracingOptions.threshold = 128;
    tracingOptions.pathFidelity = 50;
    tracingOptions.cornerFidelity = 20;
    tracingOptions.fills = true;
    tracingOptions.strokes = false;
    tracingOptions.cornerAngle = 135;

    // var tracedItem = tracingOptions.expandTracing();
    // tracedItem.selected = true;

})();

 

If you need to use a preset, then the TracingOptions object has "loadFromPreset" and "storeToPreset" methods. You could load the one you want, and then set the tracingOptions.ignoreWhite property.

- Mark

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 26, 2023 Oct 26, 2023

Copy link to clipboard

Copied

Hey Mark,

I appreciate you providing this sample code, and I do understand that I could set all of the tracing options myself, but as I previously stated, "my main problem is that when I call "options.loadFromPreset(tracingPresets[19])" (image in Debug2, line 67), the alert shows that my preset does load and that it has the correct name, but when I run the script, I can clearly see in Debug3 that the Image Trace settings did not update properly to "Ignore Color" (aka Ignore White), AND the preset is called "Custom" and not "Ignore White"."

 

It's not so much that I haven't tried using "loadFromPreset". It's that "loadFromPreset" does not work as intended. My preset SHOULD load and "ignoreWhite" should automatically be true. What is the point of loading a preset if I have to update everything myself anyway?

 

Again, I appreciate you trying, but this was not the answer I was looking for.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2023 Oct 26, 2023

Copy link to clipboard

Copied

Hi @VMPrinting, my apologies I didn't read your post properly at all!

 

If you do not get a satisfactory answer, please do the following: create a test .ai document and simplified version of your script that (a) shows the bug with a minimum of extraneous features, and (b) can be reproduced on anyone's computer (hopefully) so because I don't have 19 tracing presets I can test with your preset—does the bug appear with a built-in preset? If you can set it up so people can run it and confirm (or otherwise) your bug with a minimum of confounding factors, then this is ideal and you will get the most help.

- Mark

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 25, 2023 Oct 25, 2023

Copy link to clipboard

Copied

@VMPrinting,

It will be easy if you have share code as a text as it helps to debug easily. Also, as per the documentation the setting of property ignoreWhite work only when tracingMethod is set to TracingMethodType.TRACINGMETHODABUTTING.

Best regards

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 26, 2023 Oct 26, 2023

Copy link to clipboard

Copied

@Charu Rajput, I have posted my code below in another comment. I will try updating the tracing method to determine if that fixes it and report back.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 26, 2023 Oct 26, 2023

Copy link to clipboard

Copied

/* Here is the full code as text.
will process all jpg and png files from folder like :
e.g. take some file like my-exported-graphic.jpg, trace it and export it as my-exported-graphic.svg
*/

// SPECIFY YOUR OWN PATHS OR USE selectFolder() instead
var origin = Folder('');
var destination = Folder('');

var files = getFiles(origin);
convertFiles(files);
// select folder for import
function selectFolder() {
// allow user to select from dialog
return Folder.selectDialog('Please select the folder to be imported:', Folder('~/Development/sandbox/cyberpunk/refacto/my-tests'));
}

function getFiles(folder) {
return folder ? folder.getFiles() : undefined;
}

function convertFiles(files) {
if (files) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (!file || !(file instanceof File)) continue;
var filename = file.name;
var valid = (filename.indexOf('.jpg') != -1) || (filename.indexOf('.png') != -1)
if (!valid) {
continue;
} else {
convertFile(files[i]);
}
}
} else {
alert('no file to convert');
}
}

function write (document, filename) {
var options = new ExportOptionsSVG();
options.documentEncoding = SVGDocumentEncoding.UTF8;
var file = new File(destination + '/' + filename);
document.exportFile(file, ExportType.SVG, options);
}

function convertFile (file) {
var filename = file.name;
// create new document
var document = app.documents.add();
try {
var layer = document.layers[0];
layer.name = filename.substring(0, filename.indexOf('.'));
// place image file in the layer
var placed = layer.placedItems.add();
placed.file = file;
// position placed image in the document
placed.top = document.height;
placed.left = 0;
// trace image with custom options derived from [Default] preset
var plugin = placed.trace();
var tracing = plugin.tracing;
var options = tracing.tracingOptions;
var tracingPresets = app.tracingPresetsList;
plugin.name = layer.name;
alert(tracingPresets[19]);
options.loadFromPreset(tracingPresets[19]);
// apply tracing change and expand
alert('The Ignore White should be true: ' + options.ignoreWhite);
app.redraw();
options.ignoreWhite = true;
options.ignoreColor = true;
options.strokes = true;
alert('The Ignore White should be true: ' + options.ignoreWhite);
app.redraw();
alert('The app should have waited 5 seconds');
tracing.expandTracing().selected = true;
//tracing.expandTracing();
// resize artboard to traced image
var artboard = document.artboards[0];
artboard.artboardRect = [0, placed.height, placed.width, 0];
// easier to position group to origins first
var element = document.pageItems[0];
element.position = [0, document.artboards[0].artboardRect[1]];
// then actually ungroup
var group = document.groupItems[0];
var nested = group.pageItems;
var count = nested.length;
for (var i = count - 1; i >= 0; i--) {
nested[i].move(layer, ElementPlacement.PLACEATBEGINNING);
}
// export as SVG
var outname = layer.name + '.svg';
write(document, outname);
document.close();
} catch (error) {
document.close(SaveOptions.DONOTSAVECHANGES);
}
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2023 Oct 26, 2023

Copy link to clipboard

Copied

Hi @VMPrinting, no big deal, but in the future please use the code </> button in the editor toolbar to format code and if you have screenshots, post them inline with the graphic icon in the editor toolbar. Attached graphics (as opposed to inline graphics) are very slow to load and some people *cough* sorry... won't bother loading them.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 27, 2023 Oct 27, 2023

Copy link to clipboard

Copied

I'm also having this issue with the ignore white option no longer working. The most recent version of illustrator has swapped the ignore white tik box with Ignore Color and an eyedropper which has to be the issue. I've tried adding the method for abutting and trying to add an option for ignoreColor into the script. but ignore white still doesnt work. There has to be another option that isnt in the documentation out there yet.

The only thing I've found that somewhat works is:
-creating a preset for the tracing settings you want
-going all the way into Users>you>app settings>roaming>adobe>the newest illustrator> en_US>x64>Vectorizing Presets, opening that with notepad
-copying the settings from the collection at the top (which will be the newest preset you made)
-and pasting them over the settings down in collection 7 (which is the default settings used when you click Image Trace).

at least that way, calling image trace defaults to the desired settings. but this is a very annoying work around which still leaves my scripts calling for an option that are no longer used. What would really help is if A) you could either set the default settings in illustrator (as well as for magic wand while we're at it) OR B) allow recorded actions to store image trace settings in the action rather than just using the default. Hoping somebody figures out what can be done about this change in javascript soon.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 27, 2023 Oct 27, 2023

Copy link to clipboard

Copied

Interesting! So, do you think this is a bug introduced when they changed "ignore white" to "ignore color"? In other words, they have updated the native feature but not the scripting API for it? If anyone has confirmed this bug (and it sounds like you have @Isaac265865188lnf) please post a bug report..

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 28, 2023 Oct 28, 2023

Copy link to clipboard

Copied

I am far too much of a scripting novice to know anything like that with certainty, sadly. That "solution" is something I found from another user (I would very much like to credit them for it but I've long since lost the page where I found it). I may have come in a little hot via frustration - my thinking it's an introduced bug is purely speculation and, again, I dont know nearly enough about any of this to present this theory as fact to anyone who actually knows the ins and outs of it. I was just excited to find other people who hit this wall too.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 28, 2023 Oct 28, 2023

Copy link to clipboard

Copied

Hey Isaac, I appreciate your post. Makes me feel validated here haha. I agree that they must have added some things to ignoreColor because I also tried setting ignoreColor to true and it didn't throw an error. That told me that ignoreColor must be a variable of some type, but I agree that nothing is in the documentation yet to validate our claims.

 

As for your work around, I tip my hat to you good sir, haha!

I think that is a really smart solution, and although very tedious, if you know what it is that you want, and it's not going to change, I think your solution is spectacular!

 

As for me, I ended up leaving my code as is, but I left an alert right after "app.redraw()" and before "tracing.expandTracing().selected = true". This break in the code allows me to manually click "ignoreWhite" and then hit "OK" on the alert. Everything else is automated. The tracing, the expanding, the exporting as both a PNG and a SVG. It's all done. It's just this darn "ignoreWhite/color" that prevents it from being 100% autonomous.

But, still, I saved myself hours of work with this work around, so I'll chalk it up to a win.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 02, 2023 Nov 02, 2023

Copy link to clipboard

Copied

LATEST

Your votes can help alert Adobe engineers that there are many problems with the Tracing API: https://illustrator.uservoice.com/forums/601447-illustrator-desktop-bugs/suggestions/45506401-image-...

 

What changes have I found in plugin SDK versions, but not in ExtendScript?

Adobe Illustrator SDK CC 2022 file AIVectorize.h new lines:

 

 

/** Tracing option key for whether to ignore "color" background or not. Boolean. New for CS3. */
#define kTracingShouldIgnoreColorsKey "adobe/vectorize/output/shouldIgnoreColors"

/** Tracing option key to store colors that have to be ignored from tracing option. Boolean. New for CS3. */
#define kTracingIgnoreColorsKey "adobe/vectorize/output/ignoreColors"

#define kTracingIgnoreWhiteDefault (false)

 

 

Adobe Illustrator SDK CC 2024 file AIVectorize.h new lines:

 

 

constexpr AIRGBColor kTracingWhiteColor = { 65535, 65535, 65535 };
constexpr AIRGBColor kTracingIgnoreColorDefault = kTracingWhiteColor;

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines