1

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:
Cat PNG
The white area in the cat PNG above was from a white vector area which if deleted, would produce a solid black cat.

2
  • If you could provide an example file, that would be great. To me it sounds like you have a white vector fill on top of the object, which masks out a portion of it. However, when you convert that to .png, you are left with solid white color instead of transparency. If it's anything close to that, you should be better off "removing" it, before you convert the file to a raster format. Just sounds like a poorly made mask to me.
    – Joonas
    Jul 18, 2014 at 2:05
  • Good idea--just added.
    – user24601
    Jul 19, 2014 at 3:43

2 Answers 2

0

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.]

2
  • Actually that didn't make a difference. That might have fixed an issue with something else but perhaps I wasn't clear enough in my question. The script makes these EPS vectors into nice trimmed transparent-background PNGs, but I just want to select the white FILL where it exists and convert that to transparency.
    – user24601
    Jul 18, 2014 at 1:17
  • That does change the question considerably. Jul 18, 2014 at 14:30
0

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

I simply use Photoshop actions and batch processing:

  1. Open a typical image in Photoshop and create a new action.
  2. While it's recording, choose the magic wand tool.
  3. Right click on the image and click "Color range".
  4. Select "Highlights" from the dropdown and set both sliders pretty high (80%, 200) and hit OK.
  5. Hit delete on the keyboard.
  6. File>>Save for web
  7. Choose PNG24 and hit "Save...".
  8. Create a folder and save it there with the original name.
  9. Hit "stop" on the action recorder.
  10. File>>Automate>>Batch
  11. Choose your action, a source folder, and NO destination.
  12. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.