2012-06-29 50 views
0

我有一个脚本可以扫描所有图像类型的文档,将它们全部转换为黑白,然后将黑白图像导出到新文件夹。
InDesign CS5 /脚本:我可以将TIFF和PNG图像转换为“黑白”吗?


看来,如果InDesign中会导出这些图像文件格式:    JPEG, TIFF, and PNG(它 出口GIF文件)。

然而,似乎InDesign不具有TIFF和PNG文件 ColorSpace.GRAYColorSpaceEnum.GRAY特性(它 确实具有这种属性的JPEG文件,虽然)。


  • 那么,有没有一种方法,TIFF和PNG文件转换为黑白 在InDesign的Extendscript?

  • 如果有而不是,那么为这些文件类型不提供 黑白转换的原因是什么?



这里是我的代码,即截至目前出口只有黑色和白色JPEG文件:

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 

回答

1

那么,有没有一种方法,TIFF和PNG文件转换为黑色和白色 InDesign的Extendscript?

不是我所知道的。

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

他,他只有上帝知道:d

考虑您的期望,我宁愿委派这些图像操作到Photoshop,你可以使用任何你想要的格式,任何你想要的设置。您可以使用BridgeTalk对象,或者使用运行Ps的hotfolder(applescript for ex)。 或者您可以使用doScript来调用applescript或visualbasic,具体取决于您的操作系统,并让他们运行一些您可能拥有的转换工具。

祝你好运。

Loic

PS:另请参阅此工具。也许它有一些API,您可以用JS http://www.rorohiko.com/wordpress/indesign-downloads/color2gray/

+0

嗯好吧,以及你所说的在Photoshop中格式化文件可能更希望无论如何,所以我正在研究使用'BridgeTalk'对象(我在这里找到了一个很好的参考:http://forums.adobe。 COM /消息/ 3334670#3334670)。谢谢@Loic!当我想出来的时候我会发布我的代码... –

0

于是呼,虽然我一直在一个版本的代码,使用BridgeTalk的,我最近发现,被应用到JPEG的任何格式可应用于.BMP的,.TIF的,.EPS和.PNG的以编程方式之后将文件扩展名重命名为所需的格式。

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

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

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,我仍然在寻找到这一点,在这里找到了很好的参考:http://www.kasyan.ho.com.ua/convert_cmyk-rgb_images_to_grayscale.html

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

相关问题