Skip to content

Curved corners script for Photoshop

I’ve been looking for an excuse to play around with scripting in Photoshop. Today I found one. I came across a series of actions that rounded off the corners of an image to get it ready for uploading to a web site. The problem is that it was a series of actions. As in one action for each radii. One for 5px, one for 10px, one for 25px, etc. That’s just screaming to be scripted with a quick prompt asking for a radius.

So that’s what I did. About half an hour and I had a script that would prompt for a radius and then round off whatever image was opened.

Sample

Usage
Download and unzip the file linked below. Go to File -> Scripts -> Browse and find where you saved curvedCorners.js and select it. The script will prompt for a radius. Enter one, between 1 and 100, and click ok. That’s it. You’re done.

Download
curvedCorner.zip

The code
If you’re interested, the following is the code. You really don’t need to see it to use the script. But if you’ve never done any scripting for Photoshop, and know a bit of Javascript, it’s fairly easy stuff.

/* Curved corner script for Photoshop
 *
 * Copyright 2010 Ryan Nutt
 * http://photoartsforum.com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http ://www.gnu.org/licenses/>
 */
 
doResize();
 
function doResize() {
	
	/* Save current settings so we can reset them later	*/
	var startRulerUnits = app.preferences.rulerUnits;
	var startTypeUnits = app.preferences.typeUnits;
	var startDisplayDialogs = app.displayDialogs; 
	
	/* Change everything to pixels	*/
	app.preferences.rulerUnits = Units.PIXELS;
	app.preferences.typeUnits = TypeUnits.PIXELS;
	app.displayDialogs = DialogModes.NO;
	
	/* Prompt for the radius of the curve	*/
	var radius = -1;
	do {
		radius = prompt('Enter a radius for the corners.  Must be numeric, greater than 0, and less than or equal to 100.', '10', 'Corner Radius from http://PhotoArtsForum.com');
		if (radius==null) { break; }
	} while (radius < = 0 || radius>100 || isNaN(radius));
	
	if (radius!=null) {	
		/* We don't want to do anything to the document if they clicked cancel,
		 * but we do need to get to the end of this function so that we can reset
		 * the settings back to where they started.
		 */
	
		/* Load a few more variables	*/
		var doc = app.activeDocument;
		var docWidth = doc.width;
		var docHeight = doc.height;
		
		/* This won't work if the layer we're working on is a background
		 * layer.  So we're going to set it so it's not
		 */
		doc.activeLayer.isBackgroundLayer = false;
		
		
		/* We need to resize the canvas so that there's room to do the
		 * smoothing.  PS doesn't seem to like us smoothing when the 
		 * selection is against the edges.
		 */
		doc.resizeCanvas(docWidth + (radius*2), docHeight + (radius * 2)); 
		
		/* Select the original section	*/
		doc.selection.select(Array(
			Array(radius,radius), 
			Array(radius,doc.height - (radius)), 
			Array(doc.width - (radius),doc.height -(radius)), 
			Array(doc.width - (radius),radius))); 
		
		/* Smooth, invert, and delete the parts we don't want	*/
		doc.selection.smooth(radius);	
		doc.selection.invert();	
		doc.selection.clear(); 

		/* We want to leave everything deselected	*/
		doc.selection.deselect(); 
		
		/* Trim off the extra canvas we added earlier	*/
		doc.trim();
		}
		
	/* Put the settings back where they started	*/
	app.preferences.rulerUnits = startRulerUnits;
	app.preferences.typeUnits = startTypeUnits;
	app.displayDialogs = startDisplayDialogs; 
}

Licensing, copying, etc…
I decided to release this under GPL3. It’s such a short script I can’t see trying to charge anything for it. Feel free to do anything with it that’s permissible under the GPL3.

Published inProgramming

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *