用于批量更改打開的圖片的尺寸的photoshop 腳本 (javascript)。
操作對象:
當(dāng)前打開的所有文檔;(注意不要有尚未保存過的新建的文檔,或無法直接保存的文檔,否則會中斷批處理)
用法:
把解壓出來的 “改變尺寸.jsx” 文件復(fù)制到 “ps安裝目錄\預(yù)置\腳本” 下,重新打開ps以后就可以在~ [菜單- 文件-腳本] 里面找到 “改變尺寸”
或者解壓出來,在開著ps的情況下,直接雙擊也可以用。
操作結(jié)果:
直接覆蓋保存并關(guān)閉文檔。(最好復(fù)制一個文件夾,然后再打開圖片執(zhí)行這個操作,不然就沒有原始文件嘍)
目前只在cs3中文版上測試通過
若其他ps版本有問題,或者有什么bug還請多多反饋!謝謝! 對這個沒什么研究,剛學(xué)~尚苦于沒有資料,更歡迎高人多多指點
v1.0版本下載(old)
附上代碼,方便大家指正:
#target photoshop app.bringToFront(); // 帶自動判斷橫豎的統(tǒng)一長寬操作,對當(dāng)前所有打開文檔進(jìn)行操作。因為是直接保存關(guān)閉,原文件要注意備份! if (app.documents.length == 0){ alert("請打開至少一個文件"); }else{ var PRESOLUTION = 72; app.preferences.rulerUnits = Units.PIXELS; res ="dialog { \ text:'批量尺寸調(diào)整 - 直接覆蓋保存-請注意備份',\ group: Group{orientation: 'column',alignChildren:'left',\ corrdination: Panel { orientation: 'row', \ text: '需要尺寸', \ x: Group { orientation: 'row', \ s: StaticText { text:'長:' }, \ e: EditText { preferredSize: [50, 20] } ,\ p: StaticText { text:'pixel' }, \ }, \ y: Group { orientation: 'row', \ s: StaticText { text:'寬:' }, \ e: EditText { preferredSize: [50, 20] }, \ p: StaticText { text:'pixel' }, \ } \ }, \ a:Group{ orientation: 'row', \ c: Checkbox { preferredSize: [16, 16]} ,\ s: StaticText {text:'保持原圖長寬比,新尺寸僅作外框限定'},\ }, \ b:Group{ orientation: 'row', \ c: Checkbox { preferredSize: [16, 16]} ,\ s: StaticText {text:'不判斷原圖橫豎'},\ }, \ },\ buttons: Group { orientation: 'row', alignment: 'right',\ Btnok: Button { text:'確定', properties:{name:'ok'} }, \ Btncancel: Button { text:'取消', properties:{name:'cancel'} } \ } \ }"; win = new Window (res); win.buttons.Btncancel.onClick = function () { this.parent.parent.close(); } win.group.a.c.onClick = function(){ //如果勾選了a,則b被鎖定 if(win.group.a.c.value){ win.group.b.c.value=true; } } win.group.b.c.onClick = function(){ if(win.group.a.c.value){ win.group.b.c.value=true; } } win.buttons.Btnok.onClick = function () { var x=Number(win.group.corrdination.x.e.text); var y=Number(win.group.corrdination.y.e.text); var a=win.group.a.c.value; var b=win.group.b.c.value; //選中則不顛倒橫豎,以輸入值為準(zhǔn) if (!b && x<y){ //確保PW>=PH var PW=y var PH=x }else{ var PW=x var PH=y } while (app.documents.length){ var docRef = app.activeDocument; // 當(dāng)前活動文檔為操作對象 var dw = docRef.width; var dh = docRef.height; var fw = PW; var fh = PH; if(!b && dh>dw){ fw = PH; fh = PW; } if(a){ w1 =dw/fw; h1 =dh/fh; if(w1>h1){ fh =dh/w1; }else{ fw =dw/h1; } } docRef.resizeImage(fw, fh, PRESOLUTION, ResampleMethod.BICUBIC); docRef.save(); docRef.close(); //關(guān)閉當(dāng)前文檔 } this.parent.parent.close(); } win.center(); win.show(); }
v1.11版本下載
可以把當(dāng)前文檔(包括未保存過的)直接修改尺寸并保存為jpg。(注意會關(guān)閉當(dāng)前文檔。 可以針對整個文件夾進(jìn)行操作。(程序設(shè)定為“不包括隱藏文件”,因此不想處理和不能處理的文件可以先設(shè)置為[隱藏]屬性) 可選擇另存jpg 至另一個文件夾。 另存時支持文件名前后添加字符。
附上代碼,方便大家指正:
#target photoshop app.bringToFront(); // 帶自動判斷橫豎的統(tǒng)一長寬操作。默認(rèn)是直接保存關(guān)閉,原文件要注意備份! v1.11 - 2008.2.23 var PRESOLUTION = 72; app.preferences.rulerUnits = Units.PIXELS; res ="dialog { \ text:'批量尺寸調(diào)整 ',\ group: Group{orientation: 'column',alignChildren:'left',\ top:StaticText{text:'默認(rèn)為直接覆蓋保存 - 請注意選擇或做好備份',align:'left'},\ corrdination: Panel { orientation: 'row', \ text: '需要尺寸', \ x: Group { orientation: 'row', \ s: StaticText { text:'寬:' }, \ e: EditText { preferredSize: [50, 20] } ,\ p: StaticText { text:'px' }, \ }, \ y: Group { orientation: 'row', \ s: StaticText { text:'高:' }, \ e: EditText { preferredSize: [50, 20] }, \ p: StaticText { text:'px' }, \ } ,\ }, \ a:Group{ orientation: 'row', \ c: Checkbox { preferredSize: [16, 16]} ,\ s: StaticText {text:'保持原圖長寬比,新尺寸僅作外框限定'},\ }, \ b:Group{ orientation: 'row', \ c: Checkbox { preferredSize: [16, 16]} ,\ s: StaticText {text:'不判斷原圖橫豎'},\ }, \ now:Group{ orientation: 'row', \ c: Checkbox { preferredSize: [16, 16]} ,\ s: StaticText {text:'對文件夾進(jìn)行操作(否則處理所有目前打開的文檔)'},\ }, \ folderO:Group{ orientation: 'row', \ b: Button {text:'待處理文件夾', properties:{name:'open'} ,helpTip:'選擇您需要處理的文件所在的文件夾'},\ s: EditText { text:'', preferredSize: [180, 20] },\ },\ other:Group{ orientation: 'row', \ c: Checkbox { preferredSize: [16, 16]} ,\ s: StaticText {text:'啟用另存 (jpg格式)'},\ }, \ otherSet: Panel {orientation: 'column',alignChildren:'left',\ text: '另存設(shè)置', \ Quality: Group { orientation: 'row', \ s: StaticText { text:'Jpeg壓縮質(zhì)量:' }, \ e: EditText { preferredSize: [30, 20] ,text:'7'} ,\ s: StaticText { text:'(0-12,數(shù)值越大質(zhì)量越高)' }, \ }, \ head: Group { orientation: 'row', \ s: StaticText { text:'在文件名前添加字符:' }, \ e: EditText { preferredSize: [50, 20] } ,\ }, \ foot: Group { orientation: 'row', \ s: StaticText { text:'在文件名后添加字符:' }, \ e: EditText { preferredSize: [50, 20] }, \ } ,\ otherF:Group{ orientation: 'row', \ c: Checkbox { preferredSize: [16, 16]} ,\ s: StaticText {text:'另存至其他文件夾'},\ }, \ folderS:Group{ orientation: 'row', \ b: Button {text:'另存至', properties:{name:'save'} ,helpTip:'選擇您處理好的文件要保存至的文件夾'},\ s: EditText { text:'', preferredSize: [180, 20] },\ },\ },\ },\ buttons: Group { orientation: 'row', alignment: 'right',\ Btnok: Button { text:'確定', properties:{name:'ok'} }, \ Btncancel: Button { text:'取消', properties:{name:'cancel'} } \ }, \ }"; win = new Window (res); win.buttons.Btncancel.onClick = function () { this.parent.parent.close(); } function lock_b(){ //如果勾選了a,則b被鎖定 if(win.group.a.c.value){ win.group.b.c.value=true; } } win.group.a.c.onClick =function() { lock_b()}; win.group.b.c.onClick =function() { lock_b()}; // 打開文件夾的操作 var folderOpen=win.group.folderO var folderSave=win.group.otherSet.folderS folderOpen.b.onClick = function() { var defaultFolder = folderOpen.s.text; var testFolder = new Folder(defaultFolder); if (!testFolder.exists) { defaultFolder = "~"; } var selFolder = Folder.selectDialog("選擇待處理文件夾", defaultFolder); if ( selFolder != null ) { folderOpen.s.text = selFolder.fsName; folderOpen.s.helpTip = selFolder.fsName.toString(); } } folderSave.b.onClick = function() { var defaultFolder = folderSave.s.text; var testFolder = new Folder(defaultFolder); if (!testFolder.exists) { defaultFolder = "~"; } var selFolder = Folder.selectDialog("選擇要儲存至的文件夾", defaultFolder); if ( selFolder != null ) { folderSave.s.text = selFolder.fsName; folderSave.s.helpTip = selFolder.fsName.toString(); } } // 初始化選項 win.group.folderO.enabled =false; win.group.otherSet.enabled =false; //操作文件夾開關(guān) win.group.now.c.onClick =function(){ if (win.group.folderO.enabled) { win.group.folderO.enabled =false; }else{ win.group.folderO.enabled =true; } } //另存開關(guān) win.group.other.c.onClick =function(){ if (win.group.otherSet.enabled) { win.group.otherSet.enabled =false; }else{ win.group.otherSet.enabled =true; if (!win.group.otherSet.otherF.c.value) {win.group.otherSet.folderS.enabled =false} } } //另存至文件夾開關(guān) win.group.otherSet.otherF.c.onClick =function(){ if (win.group.otherSet.folderS.enabled) { win.group.otherSet.folderS.enabled =false; }else{ win.group.otherSet.folderS.enabled =true; } } // 實際處理開始 function newsize(docRef,a,b,PW,PH){ //改變圖象尺寸函數(shù) var dw = docRef.width; var dh = docRef.height; var fw = PW; var fh = PH; if(!b && dh>dw){ fw = PH; fh = PW; } if(a){ w1 =dw/fw; h1 =dh/fh; if(w1>h1){ fh =dh/w1; }else{ fw =dw/h1; } } docRef.resizeImage(fw, fh, PRESOLUTION, ResampleMethod.BICUBIC); } function save(docRef) { //儲存函數(shù) if (win.group.other.c.value){ //判斷是否另存 docRef.flatten() ;//合并圖層,以儲存jpg docRef.changeMode(ChangeMode.RGB); // 更改為rgb模式,避免其它模式無法儲存 var jpegOptions = new JPEGSaveOptions(); var setquality = Number(win.group.otherSet.Quality.e.text); if (setquality>12) { setquality=12; }else if (setquality<0) { setquality=0; } jpegOptions.quality = setquality; // 獲取另存文件夾及前后追加字段 var headWord = win.group.otherSet.head.e.text; var footWord = win.group.otherSet.foot.e.text; if (win.group.otherSet.otherF.c.value){ //如果選中另存至文件夾 if (win.group.otherSet.folderS.s.text){ var saveFolder = win.group.otherSet.folderS.s.text+"/"; }else{ var saveFolder = docRef.path+"/newsize_"; //容錯,避免沒有具體選擇另存到那個文件夾 } }else{ if (headWord || footWord){ var saveFolder = docRef.path+"/"; }else{ var saveFolder = docRef.path+"/newsize_"; //容錯,避免沒有添加前后綴而保存在本文件夾 } } docRef.saveAs(new File(saveFolder + headWord +docRef.name.substring(0,docRef.name.lastIndexOf('.')) + footWord + ".jpg"),jpegOptions); //設(shè)定輸出文件名稱 }else{ docRef.save(); } docRef.close(SaveOptions.DONOTSAVECHANGES); //關(guān)閉當(dāng)前文檔 } win.buttons.Btnok.onClick = function () { var x=Number(win.group.corrdination.x.e.text); var y=Number(win.group.corrdination.y.e.text); var a=win.group.a.c.value; var b=win.group.b.c.value; //選中則不顛倒橫豎,以輸入值為準(zhǔn) if (!b && x<y){ //確保PW>=PH var PW=y var PH=x }else{ var PW=x var PH=y } if (!win.group.now.c.value) { // 當(dāng)前活動文檔為操作對象 while (app.documents.length){ var docRef = app.activeDocument; newsize(docRef,a,b,PW,PH); save(docRef); } }else{ // 文件夾為操作對象 var openFolder = Folder(win.group.folderO.s.text); var fileList = openFolder.getFiles() //獲取open文件夾下所有文件 for (i=0;i<fileList.length;i++){ if (fileList[i] instanceof File && fileList[i].hidden == false){ //不處理隱藏文件 open(fileList[i]); var docRef = app.activeDocument; newsize(docRef,a,b,PW,PH); save(docRef); } } } this.parent.parent.close(); } win.center(); win.show();
經(jīng)典論壇討論: http://bbs.blueidea.com/thread-2831563-1-1.html
本文鏈接:http://www.95time.cn/tech/graph/2008/5350.asp
出處:藍(lán)色理想
責(zé)任編輯:bluehearts
◎進(jìn)入論壇Photoshop、Fireworks版塊參加討論,我還想發(fā)表評論。
|