Adobe InDesign CS5/Script:我可以将TIFF和PNG图像转换为“黑白”吗?
我有一个脚本,可以扫描文档中的所有图像类型,将它们全部转换为黑白图像,然后将黑白图像导出到新文件夹。Adobe InDesign CS5/Script:我可以将TIFF和PNG图像转换为“黑白”吗?,adobe,adobe-indesign,extendscript,Adobe,Adobe Indesign,Extendscript,我有一个脚本,可以扫描文档中的所有图像类型,将它们全部转换为黑白图像,然后将黑白图像导出到新文件夹。 InDesign似乎将导出这些图像文件格式:JPEG、TIFF和PNG(它不导出GIF文件)。 但是,InDesign似乎没有TIFF和PNG文件的ColorSpace.GRAY或ColorSpace enum.GRAY属性(但它对JPEG文件有此属性)。 那么,有没有办法将TIFF和PNG文件转换为黑白文件 在InDesign的Extendscript中 如果有不,不提供的理由是什么
InDesign似乎将导出这些图像文件格式:
JPEG、TIFF和PNG
(它不导出GIF文件)。
但是,InDesign似乎没有TIFF和PNG文件的
ColorSpace.GRAY
或ColorSpace enum.GRAY
属性(但它对JPEG文件有此属性)。
- 那么,有没有办法将TIFF和PNG文件转换为黑白文件 在InDesign的Extendscript中
- 如果有
这是我的代码,到目前为止只导出黑白JPEG文件:
var document=app.activeDocument;
var newFolder=createFolder(文档);//如果是子目录DNE,请使用下面的函数创建此文件夹
保存图像(文档,新文件夹);//使用下面的函数
函数createFolder(doc)
{
尝试
{
/*
*类型将filePath属性(对象类型)强制转换为字符串类型;
*必须是字符串类型才能与子目录变量连接(也是字符串类型)
*/
var docPath=String(doc.filePath);
var子目录=“/BLACK AND WHITE IMAGES”;
}
捕获(e)
{
警报(e.message);
退出();
}
var imagesFolder=docPath+子目录;//连接两个变量
如果(!文件夹(imagesFolder).exists)
{
文件夹(imagesFolder.create();
}
返回imagesFolder;//用于此函数外部的实例化
}//函数createFolder结束
函数SaveAllImage(文档、文件夹)
{
var imgs=doc.allGraphics;
var fileName=“”;
var img=“”;
var imgType=“”;
对于(变量i=0;i
那么,有没有一种方法可以将TIFF和PNG文件转换为黑白格式
InDesign的Extendscript
我不知道
如果没有,那么不为这些文件类型提供黑白转换的原因是什么
他只有上帝知道:D
鉴于您的期望,我宁愿将这些图像操作委托给Photoshop,在那里您可以使用任何格式,任何设置。您可以使用BridgeTalk对象,也可以使用运行Ps(applescript for ex)的hotfolder。
或者,根据您的操作系统,您可以使用doScript调用applescript或visualbasic,并让它们运行您可能有的转换工具
祝你好运
卢瓦克
PS:请参阅此工具。也许它有一些可以用JS调用的API
因此,尽管我一直在开发一个使用
BridgeTalk
的代码版本,但我最近发现,应用于JPEG的任何格式都可以应用于.BMP、.TIF、.EPS和.PNG,方法是在以后通过编程将文件扩展名重命名为所需格式。
但是,.PDF的无法强制转换为不同的文件格式,因为它们的格式不正确且已损坏
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
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