View Full Version : Script to center-crop images to a fixed ratio


gct13
01-27-2008, 09:00 PM
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.

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

DannyRaphael
01-28-2008, 09:52 AM
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

gct13
02-19-2008, 07:03 PM
Fyi I got it work using this script:

// 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;

}

DannyRaphael
02-20-2008, 12:51 AM
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.

gct13
02-20-2008, 10:20 AM
The script works with CS3 (haven't tested any other versions).

Thanks

momono
10-21-2008, 03:16 PM
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..

Damo77
10-21-2008, 05:27 PM
Hi Momono, don't make this more difficult than it needs to be - you can easily do this with a Photoshop action.

momono
10-23-2008, 08:08 PM
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.

Damo77
10-23-2008, 08:45 PM
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

DannyRaphael
10-25-2008, 08:59 AM
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 ProcessorThis 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.

momono
10-25-2008, 01:46 PM
Hey guys, thanks for the replies. The action recording did it - but in order to do the 'fit image' command I had to figure out the 4/3 constituent of the 1680 width (which is 1260), then do the crop command (with both h and w data) - and instead of applying the sharpen filter like suggested, I did a one-pixel motion blur instead... for the 1600x1200 images I have this turns the trick, and now so far I have a beautiful little collection of 1680x1050 frogs and nebulae..

Lemme know how many of you widescreen-toting photoshop gurus want somma this candy!

Thanks again,
- Brendan

Juergen D
10-25-2008, 03:01 PM
FWIW - I thought to try it in IrfanView. Using 2592 x 1944 images I batch cropped with the following settings:

X-pos 456 <<- (2592-1680)/2=456
Y-pos 447 <<- (1944-1050)/2=447
Width 1680, Height 1050, Start Corner: Left Top

Juergen

0lBaldy
10-25-2008, 05:07 PM
Would FastStone Photo Resizer 2.7 Freeware (Last Update: 2008-10-07) (http://www.faststone.org/) work for your project?

Juergen D
10-25-2008, 05:19 PM
It looks like FastStone lets you input the same settings as I used in IrfanView. It should work the same.

Juergen

momono
10-26-2008, 02:41 PM
Thanks for all the suggestions guys, they probably all work but I got this one pretty much licked in photoshop and learned a few things to boot with your help.. The best solution seems to be loading that code supplied by gct13 into a script file and saving it, opening one of the pictures and starting a new action record, run that script (tweaked to the new ratio 8:5), then resize it to 1680x1050 in my case and then maybe add a 1 pixel blur. Then I stop recording, go to the scripts - image processor and select the source folder, pick the action I made from the list, make it so it saves the new ones in the same folder (makes a new folder called JPEG, whatever) and just do that for folder after folder.. The fit image method ended up with some of my wallpapers having about 75 pixels of white gapping on either side..

Soo I'm of the opinion that God made LCD monitors for insanely high-res vistas and microcosms of the awesomeness of the Universe and this planet.. So here is the absolute best wallpaper site I have ever found, bar none.. probably familiar to a few of you - www.naturedesktop.org (http://www.naturedesktop.org) .. Just scroll down the page a little to find your resolution.. Each res has a different set of unique stuff, some carryovers, but all incredible.. but this little trick can now open any resolution to another pretty much. I'm now on file 299 / 1015 in the 1920 res's.. I harvest huge folders and load them into Webshots 1.0 (2 and beyond have spyware) with the camera turned off.. I'm a freelance computer repair guy and when I do a new format of Windows I include these webshots packs according to the screen size of my customers. They seem to enjoy it!

Well anyway, thanks again, lemme know if anybody wants more links to this stuff. Peace. :)

RetouchJones
11-12-2008, 10:05 AM
This may be a super dumb question, but the script presented by GCT13, how do I take that and use that in Photoshop? I have only worked with actions in photoshop, never scripts? What are the steps to taking what GCT13 posted and turn it into something I can use in PS-CS3?

momono
11-12-2008, 01:38 PM
Hello Mr. Jones! It's actually a good question, because it's the same one I was asking of these guys and didn't really get an answer, and in the end I had to figure it out myself.. You would open a fresh new txt file, paste in the provided code, change the variables for the crop to your suiting and save it as a 'whatever.jsx' file (change file type to 'all files') in your work folder or wherever you can remember it.. then start a new action record in photoshop, doing everything I described in my last post. Also I discovered that for images less than the desired resolution (such as 1600x1200 for example, still good enough imho) you should resize the image to 1680 width (if that happens to be your desired resolution) relative to whatever aspect ratio your source is *before* you run the script to crop - if you don't do this it seems that images that start at a lower resolution or an odd res like 1280x960 end up with white bars on either side after you run an image batch using the action you've recorded. Hope that helps..

RetouchJones
11-12-2008, 01:47 PM
GCT13, how would you change that script to make the document 2000x2000 as well as being able to center the image and have it line up at a specific X coordinate?