Jump to content
John Rostron

Resize an image to a fixed maximum size

Recommended Posts

In another thread in Questions, I have been discussing with others how to write a macro that will resize an image so that the longest side is a fixed value. For example, if I resize a 4000x3000 image to maximum of 800, it will give a 800x600 image. For a portrait image, the image will become 600X800.

After many iterations, I came up with a set of macros included here in a macro library: Resize to fixed max size.afmacros, which is attached here.

This includes five macros:

Resize to 640 max
Resize to 800 max
Resize to 1280 max
Resize to 1600 max
Resize to variable max side

The first four will resize any image to the designated maximum size. The last one includes a parameter which allows you to partly-specify the target size. The default target size is 2000px, but for any other value (up to a maximum of 4000, use the pop-up to adjust. The value of the parameter is the desired maximum size divided by 4000. A convenient way of specifying the final maximum edge size is to enter a fraction. If your target is (say) 800px then, in the parameter box (not the slider), enter 800/4000. If your target is 3200, enter 3200/4000.

I have given technical details of the macro in subsequent messages in this thread.

 

EDIT:

This is a revised version of the macros (as of 2nd November 2018). For details of the changes, see the item below which gives the macro details). The changes were in response to a user who found that if he cropped the image first, the macro did not work.

 

Resize to fixed maximum size.afmacros

Edited by John Rostron
Modified to improve useability

Windows 10, Affinity Photo 1.7 and Designer 1.7, (mainly Photo), now ex-Adobe CC

CPU: AMD A6-3670. RAM: 16 GB DDR3 @ 666MHz, Graphics: 2047MB NVIDIA GeForce GT 630

Share this post


Link to post
Share on other sites

Some technical details about these macros.

In the thread mentioned I had helpful comments and advice from @carl123, @R C-R and @v_kyr.

The problem with writing such a macro is to identify the longest side. The basic formula I used for this is:
max(w,h)
which takes the larger of the two sides, and selects the width or height accordingly.

I had to use the Distort > Equations filter to implement this, we have found that Document > Resize and the Transform functions will not work in macros. The final equations for x and y in the Distort filter are:
(x/800)*max(h,w)
(y/800)*max(h,w)

The final macro, with the parameter is:
(x/(4000*a))*max(h,w)
(y/(4000*a))*max(h,w)

 


Windows 10, Affinity Photo 1.7 and Designer 1.7, (mainly Photo), now ex-Adobe CC

CPU: AMD A6-3670. RAM: 16 GB DDR3 @ 666MHz, Graphics: 2047MB NVIDIA GeForce GT 630

Share this post


Link to post
Share on other sites

Hello John,
Thanks for sharing, I love the little macros that make the job easier


Affinity Photo 1.7.3.481 :   Affinity Photo-Beta 1.8.0.486   :    Affinity Designer 1.7.3.481:  Affinity Designer-Beta 1.8.0.486:

Affinity Publisher-Beta 1.7.3.475:   Affinity Publisher 1.7.3.481:    Windows 10 Pro  (Version  1903 Build 18362.295)

 

Share this post


Link to post
Share on other sites

Hello, that's a very helpful macro. Especially for use in batch mode.
Thanks for that.
Dieter


Germany, Affinity Photo WIN 1.7 , LR6, WIN10

Share this post


Link to post
Share on other sites

More technical details:

When we were looking at various algorithms, @carl123 came up with an ingenious and elegant method for resizing. Instead of using separate x and y coordinates (Cartesian coordinates), he proposed using polar coordinates, which essentially would scale everything from the origin (0,0) of the image. This would render finding the longest side redundant.

Unfortunately we found that if the image side was an odd number of pixels, the scaling in the Equations engine rounded the answer up, so that it became one more than the target size! There are workarounds for this, but it mean that an implementation was not as simple as the Cartesian coordinate algorithm.


Windows 10, Affinity Photo 1.7 and Designer 1.7, (mainly Photo), now ex-Adobe CC

CPU: AMD A6-3670. RAM: 16 GB DDR3 @ 666MHz, Graphics: 2047MB NVIDIA GeForce GT 630

Share this post


Link to post
Share on other sites

Do it yourself:

If you want to write your own version of this macro for your favourite maximum size, these are the steps:

Layer > Unlock
Layer > Rasterize and Trim

Filter > Distort > Equations Enter the following in the x and y fields, substituting your own maximum size, then Apply
   (x/MAXSIZE)*max(h,w)
   (y/
MAXSIZE)*max(h,w)
Document > Clip Canvas
Layer > Rasterize

Select > Alpha Range > Select Partially Transparent
Edit > Fill> Inpainting
Select > Deselect

I thank @carl123 for the last four steps.

EDIT: These macros have been edited to add the first two steps. Unlocking is necessary, but I had not previously included it in the Macro. The Rasterize step is necessary if the image has been cropped previously.

EDIT 2: I have amended step 2 to use Rasterize and Trim (rather than just Rasterize). This is new in 1.7.

Edited by John Rostron
Modified to improve useability and updated step 2

Windows 10, Affinity Photo 1.7 and Designer 1.7, (mainly Photo), now ex-Adobe CC

CPU: AMD A6-3670. RAM: 16 GB DDR3 @ 666MHz, Graphics: 2047MB NVIDIA GeForce GT 630

Share this post


Link to post
Share on other sites

John, thanks for the information on how to do this, complicated as it is. Do you happen to know if this method allows to specify the resizing method (i.e. bilinear, bicubic, etc.)? If not, do you know what the default resizing method is?

Share this post


Link to post
Share on other sites
14 hours ago, Traveler said:

John, thanks for the information on how to do this, complicated as it is. Do you happen to know if this method allows to specify the resizing method (i.e. bilinear, bicubic, etc.)? If not, do you know what the default resizing method is?

Fraid not. The Distort filter offers no options for a resizing method. You can write a macro for Document > Resize, with a specific size and include the resizing method of your choice, but you do not have the flexibility of the Equations filter.

 

John


Windows 10, Affinity Photo 1.7 and Designer 1.7, (mainly Photo), now ex-Adobe CC

CPU: AMD A6-3670. RAM: 16 GB DDR3 @ 666MHz, Graphics: 2047MB NVIDIA GeForce GT 630

Share this post


Link to post
Share on other sites
7 hours ago, John Rostron said:

Fraid not. The Distort filter offers no options for a resizing method. You can write a macro for Document > Resize, with a specific size and include the resizing method of your choice, but you do not have the flexibility of the Equations filter.

 

John

 

Thanks for clarifying, John.

Share this post


Link to post
Share on other sites
On 12/15/2017 at 8:05 AM, John Rostron said:

In another thread in Questions, I have been discussing with others how to write a macro that will resize an image so that the longest side is a fixed value. For example, if I resize a 4000x3000 image to maximum of 800, it will give a 800x600 image. For a portrait image, the image will become 600X800.

After many iterations, I came up with a set of macros included here in a macro library: Resize to fixed max size.afmacros, which is attached here.

This includes five macros:

Resize to 640 max
Resize to 800 max
Resize to 1280 max
Resize to 1600 max
Resize to variable max side

The first four will resize any image to the designated maximum size. The last one includes a parameter which allows you to partly-specify the target size. The default target size is 2000px, but for any other size you need to right-click on this macro and choose edit, then click on the cogwheel next to the Equations entry. Parameter 1 is set to approximately 0.5. This is a multiplier of 4000, so 4000*0.5 gives the default  size of 2000. Changing this value scales the maximum size (0.3 gives 1200, 0.75 gives 3000 etc).

I have given technical details of the macro in subsequent messages in this thread.

 

These resize macros don't seem to work on a cropped image.


Website

Mac mini (2018)  3.2 GHz Intel Core i7  64 GB   Radeon Pro 580 8 GB

Share this post


Link to post
Share on other sites
1 minute ago, TomM1 said:

These resize macros don't seem to work on a cropped image.

This is what the Document > Clip Canvas and Layer > Rasterize lines were supposed to resolve. I was having your problem unti I inserted them, after which it worked OK. I have just reinstalled Windows 10, so I am not able to test this myself at present. Could you try running those two lines first before the macro?

John


Windows 10, Affinity Photo 1.7 and Designer 1.7, (mainly Photo), now ex-Adobe CC

CPU: AMD A6-3670. RAM: 16 GB DDR3 @ 666MHz, Graphics: 2047MB NVIDIA GeForce GT 630

Share this post


Link to post
Share on other sites
On 9/29/2018 at 11:49 AM, John Rostron said:

This is what the Document > Clip Canvas and Layer > Rasterize lines were supposed to resolve. I was having your problem unti I inserted them, after which it worked OK. I have just reinstalled Windows 10, so I am not able to test this myself at present. Could you try running those two lines first before the macro?

John

I don’t know to do what you suggest.


Website

Mac mini (2018)  3.2 GHz Intel Core i7  64 GB   Radeon Pro 580 8 GB

Share this post


Link to post
Share on other sites
2 minutes ago, TomM1 said:

I don’t know to do what you suggest.

With your cropped pixel layer selected, first apply  Document > Clip Canvas 

and then apply Layer > Rasterize 

and then apply the macro. 

I have not yet reinstalled Affinity on my computer so I cannot try it out myself yet.

John


Windows 10, Affinity Photo 1.7 and Designer 1.7, (mainly Photo), now ex-Adobe CC

CPU: AMD A6-3670. RAM: 16 GB DDR3 @ 666MHz, Graphics: 2047MB NVIDIA GeForce GT 630

Share this post


Link to post
Share on other sites
On 9/29/2018 at 6:40 PM, TomM1 said:

These resize macros don't seem to work on a cropped image.

@TomM1, I have not forgotten you. My re-install of Windows and Affinity lost my own macro files. Fortunately I had posted most of them here so I should be able to retrieve them. I should be able to attend to them in the next few days. Could you post a cropped image that you have been having problems with (as an .afphoto file)?

John


Windows 10, Affinity Photo 1.7 and Designer 1.7, (mainly Photo), now ex-Adobe CC

CPU: AMD A6-3670. RAM: 16 GB DDR3 @ 666MHz, Graphics: 2047MB NVIDIA GeForce GT 630

Share this post


Link to post
Share on other sites
On 9/29/2018 at 6:40 PM, TomM1 said:

These resize macros don't seem to work on a cropped image.

You are correct. All my experimentation was on newly loaded images. I have found that if I crop the image, then apply the macro, it applies the resizing to the cropped area, but then adds back the area around the crop!

The solution is to Rasterize before applying the macro. It then works as expected. I will modify the macros to include this Rasterize step and re-post.

John


Windows 10, Affinity Photo 1.7 and Designer 1.7, (mainly Photo), now ex-Adobe CC

CPU: AMD A6-3670. RAM: 16 GB DDR3 @ 666MHz, Graphics: 2047MB NVIDIA GeForce GT 630

Share this post


Link to post
Share on other sites
18 hours ago, John Rostron said:

I will modify the macros to include this Rasterize step and re-post.

This has now been done. The macro set in the first post now includes these amendments.

John


Windows 10, Affinity Photo 1.7 and Designer 1.7, (mainly Photo), now ex-Adobe CC

CPU: AMD A6-3670. RAM: 16 GB DDR3 @ 666MHz, Graphics: 2047MB NVIDIA GeForce GT 630

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

These are the Terms of Use you will be asked to agree to if you join the forum. | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.