Sunday, June 7, 2009

Script




// class definitions in form of function ...
function ParameterClass () {

this.run_batch = false;

this.target_width = 1280;
this.target_height = 720;
this.x_start = 0;
this.y_start = 436;
this.width_start = 4288;
this.x_end = 0;
this.y_end = 436;
this.width_end = 4288;
this.quality = 10;
this.source_dir = "~/PanAndZoomInput";
this.target_dir = "~/PanAndZoomOutput";
this.before_action = "prep";
this.before_palette = "Udo";
this.after_action = "post";
this.after_palette = "Udo";

this.dlg = undefined;
this.target_width_edit = undefined;
this.target_height_edit = undefined;
this.x_start_edit = undefined;
this.y_start_edit = undefined;
this.width_start_edit = undefined;
this.x_end_edit = undefined;
this.y_end_edit = undefined;
this.width_end_edit = undefined;
this.quality_edit = undefined;
this.source_dir_edit = undefined;
this.target_dir_edit = undefined;
this.before_action_edit = undefined;
this.before_palette_edit = undefined;
this.after_action_edit = undefined;
this.after_palette_edit = undefined;

this.getUserInput = function () {
this.target_width = Number( this.target_width_edit.text );
this.target_height = Number( this.target_height_edit.text );
this.x_start = Number( this.x_start_edit.text );
this.y_start = Number( this.y_start_edit.text );
this.width_start = Number( this.width_start_edit.text );
this.x_end = Number( this.x_end_edit.text );
this.y_end = Number( this.y_end_edit.text );
this.width_end = Number( this.width_end_edit.text );
this.quality = Number( this.quality_edit.text );
this.source_dir = this.source_dir_edit.text;
this.target_dir = this.target_dir_edit.text;
this.before_action = this.before_action_edit.text;
this.before_palette = this.before_palette_edit.text;
this.after_action = this.after_action_edit.text;
this.after_palette = this.after_palette_edit.text;
};

};

var params = new ParameterClass();



function verifyInput () {

// read the data ...
params.getUserInput();

// tests ...
if ( ( params.target_width > 2000 && !Window.confirm( 'Target Width > 2000' ) ) ||
( params.target_width < 200 && !Window.confirm( 'Target Width < 200' ) ) ||
( params.target_height > 2000 && !Window.confirm( 'Target Height > 2000' ) ) ||
( params.target_height < 200 && !Window.confirm( 'Target Height < 200' ) ) ||
( params.x_start < 0 && !Window.confirm( 'Start X < 0' ) ) ||
( params.y_start < 0 && !Window.confirm( 'Start Y < 0' ) ) ||
( params.width_start > 5000 && !Window.confirm( 'Start Width > 5000' ) ) ||
( params.width_start < 200 && !Window.confirm( 'Start Width < 200' ) ) ||
( params.x_end < 0 && !Window.confirm( 'End X < 0' ) ) ||
( params.y_end < 0 && !Window.confirm( 'End Y < 0' ) ) ||
( params.width_end > 5000 && !Window.confirm( 'End Width > 5000' ) ) ||
( params.width_end < 200 && !Window.confirm( 'End Width < 200' ) ) ||
false ) {
return false;
}
if ( params.quality < 1 || params.quality > 12 ) {
Window.alert( 'Quality must be a value between 1 and 12' );
return false;
}
if ( !Folder( params.source_dir ).exists ) {
Window.alert( 'Source Directory does not exist' );
return false;
}
if ( !Folder( params.target_dir ).exists ) {
Window.alert( 'Target Directory does not exist' );
return false;
}

// if all tests go through ...
return true;

};



function panAndZoomCB() {

params.run_batch = false;
if ( verifyInput() ) {

// first close the main window ...
params.dlg.close();

// then open gate to run batch ...
params.run_batch = true;
}

};


function cancelCB() {

params.dlg.close();

};


function createParamsPanel () {

with ( params.dlg ) {
params_pnl = add( 'panel', [10,10,390,350], 'Parameters' );
with ( params_pnl ) {
add( 'statictext', [10, 22, 110, 37], 'Target Width:' );
add( 'statictext', [10, 42, 110, 57], 'Target Height:' );
add( 'statictext', [10, 62, 110, 77], 'Start X:' );
add( 'statictext', [10, 82, 110, 97], 'Start Y:' );
add( 'statictext', [10, 102, 110, 117], 'Start Width:' );
add( 'statictext', [10, 122, 110, 137], 'End X:' );
add( 'statictext', [10, 142, 110, 157], 'End Y:' );
add( 'statictext', [10, 162, 110, 177], 'End Width:' );
add( 'statictext', [10, 182, 110, 197], 'Quality:' );
add( 'statictext', [10, 202, 110, 217], 'Source Directory:' );
add( 'statictext', [10, 222, 110, 237], 'Target Directory:' );
add( 'statictext', [10, 242, 110, 257], 'Before Action:' );
add( 'statictext', [10, 262, 110, 277], 'Before Palette:' );
add( 'statictext', [10, 282, 110, 297], 'After Action:' );
add( 'statictext', [10, 302, 110, 317], 'After Palette:' );
params.target_width_edit = add( 'edittext', [120, 20, 200, 38], params.target_width );
params.target_height_edit = add( 'edittext', [120, 40, 200, 58], params.target_height );
params.x_start_edit = add( 'edittext', [120, 60, 200, 78], params.x_start );
params.y_start_edit = add( 'edittext', [120, 80, 200, 98], params.y_start );
params.width_start_edit = add( 'edittext', [120, 100, 200, 118], params.width_start );
params.x_end_edit = add( 'edittext', [120, 120, 200, 138], params.x_end );
params.y_end_edit = add( 'edittext', [120, 140, 200, 158], params.y_end );
params.width_end_edit = add( 'edittext', [120, 160, 200, 178], params.width_end );
params.quality_edit = add( 'edittext', [120, 180, 200, 198], params.quality );
params.source_dir_edit = add( 'edittext', [120, 200, 370, 218], params.source_dir );
params.target_dir_edit = add( 'edittext', [120, 220, 370, 238], params.target_dir );
params.before_action_edit = add( 'edittext', [120, 240, 370, 258], params.before_action );
params.before_palette_edit = add( 'edittext', [120, 260, 370, 278], params.before_palette );
params.after_action_edit = add( 'edittext', [120, 280, 370, 298], params.after_action );
params.after_palette_edit = add( 'edittext', [120, 300, 370, 318], params.after_palette );
}
}

};


function createActionPanel () {

with ( params.dlg ) {
pan_and_zoom_btn = add( 'button', [10,360,195,390], 'Just do it ...');
cancel_btn = add( 'button', [200,360,390,390], '... or not' );
pan_and_zoom_btn.onClick = panAndZoomCB;
cancel_btn.onClick = cancelCB;
}

};

function addImgName ( img_ref ) {
var fc = new SolidColor();
fc.rgb.red = 255;
fc.rgb.green = 255;
fc.rgb.blue = 255;


newTextLayer = img_ref.artLayers.add()

newTextLayer.kind = LayerKind.TEXT
newTextLayer.textItem.contents = img_ref.name
newTextLayer.textItem.position = Array( 1100, 700 )
newTextLayer.textItem.size = 5
newTextLayer.textItem.color = fc;
newTextLayer.merge()
}

function batchProcessInPhotoshop () {

if ( ! params.run_batch ) {
return;
}

jpeg_save_options = new JPEGSaveOptions();
jpeg_save_options.embedColorProfile = true;
jpeg_save_options.formatOptions = FormatOptions.STANDARDBASELINE;
jpeg_save_options.matte = MatteType.NONE;
jpeg_save_options.quality = params.quality;

var img_list = Folder( params.source_dir ).getFiles();
var img_number = img_list.length;
var aspect_ratio = params.target_width / params.target_height;
var height_start = params.width_start / params.aspect_ratio;
var x = params.x_start;
var y = params.y_start;
var width = params.width_start;
var height = width / aspect_ratio;
var x_incr = (params.x_end - params.x_start) / img_number;
var y_incr = (params.y_end - params.y_start) / img_number;
var width_incr = (params.width_end - params.width_start) / img_number;

for (var img_ix = 0; img_ix < img_number; img_ix++ ) {
if (img_list[img_ix] instanceof File && img_list[img_ix].hidden == false) {
var img_ref = app.open( img_list[img_ix] );

try { app.doAction( params.before_action, params.before_palette ); }
catch(e){alert(e);}

img_ref.crop( new Array( x, y, x+width, y+height ) );
img_ref.resizeImage( params.target_width, params.target_height );

addImgName( img_ref );

x += x_incr;
y += y_incr;
width += width_incr;
height = width / aspect_ratio;

try { app.doAction( params.after_action, params.after_palette ); }
catch(e){alert(e);}

var output_file = new File( params.target_dir + "/" + img_ref.name );
img_ref.saveAs( output_file, jpeg_save_options, true, Extension.LOWERCASE );
img_ref.close( SaveOptions.DONOTSAVECHANGES )
}
}

};


function main() {

params.dlg = new Window( 'dialog', 'Pan And Zoom', [0, 0, 400, 400] );

createParamsPanel();
createActionPanel();

with ( params.dlg ) {
center();
show();
}

batchProcessInPhotoshop();

};


main();





No comments:

Post a Comment