Take the 2-minute tour ×
Graphic Design Stack Exchange is a question and answer site for Graphic Design professionals, students, and enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
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 at 2:05
    
Good idea--just added. –  user24601 Jul 19 at 3:43

2 Answers 2

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

share|improve this answer
    
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 at 1:17
    
That does change the question considerably. –  Alan Gilbertson Jul 18 at 14:30
up vote 0 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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