我有一个脚本,它可以扫描文档中的所有图像类型,将它们全部转换为黑白,然后将黑白图像导出到新文件夹。
似乎 InDesign 将导出这些图像文件格式:(
JPEG, TIFF, and PNG
它不导出GIF 文件)。
但是,InDesign 似乎没有 TIFF 和 PNG 文件的
ColorSpace.GRAY
orColorSpaceEnum.GRAY
属性(但它确实具有 JPEG 文件的此属性)。
那么,有没有办法在 InDesign 的 Extendscript 中将 TIFF 和 PNG 文件转换为黑白文件?
如果没有,不为这些文件类型提供黑白转换的原因是什么?
这是我的代码,到目前为止只导出黑白 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