RetouchPRO

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


Photoshop Scripting Learning and sharing for all platforms

Script to center-crop images to a fixed ratio

Reply
 
Thread Tools
  #1  
Old 01-27-2008, 09:00 PM
gct13 gct13 is offline
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
Reply With Quote top
  #2  
Old 01-28-2008, 09:52 AM
DannyRaphael's Avatar
DannyRaphael DannyRaphael is offline
Moderator
 
Join Date: Mar 2002
Location: Near Seattle, Washington, USA
Posts: 6,300
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
Reply With Quote top
  #3  
Old 02-19-2008, 07:03 PM
gct13 gct13 is offline
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;

}
Reply With Quote top
  #4  
Old 02-20-2008, 12:51 AM
DannyRaphael's Avatar
DannyRaphael DannyRaphael is offline
Moderator
 
Join Date: Mar 2002
Location: Near Seattle, Washington, USA
Posts: 6,300
Re: Script to center-crop images to a fixed ratio

Well done. Thanks for sharing your solution.

Under what version of PS did you write this script?

Reason I ask: There are subtle syntax differences between versions.
Reply With Quote top
  #5  
Old 02-20-2008, 10:20 AM
gct13 gct13 is offline
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
Reply With Quote top
  #6  
Old 10-21-2008, 03:16 PM
momono momono is offline
Junior Member
 
Join Date: Oct 2008
Posts: 5
Re: Script to center-crop images to a fixed ratio

I am a total newbie to photoshop scripts altogether, and I came across this thread in a google search following a similar issue. I have a very nice collection of high-resolution desktop wallpapers for full-screen monitors and I'm upgrading to a new 1680x1050 LCD.. I would like to rig up a script along these lines to crop to a ratio of 16 by 10 and save them in a folder called "Widescreen", basically the opposite of what Dan here is trying to do - I looked over on the Adobe page as well and actually found his similar post there. So Dan yourself, maybe you can shoot me a PM on how to put this into a working file and then how to use it with ps3? Thanks in advance..
Reply With Quote top
  #7  
Old 10-21-2008, 05:27 PM
Damo77's Avatar
Damo77 Damo77 is offline
Senior Member
 
Join Date: Jul 2007
Location: Brisbane, Australia
Posts: 211
Re: Script to center-crop images to a fixed ratio

Hi Momono, don't make this more difficult than it needs to be - you can easily do this with a Photoshop action.
Reply With Quote top
  #8  
Old 10-23-2008, 08:08 PM
momono momono is offline
Junior Member
 
Join Date: Oct 2008
Posts: 5
Re: Script to center-crop images to a fixed ratio

And how would I begin to do that? I've tinkered around with it and there doesn't seem to be an aspect ratio crop option in the normal menu stuff.. I put the working code provided here into a script that did what it said it would, it just didn't save it or run the whole batch, just cropped to the aspect I put in for the one file.
Reply With Quote top
  #9  
Old 10-23-2008, 08:45 PM
Damo77's Avatar
Damo77 Damo77 is offline
Senior Member
 
Join Date: Jul 2007
Location: Brisbane, Australia
Posts: 211
Re: Script to center-crop images to a fixed ratio

Just record:

1. File > Automate > Fit Image ... set to 1680px wide

2. Image > Canvas Size ... set to 1050px high

3. Sharpen

4. Save and close

Then run that action in a batch, or via File > Scripts > Image Processor
Reply With Quote top
  #10  
Old 10-25-2008, 08:59 AM
DannyRaphael's Avatar
DannyRaphael DannyRaphael is offline
Moderator
 
Join Date: Mar 2002
Location: Near Seattle, Washington, USA
Posts: 6,300
Re: Script to center-crop images to a fixed ratio

Quote:
Originally Posted by Damo77 View Post
Just record:

1. File > Automate > Fit Image ... set to 1680px wide

2. Image > Canvas Size ... set to 1050px high

3. Sharpen

4. Save and close

Then run that action in a batch, or via File > Scripts > Image Processor
This will indeed result in 1680x1050 images. Well done.

Note **:
> 1280x1024 will result in 1680x1345
> 1600x1200 will result in 1680x1261
... after applying Fit Image command, so the following Canvas Size command will "cut off" little at the top and bottom of each file.

** Image source sizes provided to me in a PM from momono.

When the Canvas Size command is applied, PS will display an "Are you sure?" warning that must be acknowledged = this will not a "hands off"/automatic solution even if recorded in an action applied though euther Automate > Batch or Automate > Scripts > Image Processor.

No free lunch in this one...

PM me if you need help on recording the action or applying it.
Reply With Quote top
Reply

  RetouchPRO > Tools > Software > Photoshop Scripting


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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


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


All times are GMT -6. The time now is 05:12 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
Copyright © 2016 Doug Nelson. All Rights Reserved