Skip to main content
Participant
April 13, 2022
Answered

Resize canvas width to 60% of the height.

  • April 13, 2022
  • 3 replies
  • 2245 views

Hi,

 

I was wondering if it's possible to resize the width of an image canvas to a specific % of the height? Let's say my image is 800px by 1000px, but i want the width to be 60% of the height which equals 600px, is there a tool that allows me to do that?

 

The problem with the "Image -> Canvas Size -> Percent" is that if i put 60% in the width field, it will reduce the width to 60% of 800px, not 60% of the 1000px height.

 

I am also looking for a way to do the same thing with the Content-Aware Scale function.

 

I would very much appreciate it if any good samaritan could enlighten me.

This topic has been closed for replies.
Correct answer Stephen Marsh

@Boute240157133512 – Thank you for marking my script post as a correct answer. I marked the reply from @Semaphoric as correct as all I did was put that reply into a simple script, so full credit to Semaphoric!

 

Please try the following content-aware version. It is only for flattened images, although it could easily work with a single-layered image too. You can mark this new script as a correct answer if it works for you. Please let me know how it goes and if any modifications are required.

 

This revised version adds the "protection" parameter which uses Alpha 1 to protect areas from the content-aware transform with a 50% protection value:

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/resize-canvas-width-to-60-of-the-height/m-p/12899014
v1.1, Stephen Marsh, 23rd April 2022
*/

#target photoshop
app.bringToFront();
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// 0.6 = 60%
var W = activeDocument.height.value * 0.6 / activeDocument.width.value * 100;
if (app.activeDocument.activeLayer.isBackgroundLayer) {
    activeDocument.activeLayer.name = 'Content Aware Scale Temp';
    contentAwareTransformProtected(0, 0, W, 100, true, true, 100, "Alpha 1");
    app.activeDocument.trim(TrimType.TRANSPARENT);
    app.activeDocument.flatten();
    deleteAlphaByName("Alpha 1");
} else {
    alert("This script is only intended for flattened images!");
}
app.preferences.rulerUnits = savedRuler;

function deleteAlphaByName(chaName) {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putName( s2t( "channel" ), chaName );
	descriptor.putReference( s2t( "null" ), reference );
	executeAction( s2t( "delete" ), descriptor, DialogModes.NO );
}

function contentAwareTransformProtected(horizontal, vertical, width, height, contentAware, skinTone, amount, channelName) {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	descriptor.putReference( s2t( "null" ), reference );
	descriptor.putEnumerated( s2t( "freeTransformCenterState" ), s2t( "quadCenterState" ), s2t( "QCSAverage" ));
	descriptor2.putUnitDouble( s2t( "horizontal" ), s2t( "pixelsUnit" ), horizontal );
	descriptor2.putUnitDouble( s2t( "vertical" ), s2t( "pixelsUnit" ), vertical );
	descriptor.putObject( s2t( "offset" ), s2t( "offset" ), descriptor2 );
	descriptor.putUnitDouble( s2t( "width" ), s2t( "percentUnit" ), width );
	descriptor.putUnitDouble( s2t( "height" ), s2t( "percentUnit" ), height );
	descriptor.putEnumerated( s2t( "interfaceIconFrameDimmed" ), s2t( "interpolationType" ), s2t( "bicubic" ));
	descriptor.putBoolean( s2t( "contentAware" ), contentAware );
	descriptor.putBoolean( s2t( "skinTone" ), skinTone );
	descriptor.putDouble( s2t( "amount" ), amount );
	descriptor.putString( s2t( "channelName" ), channelName );
	executeAction( s2t( "transform" ), descriptor, DialogModes.NO );
}

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

3 replies

Stephen Marsh
Community Expert
Community Expert
April 13, 2022

I second the approach offered by @Semaphoric – if you need this automated, then you would need a script, such as this basic example:

 

var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var W = activeDocument.height.value * 0.6; // 60%
activeDocument.resizeCanvas(W, null, AnchorPosition.MIDDLECENTER);
app.preferences.rulerUnits = savedRuler;

 

Participant
April 22, 2022

Thank you very much @Stephen Marsh !

 

By any chance, would you know how to do the exact same thing while being Content-Aware so that it resizes the canvas with the object centered? Kind of like what the Content-Aware Scale function does.

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
April 22, 2022

@Boute240157133512 – Thank you for marking my script post as a correct answer. I marked the reply from @Semaphoric as correct as all I did was put that reply into a simple script, so full credit to Semaphoric!

 

Please try the following content-aware version. It is only for flattened images, although it could easily work with a single-layered image too. You can mark this new script as a correct answer if it works for you. Please let me know how it goes and if any modifications are required.

 

This revised version adds the "protection" parameter which uses Alpha 1 to protect areas from the content-aware transform with a 50% protection value:

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/resize-canvas-width-to-60-of-the-height/m-p/12899014
v1.1, Stephen Marsh, 23rd April 2022
*/

#target photoshop
app.bringToFront();
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// 0.6 = 60%
var W = activeDocument.height.value * 0.6 / activeDocument.width.value * 100;
if (app.activeDocument.activeLayer.isBackgroundLayer) {
    activeDocument.activeLayer.name = 'Content Aware Scale Temp';
    contentAwareTransformProtected(0, 0, W, 100, true, true, 100, "Alpha 1");
    app.activeDocument.trim(TrimType.TRANSPARENT);
    app.activeDocument.flatten();
    deleteAlphaByName("Alpha 1");
} else {
    alert("This script is only intended for flattened images!");
}
app.preferences.rulerUnits = savedRuler;

function deleteAlphaByName(chaName) {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putName( s2t( "channel" ), chaName );
	descriptor.putReference( s2t( "null" ), reference );
	executeAction( s2t( "delete" ), descriptor, DialogModes.NO );
}

function contentAwareTransformProtected(horizontal, vertical, width, height, contentAware, skinTone, amount, channelName) {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	descriptor.putReference( s2t( "null" ), reference );
	descriptor.putEnumerated( s2t( "freeTransformCenterState" ), s2t( "quadCenterState" ), s2t( "QCSAverage" ));
	descriptor2.putUnitDouble( s2t( "horizontal" ), s2t( "pixelsUnit" ), horizontal );
	descriptor2.putUnitDouble( s2t( "vertical" ), s2t( "pixelsUnit" ), vertical );
	descriptor.putObject( s2t( "offset" ), s2t( "offset" ), descriptor2 );
	descriptor.putUnitDouble( s2t( "width" ), s2t( "percentUnit" ), width );
	descriptor.putUnitDouble( s2t( "height" ), s2t( "percentUnit" ), height );
	descriptor.putEnumerated( s2t( "interfaceIconFrameDimmed" ), s2t( "interpolationType" ), s2t( "bicubic" ));
	descriptor.putBoolean( s2t( "contentAware" ), contentAware );
	descriptor.putBoolean( s2t( "skinTone" ), skinTone );
	descriptor.putDouble( s2t( "amount" ), amount );
	descriptor.putString( s2t( "channelName" ), channelName );
	executeAction( s2t( "transform" ), descriptor, DialogModes.NO );
}

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

Semaphoric
Community Expert
Community Expert
April 13, 2022

You could do this: In the Canvas Size dialog, highlight the Height field (in pixels), and copy it to the clipboard. Highlight the Width field, and paste the Height field into it, followed by "*0.6" ("times 60%"), and click OK.

 

TheDigitalDog
Inspiring
April 13, 2022

Why can't you set the units from pixels to the percentage you wish, whatever dimension you want to be 60% (in this case height)? It is going to change the aspect ratio and pop a warning of this.

I get 60% of 1000 (600 pixels) as seen below.

Author “Color Management for Photographers" & "Photoshop CC Color Management/pluralsight"