首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InDesign CS5 / Script:我可以将TIFF和PNG图像转换为“黑白”吗?

InDesign CS5 / Script:我可以将TIFF和PNG图像转换为“黑白”吗?
EN

Stack Overflow用户
提问于 2012-06-29 04:38:50
回答 2查看 1.9K关注 0票数 0

我有一个脚本,可以扫描文档中的所有图像类型,将它们转换为黑白,然后将黑白图像导出到一个新文件夹中。

看起来,InDesign将导出这些图像文件格式:JPEG, TIFF, and PNG(它做而不是导出GIF文件)。

但是,InDesign似乎没有用于TIFF和PNG文件的ColorSpace.GRAYColorSpaceEnum.GRAY属性(但确实对文件具有此属性)。

  • 那么,是否有一种方法可以将TIFF和PNG文件转换为黑白文件?
  • 如果存在而不是,那么不为这些文件类型提供黑白转换的理由是什么?

下面是我的代码,到目前为止,它只导出黑白JPEG文件:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-29 09:14:08

那么,是否有一种方法可以将TIFF和PNG文件转换为黑白文件?

据我所知没有。

如果没有,那么不为这些文件类型提供黑白转换的理由是什么?

他只有上帝知道

鉴于您的期望,我宁愿将这些图像操作委托给Photoshop,在那里您可以使用您想要的任何格式,任何您想要的设置。您可以使用BridgeTalk对象,也可以有一个将运行Ps的热文件夹( applescript )。或者,您可以使用doScript调用applescript或visualbasic,这取决于您的操作系统,并让它们运行您可能拥有的一些转换工具。

祝好运。

洛奇

PS:也见此工具。也许它有一些你可以用JS http://www.rorohiko.com/wordpress/indesign-downloads/color2gray/调用的API。

票数 1
EN

Stack Overflow用户

发布于 2012-07-05 23:03:49

因此,虽然我一直在开发使用BridgeTalk的此代码版本,但最近我发现,应用于JPEG的任何格式都可以应用于..BMP、.TIF、..EPS和..PNG,方法是随后通过编程方式将文件扩展名重命名为所需的格式。

但是,..PDF的不能强制转换成不同的文件格式,因为它们随后被错误地格式化和损坏。

下面是我的工作代码,它非常类似于上面的代码,但有一些修改:

代码语言:javascript
复制
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

虽然我没有使用BridgeTalk,但我仍然在研究这个问题,并在这里找到了一个很好的参考:grayscale.html

关于不覆盖原始图像的想法:http://forums.adobe.com/message/4292613#4292613

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11256138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档