Mobile app version of vmapp.org
Login or Join
Kaufman565

: Illustrator script to export eps files to black/transparent only pngs I've modified a script I found for Illustrator to allow me to convert multiple folders full of EPS files to transparent PNGs

@Kaufman565

Posted in: #IllustratorScripting #Png #Transparency

I've modified a script I found for Illustrator to allow me to convert multiple folders full of EPS files to transparent PNGs and it does a fantastic job. The part where I'm stumped is in finding out how to convert all white (#ffffff) to transparency before saving it as a PNG file.

Note/edit: The script already removes the white 'stage' (or background or canvas whatever it's called) and makes nice trimmed PNGs with transparent backgrounds, but I need to select the white if it exists in the image and convert it to transparency. I know this could not be done by removing a path that has its fill color as white before converting to PNG because underneath the white path is usually the larger black path. So it would need to be done after becoming PNG data, unless there's a way to have the white areas just subtract from the layer underneath it somehow.

I've been Googling Illustrator script documentation for an hour already, and the closest I get seems to be to use PNG8 with some sort of ColorReductionMethod, but there isn't an example of how to reduce specific colors (or change them to transparency) that I can find.

Here's my working code that I call epsToPng.js:

var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pngExportOpts;

// Select the source folder.
sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files you want to convert to PNG', '~' );

// If a valid folder is selected
if ( sourceFolder != null )
{
files = new Array();
//fileType = '*.EPS'//prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' );

// Get all files matching the pattern
//files = sourceFolder.getFiles( fileType );
files = find_files(sourceFolder);

if ( files.length > 0 )
{
// Get the destination to save the files
destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted PNG files.', '~' );
for ( i = 0; i < files.length; i++ )
{
sourceDoc = app.open(files[i]); // returns the document object

// Call function getNewName to get the name and file to save the pdf
targetFile = getNewName();

// Call function getPNGOptions get the PNGExportOptions for the files
pngExportOpts = getPNGOptions();



// Export as PNG
sourceDoc.exportFile( targetFile, ExportType.PNG24, pngExportOpts );

sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
}
alert( 'Files are saved as PNG in ' + destFolder );
}
else
{
alert( 'No matching files found' );
}
}

function find_files (dir)
{
return find_files_sub (dir, []);
}

function find_files_sub (dir, array)
{
var f = Folder (dir).getFiles ("*.*");
for (var i = 0; i < f.length; i++)
{
if (f[i] instanceof Folder)
find_files_sub (f[i], array);
else
if (f[i].name.slice (-4).toLowerCase() == ".eps")
array.push (f[i]);
}
return array;
}


/*********************************************************

getNewName: Function to get the new file name. The primary
name is the same as the source file.

**********************************************************/

function getNewName()
{
var ext, docName, newName, saveInFile, docName;
docName = sourceDoc.name;
ext = '.png'; // new extension for png file
newName = "";

for ( var i = 0 ; docName[i] != "." ; i++ )
{
newName += docName[i];
}
newName += ext; // full png name of the file

// Create a file object to save the png
saveInFile = new File( destFolder + '/' + newName );


return saveInFile;
}




/*********************************************************

getPNGOptions: Function to set the PNG saving options of the
files using the PDFSaveOptions object.

**********************************************************/

function getPNGOptions()
{

// Create the PDFSaveOptions object to set the PDF options
var pngExportOpts = new ExportOptionsPNG24();



// Setting PNGExportOptions properties. Please see the JavaScript Reference
// for a description of these properties.
// Add more properties here if you like
pngExportOpts.antiAliasing = true;
pngExportOpts.artBoardClipping = false;
pngExportOpts.horizontalScale = 100.0;
//pngExportOpts.matte = true;
//pngExportOpts.matteColor = 0, 0, 0;
pngExportOpts.saveAsHTML = false;
pngExportOpts.transparency = true;
pngExportOpts.verticalScale = 100.0;




return pngExportOpts;
}


Edit:

The white area in the cat PNG above was from a white vector area which if deleted, would produce a solid black cat.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Kaufman565

2 Comments

Sorted by latest first Latest Oldest Best

 

@Annie732

I've accomplished my goal, however my solution does not involve Illustrator scripts.

I simply use Photoshop actions and batch processing:


Open a typical image in Photoshop and create a new action.
While it's recording, choose the magic wand tool.
Right click on the image and click "Color range".
Select "Highlights" from the dropdown and set both sliders pretty high (80%, 200) and hit OK.
Hit delete on the keyboard.
File>>Save for web
Choose PNG24 and hit "Save...".
Create a folder and save it there with the original name.
Hit "stop" on the action recorder.
File>>Automate>>Batch
Choose your action, a source folder, and NO destination.
Be sure to set a log output file to suppress error dialogs during the batch.


Bingo. All my PNG images have had their white removed automatically in not too many steps.

10% popularity Vote Up Vote Down


 

@BetL875

Uncomment the line pngExportOpts.matte = true; and change the value to false. The value of pngExportOpts.matte defaults to true, so you must set it explicitly in the script.

[Note: I used the original Adobe script and modified it, but I didn't at a glance see any differences between it and your edited version.]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme