|
|
AutoCrop Script by Paul Jaruszewski This script is handy for performing a crop after image rotation. The script looks at the colors in the corners of the rotated image. The colors there are the result of the rotation and are usually black or white depending on the background color chosen when the rotation is performed. In the example below, the background color was white and you can see the rotation artifacts that need to be cropped out. If you are unfamiliar with rotating images in Photoshop, please feel free to check out the tutorial on my web site by clicking here
Two versions This script exists in two versions. Version One that will run in Photoshop 7 or Photoshop CS and Version Two which has a dialog that lets you select the crop technique and only runs in Photoshop CS. The dialog below is from the version for Photoshop CS and allows you to play around with the different techniques for detecting the edge of the image. Unless it fails, use the binary search method.
Discussion of program Photoshop scripting is not an easy thing. Not that the programming is so tough, but the documentation is very difficult to follow and in some cases nonexistent In any case, now that I got that off my shoulders on to the discussion. If you look at a post-rotated image you will see the areas of the image that were added by Photoshop. The trick is to find the point where the artificial background turns into the real image. This would be very easy if you just grab the color of the outside corner and step in one pixel at a time until you got to a pixel that is not the same color as the corner pixel. Believe it or not, there is no known way to simply look at the color of a single pixel in the scripting language. To check the color, you need to look at each of the three RGB channels and ask is the R channel a 1? No How about a 2? How about a 3? You do this until you find the value of the R channel then repeat for the G and B channels. What a shame that the scripting language has so many difficulties. OK so you can guess that the fewer pixels you have to check, the faster the script will run. What I did was use a binary search to find the edge of the image. You know the edge must be somewhere between the outside border and half the dimension of the image. Divide this in two and check again. Keep dividing the span of unknown until you find the edge. I will think of a number between 1 and 100 and tell you if you are high or low. Most people will pick 50 and go from there. You know how to do a binary search even if you didn't know you did. In any case, the binary search is the fastest, but if you have the same color pixel somewhere in the middle of the picture that is on the border, it will fail. Best bet. If you are cropping a washed out picture, make the background black before you rotate. If it is a night scene, use white for the background. If you are trying to crop a tie-dye T-shirt, crop it by hand! Credits This script and most of the other ones here on my web site are the product of a collaboration across the globe. Roger Cavanagh, a talented gentleman from England and I have been slogging through the use of JavaScript in Photoshop. His web site is www.rogercavanagh.com and contains this script as well as his comments on it as well as the other scripts that we worked on together. The autocrop scripts are Copyright © Paul Jaruszewski and Roger Cavanagh, 2004. The autocrop scripts are provided free for personal use. No liability will be accepted for any consequences arising from the use of these scripts. Each script may be copied IN ITS ENTIRETY to others provided no charge is made other than for media. Download Here Click here to download the version for Photoshop 7. Click here to download the version for Photoshop CS.
Paul
|
|
|