最近用爬虫爬了几千张高清图片回来,由于图片质量太高(几千x几千的分辨率,一张图几十MB),连平常浏览都费劲。因此,就用photoshop自带的ExtendScript Toolkit脚本工具写了个缩图脚本。
脚本支持正常的另存为JPG和PNG,也支持"储存为Wwb所用格式"的JPG和PNG。
#target photoshop
var INPUT_PATH = 'C:\\Users\\Administrator\\Desktop\\input' //输入路径
var OUTPUT_PATH = 'C:\\Users\\Administrator\\Desktop\\output' //输出路径
var LIMIT_SIZE = 1024 // 最大边长
var SAVE_FORMAT = 'webpng' // 保存格式 jpg, png, webjpg, webpng
var SAVE_QUALITY = 60 // 图片质量[0, 100]
var TEST_MODE = false // 画质测试模式
function Main(){
if(TEST_MODE==true)
{
TestMode();
alert("脚本执行完毕");
return;
}
fileOut = new File(OUTPUT_PATH + "\\PhotoShop.log");
fileOut.open("w", "TEXT", "????");
fileOut.write("开始批处理\n");
var input_folder = new Folder(INPUT_PATH);
if(input_folder!=null)
{
var files = input_folder.getFiles ("*.*");
var i=0;
while(files[i]!=null ){
var fp = app.open(new File(files[i]));
if(fp !=null){
try{
Resize(LIMIT_SIZE);
var filename = fp.name.substring(0,fp.name.lastIndexOf("."));
Save(OUTPUT_PATH, filename, SAVE_FORMAT, SAVE_QUALITY);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}catch(e){
//alert (e.name + ": " + e.message);
fileOut.write(e.name + ": " + e.message+"\n");
}
}
i++;
}
alert("脚本执行完毕");
}
fileOut.write("批处理结束\n");
}
function Resize(LimitSize){
var w = app.activeDocument.width.value;
var h = app.activeDocument.height.value;
if(w> LimitSize || h >LimitSize){
if(w>h){
var per=LimitSize/w*100;
app.activeDocument.resizeImage(per+"%",per+"%", null, ResampleMethod.BICUBICSHARPER);
}
else{
var per=LimitSize/h*100;
app.activeDocument.resizeImage(per+"%",per+"%", null, ResampleMethod.BICUBICSHARPER);
}
}
}
function TestMode(){
var input_folder = new Folder(INPUT_PATH);
if(input_folder!=null)
{
var files = input_folder.getFiles ("*.*");
var fp = app.open(new File(files[0]));
if(fp !=null){
Resize(LIMIT_SIZE);
// jpg test
Save(OUTPUT_PATH, 'jpg_0', 'jpg', 0);
Save(OUTPUT_PATH, 'jpg_30', 'jpg', 30);
Save(OUTPUT_PATH, 'jpg_60', 'jpg', 60);
Save(OUTPUT_PATH, 'jpg_80', 'jpg', 80);
Save(OUTPUT_PATH, 'jpg_100', 'jpg', 100);
// png test
Save(OUTPUT_PATH, 'png_0', 'png', 0);
Save(OUTPUT_PATH, 'png_30', 'png', 30);
Save(OUTPUT_PATH, 'png_60', 'png', 60);
Save(OUTPUT_PATH, 'png_80', 'png', 80);
Save(OUTPUT_PATH, 'png_100', 'png', 100);
// webjpg test
Save(OUTPUT_PATH, 'webjpg_0', 'webjpg', 0);
Save(OUTPUT_PATH, 'webjpg_30', 'webjpg', 30);
Save(OUTPUT_PATH, 'webjpg_60', 'webjpg', 60);
Save(OUTPUT_PATH, 'webjpg_80', 'webjpg', 80);
Save(OUTPUT_PATH, 'webjpg_100', 'webjpg', 100);
// webpng test
Save(OUTPUT_PATH, 'webpng_0', 'webpng', 0);
Save(OUTPUT_PATH, 'webpng_30', 'webpng', 30);
Save(OUTPUT_PATH, 'webpng_60', 'webpng', 60);
Save(OUTPUT_PATH, 'webpng_80', 'webpng', 80);
Save(OUTPUT_PATH, 'webpng_100', 'webpng', 100);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
}
//format: jpg, png, webjpg, webpng
//quality: [0, 100]
function Save(savePath, filename, format, quality){
saveFile = OUTPUT_PATH+"\\"+filename
if(format == 'jpg'){
saveFile += '.jpg'
q = Math.round(quality / 100 * 12)
SaveJPEG(saveFile, q)
}else if(format == 'png'){
saveFile += '.png'
q = Math.round((100-quality) / 100 * 9)
SavePNG(saveFile, q)
}else if(format == 'webjpg'){
saveFile += '.jpg'
SaveForWeb(saveFile, SaveDocumentType.JPEG, quality)
}else if(format == 'webpng'){
saveFile += '.png'
SaveForWeb(saveFile, SaveDocumentType.PNG-24, quality)
}else{
saveFile += '.jpg'
SaveForWeb(saveFile, SaveDocumentType.JPEG, quality)
}
}
function SaveJPEG(saveFile, quality){
jpgSaveOptions = new JPEGSaveOptions();
// embedColorProfile (True to embed the color profile in the document.)
jpgSaveOptions.embedColorProfile = true;
// formatOptions (The download format to use.)
// Default: FormatOptions.STANDARDBASELINE
// Range: FormatOptions.STANDARDBASELINE,
// FormatOptions.OPTIMIZEDBASELINE,
// FormatOptions.PROGRESSIVE
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
// matte (The color to use to fill anti-aliased edges adjacent to transparent areas of the image.
// When transparency is turned off for an image, the matte color is applied to transparent areas.)
// Default: MatteType.WHITE
// Range: MatteType.BACKGROUND
// MatteType.BLACK
// MatteType.FOREGROUND
// MatteType.NETSCAPE
// MatteType.NONE
// MatteType.SEMIGRAY
// MatteType.WHITE
jpgSaveOptions.matte = MatteType.NONE;
// quality (The image quality setting to use; affects file size and compression)
// Default: 3
// Range: [0,12]
jpgSaveOptions.quality = quality;
// scans (The number of scans to make to incrementally display the image on the page.
// Valid only for when formatOptions = FormatOptions.PROGRESSIVE.)
// Default: 3
// Range: [3,5]
//jpgSaveOptions.scans = 3
// typename (The class name of the referenced JPEGSaveOptions object.)
// Read only
//jpgSaveOptions.typename
activeDocument.saveAs(new File(saveFile), jpgSaveOptions, true,Extension.LOWERCASE);
}
function SavePNG(saveFile, compression){
pngSaveOptions = new PNGSaveOptions();
// compression (The compression value)
// Default: 0
// Range: [0,9]
pngSaveOptions.compression = compression
// interlaced (True to interlace rows)
// Default: false
pngSaveOptions.interlaced = false
// typename (The class name of the referenced PNGSaveOptions object.)
// Read only
//pngSaveOptions.typename
activeDocument.saveAs(new File(saveFile), pngSaveOptions, true,Extension.LOWERCASE);
}
function SaveForWeb(saveFile, format, quality){
var webSaveOptions = new ExportOptionsSaveForWeb();
// blur (Applies blur to the image to reduce artifacts)
// Default: 0.0
webSaveOptions.blur = 0.0;
// colorReduction (The color reduction algorithm.)
// Default: ColorReductionType.SELECTIVE
// Range: ColorReductionType.PERCEPTUAL,
// ColorReductionType.SELECTIVE,
// ColorReductionType.ADAPTIVE,
// ColorReductionType.RESTRICTIVE,
// ColorReductionType.CUSTOM,
// ColorReductionType.BLACKWHITE
// ColorReductionType.GRAYSCALE,
// ColorReductionType.MACINTOSH
// ColorReductionType.WINDOWS
webSaveOptions.colorReduction = ColorReductionType.SELECTIVE
// colors (The number of colors in the palette.)
// Default: 256
webSaveOptions.colors = 256
// dither (The type of dither)
// Default: Dither.DIFFUSION
// Range: Dither.DIFFUSION
// Dither.NOISE
// Dither.NONE
// Dither.PATTERN
webSaveOptions.dither = Dither.DIFFUSION
// ditherAmount (The amount of dither.Valid only when dither = Dither.DIFFUSION)
// Default: 100
webSaveOptions.ditherAmount = 100;
// format (The file format to use)
// Ddefault: SaveDocumentType.COMPUSERVEGIF
// Range: SaveDocumentType.COMPUSERVEGIF,
// SaveDocumentType.JPEG,
// SaveDocumentType.PNG-8,
// SaveDocumentType.PNG-24,
// SaveDocumentType.BMP
webSaveOptions.format = SaveDocumentType.JPEG;
// includeProfile (True to include the document’s embedded color profile)
// Default: false
webSaveOptions.includeProfile = false;
// interlaced (True to download in multiple passes progressive)
// Default: false
webSaveOptions.interlaced = false;
// lossy (The amount of lossiness allowed)
// Default: 0
webSaveOptions.lossy = 0;
// matteColor (The colors to blend transparent pixels against.)
// Type: RGBColor
//webSaveOptions.matteColor ;
// optimized (True to create smaller but less compatible files. Valid only when format = SaveDocumentType.JPEG.)
// Default: true
webSaveOptions.optimized = true;
// PNG8 (Indicates the number of bits; true = 8, false = 24. Valid only when format = SaveDocumentType.PNG.)
// Default: true
webSaveOptions.PNG8 = true;
// quality (The quality of the produced image as a percentage)
// Default: 60
// Range: [0, 100]
webSaveOptions.quality = quality;
// transparency (Indication of transparent areas of the image should be included in the saved image)
// Default: true
webSaveOptions.transparency = true;
// transparencyAmount (The amont of transparency dither. Valid only if transparency = true.)
// Default: 100
webSaveOptions.transparencyAmount = 100;
// transparencyDither (The transparency dither algorithm)
// Default: Dither.NONE
// Range: Dither.DIFFUSION
// Dither.NOISE
// Dither.NONE
// Dither.PATTERN
webSaveOptions.transparencyDither = Dither.NONE
// typename (The class name of the referenced ExportOptionsSaveForWeb object.)
// Read only
//webSaveOptions.typename
// webSnap (The tolerance amount within which to snap close colors to web palette colors)
// Default: 0
webSaveOptions.webSnap = 0;
activeDocument.exportDocument(new File(saveFile), ExportType.SAVEFORWEB, webSaveOptions);
}
Main();
//alert ("finish");