Photoshop Script: Alle Bilder in einem Ordner verkleinern, Rand entfernen und speichern

Skaliert alle Bilder aus einem Ordner auf die gegebene maximal Breite/Höhe und speichert diese wieder ab. Zusätzlich kann ein Rand entfernt und der Dateiname angepasst werden.

//###################################################################################
//
// Crop image by dialog
// Resize images to max 500px depends of witgh/height and save as JPG
// Photoshop Script (CS2, tested on PC) 
//
//###################################################################################

// main settings
var picSourcePath = "C:\\Test\\Original\\";
var picTargetPath = "C:\\Test\\JPG\\";
var maxSize = 500; // max width and height

//###################################################################################

// save settings and customize environment
var startRulerUnits = app.preferences.rulerUnits;
var startTypeUnits = app.preferences.typeUnits;
var startDisplayDialogs = app.displayDialogs;
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
app.displayDialogs = DialogModes.NO;
//var doc = app.activeDocument;

var picFolder = Folder(picSourcePath);
var fileList = picFolder.getFiles();

if (picSourcePath != null && picTargetPath != null) {

	for (var i=0; i<fileList.length; i++) {

		if (fileList[i] instanceof File) {
			open(fileList[i]);

			var newFileName = fileList[i].name;
			app.activeDocument.changeMode(ChangeMode.RGB);

			if (app.activeDocument.width > maxSize || app.activeDocument.height > maxSize) { 

				// remove surrounding
				var cropSize = prompt('Crop from Border (px).', '');

				if (cropSize != '' && cropSize > 0 && 
					cropSize < app.activeDocument.width && 
					cropSize < app.activeDocument.height) { 
						cropWidthFromBorder(cropSize);
				}
				
				// resize
				resize(maxSize);		

				//if (confirm('Sharpen?')) {						
				//	app.activeDocument.activeLayer.applySharpen();
				//}
				
				//if (confirm('Save and Close?')) {						
					saveWebJpg(newFileName.substring(0, newFileName.length-4) + ".jpg", picTargetPath, 75);
					//app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
				//}
			}
			else {
				// if pictures already 500px or smaller, then only rename
				fileList[i].rename(picTargetPath + newFileName.substring(0, newFileName.length-4) + ".jpg");
			}
			
			// ... and close at all events
			app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
		}
	}
}


// restore settings
app.preferences.rulerUnits = startRulerUnits;
app.preferences.typeUnits = startTypeUnits;
app.displayDialogs = startDisplayDialogs;

alert("Done!");

// Funktion um Dokument als Jpeg zu speichern (Fuer Web speichern...)
// Parameter: Dateiname, Pfad mit abschliessendem '\'!, Qualitaet 1-100
// Beispielaufruf: saveWebJpg("test.jpg", "c:\\bilder\\", 75);
function saveWebJpg(jpgName, filePath, jpgQuality ) {
 var saveFile = new File(filePath + jpgName);
 var webJpgOptions = new ExportOptionsSaveForWeb();
 webJpgOptions.format = SaveDocumentType.JPEG;
 webJpgOptions.optimized = true;
 webJpgOptions.quality = jpgQuality;
 activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, webJpgOptions);
 File = null; ExportOptionsSaveForWeb = null;
}

// http://www.chadwickwood.com/2008/11/conditional-image-resizing-with-photoshop-and-javascript
function resize(size)
{
  if(app.activeDocument.width > app.activeDocument.height)
    app.activeDocument.resizeImage(size, (size * app.activeDocument.height/app.activeDocument.width),null, ResampleMethod.BICUBIC);
  else
    app.activeDocument.resizeImage((size * app.activeDocument.width/app.activeDocument.height), size, null, ResampleMethod.BICUBIC);
} 

function cropWidthFromBorder(cropWidth) { 
	app.activeDocument.crop(Array(
		cropWidth,
		cropWidth,
		app.activeDocument.width - cropWidth,
		app.activeDocument.height - cropWidth));
}

August 2007 | Permalink | Feedback