RetouchPRO

Welcome to RetouchPRO, the web community for retouchers.

You are currently viewing as an unregistered guest which gives you limited access. Registration is fast, simple and absolutely free so please, join RetouchPRO today!

If you have any problems with the registration process or your account login, please contact us. If you've forgotten your password, click here.

Go Back   RetouchPRO > Tools > Software > Photoshop Scripting
Register Blogs FAQ Search Today's Posts Mark Forums Read

Photoshop Scripting Learning and sharing for all platforms

Reply
 
LinkBack Thread Tools
  #1  
Old 01-27-2008, 08:00 PM
Junior Member
 
Join Date: Jan 2008
Posts: 3
Script to center-crop images to a fixed ratio

Hello, could someone set me on the right path for Windows Photoshop scripting (I assume this would require a script)...

My objective is to iterate thru a folder of images, center crop the images based on height/width ratio variables, and save the images within the same folder. I've detailed the 2 IF statements conditionals below-

On all the calculations, round up to the nearest pixel.

Code:
Variables:
 WidthRatio  = 4
 HeightRatio = 3

-- Image's aspect ratio is greater than the desired ratio
-- so crop out left and right areas of the Image
If (Image's width / Image's height) > (WidthRatio/ HeightRatio)
 Set selection
  To: rectangle
  Top: 0 px
  Left: (Image's width - (Image's height * (WidthRatio/HeightRatio))) / 2  px
  Bottom: (Image's height) px
  Right: (Image's width + (Image's height * (WidthRatio/HeightRatio))) / 2  px
 Crop
 Save image
End If

-- Image's aspect ratio is less than the desired ratio
-- so crop out top and bottom areas of the Image
If (Image's width / Image's height) < (WidthRatio/ HeightRatio)
 Set selection
  To: rectangle
  Top: (Image's height - (Image's width * (HeightRatio/WidthRatio))) / 2  px
  Left: 0 px
  Bottom: (Image's height + (Image's width * (HeightRatio/WidthRatio))) / 2  px
  Right: (Image's width) px
 Crop
 Save image
End If
Thanks,
Dan
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Float This Post!Stumble this Post!Google Bookmark this Post!Yahoo Bookmark this Post!Live Bookmark this Post!Share this post on Facebook
Reply With Quote
  #2  
Old 01-28-2008, 08:52 AM
DannyRaphael's Avatar
Moderator
Patron
 
Join Date: Mar 2002
Location: Near Seattle, Washington, USA
Posts: 5,600
Re: Script to center-crop images to a fixed ratio

Hi Dan:

Yep...this would need a script and FWIW it's way over my head scripting wise (I'm a rookie at this).

In addition to RetouchPRO there are two other places where I post questions of this nature. If you haven't yet, I highly recommend you check them out:

* http://www.ps-scripts.com
* http://www.adobe.com - Support, Photoshop scripting forum
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Float This Post!Stumble this Post!Google Bookmark this Post!Yahoo Bookmark this Post!Live Bookmark this Post!Share this post on Facebook
Reply With Quote
  #3  
Old 02-19-2008, 06:03 PM
Junior Member
 
Join Date: Jan 2008
Posts: 3
Re: Script to center-crop images to a fixed ratio

Fyi I got it work using this script:

Code:
// Get the active document and make a new selection.

// enable double clicking from the Macintosh Finder or the Windows Explorer
#target photoshop

// in case we double clicked the file
app.bringToFront();

// debug level: 0-2 (0:disable, 1:break on error, 2:break at beginning)
// $.level = 0;
// debugger; // launch debugger on next line


if (app.documents.length > 0) {    

 var docRef = app.activeDocument;
 var WidthRatio = 4;
 var HeightRatio = 3;
 var boundTop;
 var boundLeft;
 var boundRight;
 var boundBottom;

// Image's aspect ratio is greater than the desired ratio
// so crop out left and right areas of the Image
if ((docRef.width / docRef.height) > (WidthRatio/ HeightRatio))
{
 boundTop = 0;
 boundLeft = ((docRef.width - (docRef.height * (WidthRatio/HeightRatio))) / 2);
 boundBottom = docRef.height;
 boundRight = ((docRef.width + (docRef.height * (WidthRatio/HeightRatio))) / 2);
 bounds = new Array(boundLeft,boundTop,boundRight,boundBottom);
 docRef.crop(bounds);
}

// Image's aspect ratio is less than the desired ratio
// so crop out top and bottom areas of the Image
if ((docRef.width / docRef.height) < (WidthRatio/ HeightRatio))
{
 boundTop = ((docRef.height - (docRef.width * (HeightRatio/WidthRatio))) / 2);
 boundLeft = 0;
 boundBottom = ((docRef.height + (docRef.width * (HeightRatio/WidthRatio))) / 2);
 boundRight = docRef.width;
 bounds = new Array(boundLeft,boundTop,boundRight,boundBottom);
 docRef.crop(bounds);
}

// bounds = new Array(10, 10, app.activeDocument.width - 10, app.activeDocument.height - 10);
// docRef.crop(bounds);

 boundTop = null;
 boundLeft = null;
 boundRight = null;
 boundBottom = null;
 WidthRatio = null;
 HeightRatio = null;
 docRef = null;

}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Float This Post!Stumble this Post!Google Bookmark this Post!Yahoo Bookmark this Post!Live Bookmark this Post!Share this post on Facebook
Reply With Quote
  #4  
Old 02-19-2008, 11:51 PM
DannyRaphael's Avatar
Moderator
Patron
 
Join Date: Mar 2002
Location: Near Seattle, Washington, USA
Posts: 5,600
Re: Script to center-crop images to a fixed ratio

Well done. Thanks for sharing your solution.

Under what version of Photoshop did you write this script?

Reason I ask: There are subtle syntax differences between versions.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Float This Post!Stumble this Post!Google Bookmark this Post!Yahoo Bookmark this Post!Live Bookmark this Post!Share this post on Facebook
Reply With Quote
  #5  
Old 02-20-2008, 09:20 AM
Junior Member
 
Join Date: Jan 2008
Posts: 3
Re: Script to center-crop images to a fixed ratio

The script works with CS3 (haven't tested any other versions).

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Float This Post!Stumble this Post!Google Bookmark this Post!Yahoo Bookmark this Post!Live Bookmark this Post!Share this post on Facebook
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Not seeing SOME images mikajomc Forums Q&A 4 01-23-2008 03:37 PM
Script to Detect Upright and View images Cameraken Photoshop Scripting 4 01-08-2008 07:50 AM
Other festive/xmas Images palms1 Photo-Based Art 13 12-19-2007 07:14 AM
Protecting images on your website ChinaMark Your Website 4 11-16-2007 06:11 PM
HDR - ( High Dynamic Range ) Images CJ Swartz HDR/HDRi and Tone Mapping 6 11-12-2007 03:40 PM


All times are GMT -6. The time now is 02:10 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 Doug Nelson. All Rights Reserved moo




1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50