Copy link to clipboard
Copied
Hi,
I am scanning batches of Polaroid and Instax pictures and to have them cropped from the scanned file and saved separately, I am using the following script which I found on a GitHub site (An photoshop batch script for crop & straighten photo from Jeffrey Tranberry ยท GitHub๐
// cropAndStraightenBatch.jsx
// Copyright 2006-2008
// Written by Jeffrey Tranberry
// Photoshop for Geeks Version 2.0
/*
Description:
This script demonstates how to batch process
a folder of images using the crop and straighten command
*/
// enable double clicking from the
// Macintosh Finder or the Windows Explorer
#target photoshop
// Make Photoshop the frontmost application
// in case we double clicked the file
app.bringToFront();
/////////////////////////
// SETUP
/////////////////////////
// A list of file extensions to skip, keep them lower case
gFilesToSkip = Array( "db", "xmp", "thm", "txt", "doc", "md0", "tb0", "adobebridgedb", "adobebridgedbt", "bc", "bct" );
/////////////////////////
// MAIN
/////////////////////////
//Make sure there are no open documents
if (app.documents.length > 0){
alert ("This script requires that there are no open documents to run.");
}else{
// Pops open a dialog for the user to choose the folder of documents to process
var inputFolder = Folder.selectDialog("Select a folder of documents to process");
// Pops open a dialog for the user to set the output folder
var outputFolder = Folder.selectDialog("Select a folder for the output files");
// Open and process a folder of Images
OpenFolder();
}
/////////////////////////
// FUNCTIONS
/////////////////////////
// Given the a Folder of files, open the files and process them
function OpenFolder() {
var filesOpened = 0;
var fileList = inputFolder.getFiles();
for ( var i = 0; i < fileList.length; i++ ) {
// Make sure all the files in the folder are compatible with PS
if ( fileList instanceof File && ! fileList.hidden && ! IsFileOneOfThese( fileList, gFilesToSkip )) {
open( fileList );
filesOpened++;
/////////////////////////
// Put all your processing functions...
/////////////////////////
// Create a variable to store a reference to
// the currently active document, which in this
// case is the parent document we want to extract
// multiple scanned images from
var docRef = app.activeDocument;
// Run the cropAndStraighten function
// which will rusult in more than one open document
cropAndStraighten();
// Close the parent document we originally opened
docRef.close(SaveOptions.DONOTSAVECHANGES);
// Process all open documents until no documents
// are left open.
while (app.documents.length >=1){
/////////////////////////
// Put all your processing functions...
/////////////////////////
// Flatten the document in case the file type we want to save to requires a flat doc
app.activeDocument.flatten();
//Save as a JPEG to the outputFolder
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 10;
jpegOptions.embedColorProfile = false;
app.activeDocument.saveAs( File( outputFolder + "/" + activeDocument.name + ".jpg"), jpegOptions, false);
// Close without saving
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
/////////////////////////
// ...in the area between these two comments.
/////////////////////////
}
/////////////////////////
// ...in the area between these two comments.
/////////////////////////
}
}
return filesOpened;
}
// given a file name and a list of extensions
// determine if this file is in the list of extensions
function IsFileOneOfThese( inFileName, inArrayOfFileExtensions ) {
var lastDot = inFileName.toString().lastIndexOf( "." );
if ( lastDot == -1 ) {
return false;
}
var strLength = inFileName.toString().length;
var extension = inFileName.toString().substr( lastDot + 1, strLength - lastDot );
extension = extension.toLowerCase();
for (var i = 0; i < inArrayOfFileExtensions.length; i++ ) {
if ( extension == inArrayOfFileExtensions ) {
return true;
}
}
return false;
}
// Crop and Straighten function created
// using the ScriptingListener plug-in
function cropAndStraighten(){
var id333 = stringIDToTypeID( "CropPhotosAuto0001" );
executeAction( id333, undefined, DialogModes.NO );
}
In the script, the cropAndStraighten(); function sometimes cuts off a bit much of the white border of the Polaroids/Instax pictures. That's why I would have to automatically set a border which should be respected when cropping the images.
Does anybody know how you can do that?
I have no idea of Photoshop scripting / functions... ๐
Thanks a lot for any help
(oh no, I don't know why but the beautifully inserted html is messed up when posting my question, sorry)
Check out this script.
My new code is marked as
///NEW CODE ///////////////////////////////////////////////
...
//////////////////////////////////////////////////////////
in two places
The scipt:
...// cropAndStraightenBatch.jsx
// Copyright 2006-2008
// Written by Jeffrey Tranberry
// Photoshop for Geeks Version 2.0
/*
Description:
This script demonstates how to batch process
a folder of images using the crop and straighten command
*/
// enable double clicking from the
// Macintosh Finder or the Windows Explorer
Copy link to clipboard
Copied
Algorithm. )
In the current document, duplicate the layer. The bottom layer convert to smart object.
Activate the top layer. Execute "CropPhotosAuto0001" action.
In the new document, delete the top layer. Expand the canvas to the desired size.
Copy link to clipboard
Copied
Thank you for your answer and the information.
I'm sorry but how do I do all this inside the script?
Since I need to batch-resize a lot of images (about 100), I cannot duplicate layers etc. for each image....
Copy link to clipboard
Copied
Check out this script.
My new code is marked as
///NEW CODE ///////////////////////////////////////////////
...
//////////////////////////////////////////////////////////
in two places
The scipt:
// cropAndStraightenBatch.jsx
// Copyright 2006-2008
// Written by Jeffrey Tranberry
// Photoshop for Geeks Version 2.0
/*
Description:
This script demonstates how to batch process
a folder of images using the crop and straighten command
*/
// enable double clicking from the
// Macintosh Finder or the Windows Explorer
#target photoshop
// Make Photoshop the frontmost application
// in case we double clicked the file
app.bringToFront();
/////////////////////////
// SETUP
/////////////////////////
// A list of file extensions to skip, keep them lower case
gFilesToSkip = Array( "db", "xmp", "thm", "txt", "doc", "md0", "tb0", "adobebridgedb", "adobebridgedbt", "bc", "bct" );
/////////////////////////
// MAIN
/////////////////////////
//Make sure there are no open documents
if (app.documents.length > 0){
alert ("This script requires that there are no open documents to run.");
}else{
// Pops open a dialog for the user to choose the folder of documents to process
var inputFolder = Folder.selectDialog("Select a folder of documents to process");
// Pops open a dialog for the user to set the output folder
var outputFolder = Folder.selectDialog("Select a folder for the output files");
// Open and process a folder of Images
OpenFolder();
}
/////////////////////////
// FUNCTIONS
/////////////////////////
// Given the a Folder of files, open the files and process them
function OpenFolder() {
var filesOpened = 0;
var fileList = inputFolder.getFiles();
for ( var i = 0; i < fileList.length; i++ ) {
// Make sure all the files in the folder are compatible with PS
if ( fileList instanceof File && ! fileList.hidden && ! IsFileOneOfThese( fileList, gFilesToSkip )) {
open( fileList );
filesOpened++;
/////////////////////////
// Put all your processing functions...
/////////////////////////
// Create a variable to store a reference to
// the currently active document, which in this
// case is the parent document we want to extract
// multiple scanned images from
var docRef = app.activeDocument;
// Run the cropAndStraighten function
// which will rusult in more than one open document
///NEW CODE ///////////////////////////////////////////////
app.activeDocument.flatten();
app.activeDocument.activeLayer.duplicate();
executeAction( stringIDToTypeID( "newPlacedLayer" ), undefined, DialogModes.NO );
app.activeDocument.activeLayer = app.activeDocument.layers[0];
app.preferences.rulerUnits = Units.PIXELS;
//////////////////////////////////////////////////////////
cropAndStraighten();
// Close the parent document we originally opened
docRef.close(SaveOptions.DONOTSAVECHANGES);
// Process all open documents until no documents
// are left open.
while (app.documents.length >=1){
///NEW CODE ///////////////////////////////////////////////
var border = Math.max(Number(app.activeDocument.width.value), Number(app.activeDocument.height.value))*0.1;
app.activeDocument.layers[0].remove();
app.activeDocument.resizeCanvas(app.activeDocument.width+border, app.activeDocument.height+border, AnchorPosition.MIDDLECENTER);
//////////////////////////////////////////////////////////
/////////////////////////
// Put all your processing functions...
/////////////////////////
// Flatten the document in case the file type we want to save to requires a flat doc
app.activeDocument.flatten();
//Save as a JPEG to the outputFolder
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 10;
jpegOptions.embedColorProfile = false;
app.activeDocument.saveAs( File( outputFolder + "/" + activeDocument.name + ".jpg"), jpegOptions, false);
// Close without saving
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
/////////////////////////
// ...in the area between these two comments.
/////////////////////////
}
/////////////////////////
// ...in the area between these two comments.
/////////////////////////
}
}
return filesOpened;
}
// given a file name and a list of extensions
// determine if this file is in the list of extensions
function IsFileOneOfThese( inFileName, inArrayOfFileExtensions ) {
var lastDot = inFileName.toString().lastIndexOf( "." );
if ( lastDot == -1 ) {
return false;
}
var strLength = inFileName.toString().length;
var extension = inFileName.toString().substr( lastDot + 1, strLength - lastDot );
extension = extension.toLowerCase();
for (var i = 0; i < inArrayOfFileExtensions.length; i++ ) {
if ( extension == inArrayOfFileExtensions ) {
return true;
}
}
return false;
}
// Crop and Straighten function created
// using the ScriptingListener plug-in
function cropAndStraighten(){
var id333 = stringIDToTypeID( "CropPhotosAuto0001" );
executeAction( id333, undefined, DialogModes.NO );
}
Copy link to clipboard
Copied
First of all a big SORRY for not getting back to you earlier... I was terribly busy with work
I just had the possibility to check your script and it works like a charm! Thank you so very much! You really saved me hours of time checking and cropping scans
Copy link to clipboard
Copied
I've been looking for a crop and straighten with borders for quite sometime. Thanks for posting the script.