背景:有个朋友让我写一个批量四宫格裁切图片的程序,我吹牛答应了,写了一个程序实现了。但是发现裁切后的图片失真,大小也变小,以失败告终。硬着头皮尝试Adobe PhotoshopScripting来实现。
优点:保持原图的分辨率和CMYK颜色模式、TIF格式,真正的无损批量裁切
1、电脑D盘创建文件夹“'D:\\tupian' ”,把要处理的图片拷贝到这个文件夹下
2、电脑D盘创建文件夹“'D:\\tupian_new'”,运行脚本后的图片会保证在这个文件夹下
3、复制分割线一下代码,记事本保存,然后修改扩展名为“.jsx”
4、打开Photoshop,把脚本拖入ps即可自动批量处理
参考:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#target photoshop
var INPUT_PATH = 'D:\\tupian' //输入路径
var OUTPUT_PATH = 'D:\\tupian_new' //输出路径
var LIMIT_SIZE = 1024 // 最大边长
var SAVE_FORMAT = 'tif' // 保存格式 jpg, png, webjpg, webpng,tif
var SAVE_quality = 60 // 图片质量[0, 100]
var TEST_MODE = false // 画质测试模式
function Main(){
if(TEST_MODE==true)
{
TestMode();
alert("脚本执行完毕");
return;
}
fileOut = new File(OUTPUT_PATH + "\\P;);
("w", "TEXT", "????");
("开始批处理\n");
var input_folder = new Folder(INPUT_PATH);
if(input_folder!=null)
{
var files = in ("*.*");
var i=0;
while(files[i]!=null ){
var fp = a(new File(files[i]));
+"\n");
if(fp !=null){
try{
//Resize(LIMIT_SIZE);
Crop();
var filename = (0,("."))+"_1";
Save(OUTPUT_PATH, filename, SAVE_FORMAT, SAVE_QUALITY);
a);
}catch(e){
//alert + ": " + e.message);
+ ": " + e.message+"\n");
}
}
fp = a(new File(files[i]));
if(fp !=null){
try{
Crop2();
var filename = (0,("."))+"_2";
Save(OUTPUT_PATH, filename, SAVE_FORMAT, SAVE_QUALITY);
a);
}catch(e){
//alert + ": " + e.message);
+ ": " + e.message+"\n");
}
}
fp = a(new File(files[i]));
if(fp !=null){
try{
Crop3();
var filename = (0,("."))+"_3";
Save(OUTPUT_PATH, filename, SAVE_FORMAT, SAVE_QUALITY);
a);
}catch(e){
//alert + ": " + e.message);
+ ": " + e.message+"\n");
}
}
fp = a(new File(files[i]));
if(fp !=null){
try{
Crop4();
var filename = (0,("."))+"_4";
Save(OUTPUT_PATH, filename, SAVE_FORMAT, SAVE_QUALITY);
a);
}catch(e){
//alert + ": " + e.message);
+ ": " + e.message+"\n");
}
}
i++;
}
alert("脚本执行完毕");
}
("批处理结束\n");
}
function Resize(LimitSize){
var w = a;
var h = a;
if(w> LimitSize || h >LimitSize){
if(w>h){
var per=LimitSize/w*100;
a(per+"%",per+"%", null, Re);
}
else{
var per=LimitSize/h*100;
a(per+"%",per+"%", null, Re);
}
}
}
function Crop(){
var w=a;
var h=a;
var ww=w/2;
var hh=h/2;
//定义一个变量[bounds],用来表示文档需要裁切的区域,即裁切从坐标[0,0]至[140,104]的区域。
//注意Photoshop坐标原点在左上角。
var bounds = [0, 0, ww, hh];
//定义一个变量[angle],用来设置裁切的旋转角度为0。
var angle = 0;
//调用[document]对象的[crop]方法,来裁切当前文档。
a(bounds, angle);
}
function Crop2(){
var w=a;
var h=a;
var ww=w/2;
var hh=h/2;
//定义一个变量[bounds],用来表示文档需要裁切的区域,即裁切从坐标[0,0]至[140,104]的区域。
//注意Photoshop坐标原点在左上角。
var bounds = [ww, 0, w, hh];
//定义一个变量[angle],用来设置裁切的旋转角度为0。
var angle = 0;
//调用[document]对象的[crop]方法,来裁切当前文档。
a(bounds, angle);
}
function Crop3(){
var w=a;
var h=a;
var ww=w/2;
var hh=h/2;
//定义一个变量[bounds],用来表示文档需要裁切的区域,即裁切从坐标[0,0]至[140,104]的区域。
//注意Photoshop坐标原点在左上角。
var bounds = [ 0, hh, ww, h];
//定义一个变量[angle],用来设置裁切的旋转角度为0。
var angle = 0;
//调用[document]对象的[crop]方法,来裁切当前文档。
a(bounds, angle);
}
function Crop4(){
var w=a;
var h=a;
var ww=w/2;
var hh=h/2;
//定义一个变量[bounds],用来表示文档需要裁切的区域,即裁切从坐标[0,0]至[140,104]的区域。
//注意Photoshop坐标原点在左上角。
var bounds = [ ww, hh, w, h];
//定义一个变量[angle],用来设置裁切的旋转角度为0。
var angle = 0;
//调用[document]对象的[crop]方法,来裁切当前文档。
a(bounds, angle);
}
function TestMode(){
var input_folder = new Folder(INPUT_PATH);
if(input_folder!=null)
{
var files = in ("*.*");
var fp = a(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);
a);
}
}
}
//format: jpg, png, webjpg, webpng
//quality: [0, 100]
function Save(savePath, filename, format, quality){
saveFile = OUTPUT_PATH+"\\"+filename
if(format == 'jpg'){
saveFile += '.jpg'
q = Ma(quality / 100 * 12)
SaveJPEG(saveFile, q)
}else if(format == 'png'){
saveFile += '.png'
q = Ma((100-quality) / 100 * 9)
SavePNG(saveFile, q)
}else if(format == 'tif'){
saveFile += '.tif'
q =12
SaveTIFF(saveFile, q)
}else if(format == 'webjpg'){
saveFile += '.jpg'
SaveForWeb(saveFile, SaveDocumen, quality)
}else if(format == 'webpng'){
saveFile += '.png'
SaveForWeb(saveFile, SaveDocumen, quality)
}else{
saveFile += '.jpg'
SaveForWeb(saveFile, SaveDocumen, quality)
}
}
function SaveJPEG(saveFile, quality){
jpgSaveOptions = new JPEGSaveOptions();
// embedColorProfile (True to embed the color profile in the document.)
j = true;
// formatOptions (The download format to use.)
// Default: Forma
// Range: Forma,
// Forma,
// Forma
j = Forma;
// 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: Ma
// Range: Ma
// Ma
// Ma
// Ma
// Ma
// Ma
// Ma
j = Ma;
// quality (The image quality setting to use; affects file size and compression)
// Default: 3
// Range: [0,12]
j = quality;
// scans (The number of scans to make to incrementally display the image on the page.
// Valid only for when formatOptions = Forma.)
// Default: 3
// Range: [3,5]
= 3
// typename (The class name of the referenced JPEGSaveOptions object.)
// Read only
ac(new File(saveFile), jpgSaveOptions, true,Ex);
}
function SavePNG(saveFile, compression){
pngSaveOptions = new PNGSaveOptions();
// compression (The compression value)
// Default: 0
// Range: [0,9]
= compression
// interlaced (True to interlace rows)
// Default: false
= false
// typename (The class name of the referenced PNGSaveOptions object.)
// Read only
ac(new File(saveFile), pngSaveOptions, true,Ex);
}
function SaveForWeb(saveFile, format, quality){
var webSaveOptions = new ExportOptionsSaveForWeb();
// blur (Applies blur to the image to reduce artifacts)
// Default: 0.0
webSaveO = 0.0;
// colorReduction (The color reduction algorithm.)
// Default: ColorReduc
// Range: ColorReduc,
// ColorReduc,
// ColorReduc,
// ColorReduc,
// ColorReduc,
// ColorReduc
// ColorReduc,
// ColorReduc
// ColorReduc
webSaveO = ColorReduc
// colors (The number of colors in the palette.)
// Default: 256
webSaveO = 256
// dither (The type of dither)
// Default: Di
// Range: Di
// Di
// Di
// Di
webSaveO = Di
// ditherAmount (The amount of di only when dither = Di)
// Default: 100
webSaveOAmount = 100;
// format (The file format to use)
// Ddefault: SaveDocumen
// Range: SaveDocumen,
// SaveDocumen,
// SaveDocumen,
// SaveDocumen,
// SaveDocumen
webSaveO = SaveDocumen;
// includeProfile (True to include the document’s embedded color profile)
// Default: false
webSaveO = false;
// interlaced (True to download in multiple passes progressive)
// Default: false
webSaveO = false;
// lossy (The amount of lossiness allowed)
// Default: 0
webSaveO = 0;
// matteColor (The colors to blend transparent pixels against.)
// Type: RGBColor
;
// optimized (True to create smaller but less compatible files. Valid only when format = SaveDocumen.)
// Default: true
webSaveO = true;
// PNG8 (Indicates the number of bits; true = 8, false = 24. Valid only when format = SaveDocumen.)
// Default: true
webSaveO = true;
// quality (The quality of the produced image as a percentage)
// Default: 60
// Range: [0, 100]
webSaveO = quality;
// transparency (Indication of transparent areas of the image should be included in the saved image)
// Default: true
webSaveO = true;
// transparencyAmount (The amont of transparency dither. Valid only if transparency = true.)
// Default: 100
webSaveOAmount = 100;
// transparencyDither (The transparency dither algorithm)
// Default: Di
// Range: Di
// Di
// Di
// Di
webSaveODither = Di
// typename (The class name of the referenced ExportOptionsSaveForWeb object.)
// Read only
// webSnap (The tolerance amount within which to snap close colors to web palette colors)
// Default: 0
webSaveO = 0;
ac(new File(saveFile), Ex, webSaveOptions);
}
function SaveTIFF (savePath, quality){
tiffsaveOptions = new TiffSaveOptions();
= true;
tiffsaveOptions.imageCompression = TIFFEncoding.NONE;
ac(new File(saveFile), tiffsaveOptions, true,Ex);
}
Main();
//alert ("finish");