0

I have a script that scans a document for all image types, converts them all to black and white, and then exports the black and white images to a new folder.


It seems as if InDesign will export these image file-formats:  JPEG, TIFF, and PNG (it does not export GIF files).

However, it seems that InDesign doesn't have the ColorSpace.GRAY or ColorSpaceEnum.GRAY properties for TIFF and PNG files (it does have this property for JPEG files, though).


  • So, is there a way to convert TIFF and PNG files to black and white in InDesign's Extendscript?

  • If there is not, what would be the reasoning for not providing black and white conversion for these file types?



Here is my code, that as of now exports only black and white JPEG files:
var document = app.activeDocument;
var newFolder = createFolder(document); // if subdirectory DNE, create this folder with the function below

saveAllImages(document, newFolder); // with the function below



function createFolder(doc)
{
    try
    {
      /*
         * type-casting the filePath property (of type object) into a String type;
         * must be a String type to concatenate with the subdirectory variable (also of type String)
         */
        var docPath = String(doc.filePath);
        var subdirectory = "/BLACK AND WHITE IMAGES";
    }
    catch(e)
    {
        alert(e.message);
        exit();
    }

    var imagesFolder = docPath + subdirectory; // concatenating the two variables
    if(!Folder(imagesFolder).exists)
    {
        Folder(imagesFolder).create();
    }

    return imagesFolder; // for instantiation outside of this function

} // end of function createFolder



function saveAllImages(doc, folder)
{
    var imgs = doc.allGraphics;
    var fileName = "";
    var img = "";
    var imgType = "";

    for(var i = 0; i < imgs.length; i++)
    {
        if(imgs[i].itemLink != null)
        {
            fileName = imgs[i].itemLink.name;
            img = new File(folder + "/" + fileName); // each image instantiated here
            imgType = imgs[i].imageTypeName; // image type is determined here (.tif, .jpg, .png, etc..)

            alert("This is a " + imgType + " file.");

            /*
                * array for image options, instantiated from the function below;
                * options[0] is the "MAXIMUM" image quality property, &
                * options[1] is the "GRAY" image conversion property;
                */
            var options = convertToBlackAndWhite(imgType);

            // each image exported to the new folder here, by file type
            switch(imgType)
            {
                case "GIF":
                    alert("This script cannot convert and export the GIF file:\n\t" + fileName + " !");
                    break;

                case "TIFF":

            case "EPS":

                case "JPG":
                    options[0]; // maximum image quality
                    options[1]; // black & white conversion

                    imgs[i].exportFile(ExportFormat.JPG, img, false);
                    break;

                case "PNG":
                    options[0]; // maximum image quality
                    options[1]; // black & white conversion

                    imgs[i].exportFile(ExportFormat.PNG_TYPE, img, false);
                    break;

                default:
                    alert("\tUnlisted image type: " + imgType + "!\nAdd this type to the switch statement.");
                    break;
            } // end of switch statement

        } // end of if statement
    } // end of for loop

} // end of function saveAllImages



        function convertToBlackAndWhite(fileType)
        {
            // array for image-quality and color-conversion values
            var settings = [];

            // each image exported to the new folder here, by file type
            switch(fileType)
            {
                case "TIFF":

                case "PNG":

            case "EPS":

                case "JPG":
                    settings[0] = "app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM"; // maximum image quality
                    settings[1] = "app.jpegExportPreferences.jpegColorSpace = JpegColorSpaceEnum.GRAY"; // black & white conversion
                    break;

                default:
                    break;

            } // end of switch statement

            return settings; // for instantiation outside of this function

        } // end of function convertToBlackAndWhite
Ian Campbell
  • 2,678
  • 10
  • 56
  • 104

2 Answers2

1

So, is there a way to convert TIFF and PNG files to black and white in InDesign's Extendscript?

Not that I am aware of.

If there is not, what would be the reasoning for not providing black and white conversion for these file types?

He he only God knows :D

Given your expectations, I would rather delegate these image operations to Photoshop where you can use any formats you want, any settings you want. You can either use a BridgeTalk object or have a hotfolder that will run Ps ( applescript for ex ). Or you could use a doScript to call applescript or visualbasic depending on your os and have them run some conversion tool you might have.

Good luck.

Loic

PS: See this tool also. Maybe it has some API you can call with JS http://www.rorohiko.com/wordpress/indesign-downloads/color2gray/

Loic Aigon
  • 506
  • 3
  • 1
  • Hmm ok, well as you are saying formatting the files in Photoshop might be more desirable anyway, so I am looking into using the `BridgeTalk` object (I have found a good reference here: http://forums.adobe.com/message/3334670#3334670). Thanks @Loic! I will post my code when I have figured it out... – Ian Campbell Jun 29 '12 at 20:31
0

So, though I have been working on a version of this code that uses BridgeTalk, I discovered recently that any formatting that is applied to JPEG's can be applied to .BMP's, .TIF's, .EPS's, and .PNG's by programmatically renaming the file extension afterwards to the desired format.

However, .PDF's cannot be coerced into different file formats, because they are then incorrectly formatted and corrupted.

Here is my working code, which is pretty similar to the above code, with a few modifications:

var document = app.activeDocument;
var newFolder = createFolder(document); // if subdirectory images DNE, create this folder with the function below

saveAllImages(document, newFolder); // with the function below

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function createFolder(doc)
{
    try
    {
      /*
         * type-casting the filePath property (of type object) into a String type;
         * must be a String type to concatenate with the subdirectory variable (also of type String)
         */
        var docPath = String(doc.filePath);
        var subdirectory = "/BLACK AND WHITE IMAGES";
    }
    catch(e)
    {
        alert(e.message);
        exit();
    }

    var imagesFolder = docPath + subdirectory; // concatenating the two variables
    if(!Folder(imagesFolder).exists)
    {
        Folder(imagesFolder).create();
    }

    return imagesFolder; // for instantiation outside of this function

} // end of function createFolder

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function saveAllImages(doc, folder)
{
    var imgs = doc.allGraphics;
    var fileName = "";
    var img = "";
    var imgType = "";

    for(var i = 0; i < imgs.length; i++)
    {
        if(imgs[i].itemLink != null)
        {
            fileName = imgs[i].itemLink.name;
            var str = fileName.substr(0, fileName.length - 4) + ".tif"; // converting all file types to .TIF
            img = new File(folder + "/" + str); // each image instantiated here
            imgType = imgs[i].imageTypeName; // image type is determined here (.tif, .jpg, .png, etc..)

            //alert("The file \"" + fileName + "\"\n\tis a " + imgType + " file.");

            /*
                * array for image options, instantiated from the function below;
                * options[0] is the "MAXIMUM" image quality property, &
                * options[1] is the "GRAY" image conversion property;
                */
            var options = convertToBlackAndWhite(imgType);

            // each image exported to the new folder here, by file type
            switch(imgType)
            {
                case "GIF":
                    alert("This script cannot convert and export the GIF file:\n\t" + fileName + " !");
                    break;

                case "Adobe PDF":
                    // PDF file type does not have the maximum image quality property
                    options[1]; // black & white conversion

                    imgs[i].exportFile(ExportFormat.PDF_TYPE, img, false);
                    break;

                case "EPS":
                    // PDF file type does not have the maximum image quality property
                    options[1]; // black & white conversion

                    imgs[i].exportFile(ExportFormat.EPS_TYPE, img, false);
                    break;

                case "Windows Bitmap":
                case "PNG":
                case "TIFF":
                case "JPEG":
                    options[0]; // maximum image quality
                    options[1]; // black & white conversion

                    imgs[i].exportFile(ExportFormat.JPG, img, false);
                    break;

                default:
                    alert("\tUnlisted image type: " + imgType + "!\nAdd this type to the switch statement.");
                    break;
            } // end of switch statement

            replaceWithNewImage(doc, fileName, img); // with the function below

        } // end of if statement
    } // end of for loop

} // end of function saveAllImages

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        function convertToBlackAndWhite(fileType)
        {
            // array for image-quality and color-conversion values
            var settings = [];

            // each image exported to the new folder here, by file type
            switch(fileType)
            {
                case "Adobe PDF":
                    settings[0] = ""; // PDF file type does not have the maximum image quality property
                    settings[1] = "app.pdfExportPreferences.pdfColorSpace = PDFColorSpace.GRAY"; // black & white conversion
                    break;

                case "EPS":
                    settings[0] = ""; // EPS file type does not have the maximum image quality property
                    settings[1] = "app.epsExportPreferences.epsColorSpace = EPSColorSpace.GRAY"; // black & white conversion
                    break;

                case "Windows Bitmap":
                case "PNG":
                case "TIFF":
                case "JPEG":
                    settings[0] = "app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM"; // maximum image quality
                    settings[1] = "app.jpegExportPreferences.jpegColorSpace = JpegColorSpaceEnum.GRAY"; // black & white conversion
                    break;

                default:
                    break;
            } // end of switch statement

            return settings; // for instantiation outside of this function

        } // end of function convertToBlackAndWhite


Though I did not use BridgeTalk, I am still looking into this and have found a good reference here: http://www.kasyan.ho.com.ua/convert_cmyk-rgb_images_to_grayscale.html.

And for ideas on not overwriting the original images: http://forums.adobe.com/message/4292613#4292613.

Ian Campbell
  • 2,678
  • 10
  • 56
  • 104