Jump to content
You must now use your email address to sign in [click for more info] ×

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.10.5 Designer 1.10.5 and Publisher 1.10.5 (mainly Photo), now ex-Adobe CC

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

Link to comment
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.10.5 Designer 1.10.5 and Publisher 1.10.5 (mainly Photo), now ex-Adobe CC

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

Link to comment
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.10.5 Designer 1.10.5 and Publisher 1.10.5 (mainly Photo), now ex-Adobe CC

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

Link to comment
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.10.5 Designer 1.10.5 and Publisher 1.10.5 (mainly Photo), now ex-Adobe CC

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

Link to comment
Share on other sites

  • 2 months later...
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.10.5 Designer 1.10.5 and Publisher 1.10.5 (mainly Photo), now ex-Adobe CC

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

Link to comment
Share on other sites

  • 7 months later...
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 • macOS Monterey

Link to comment
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.10.5 Designer 1.10.5 and Publisher 1.10.5 (mainly Photo), now ex-Adobe CC

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

Link to comment
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 • macOS Monterey

Link to comment
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.10.5 Designer 1.10.5 and Publisher 1.10.5 (mainly Photo), now ex-Adobe CC

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

Link to comment
Share on other sites

  • 4 weeks later...
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.10.5 Designer 1.10.5 and Publisher 1.10.5 (mainly Photo), now ex-Adobe CC

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

Link to comment
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.10.5 Designer 1.10.5 and Publisher 1.10.5 (mainly Photo), now ex-Adobe CC

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

Link to comment
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.10.5 Designer 1.10.5 and Publisher 1.10.5 (mainly Photo), now ex-Adobe CC

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

Link to comment
Share on other sites

  • 1 year later...
8 hours ago, Zenfoto said:

Does this Macro resize the original files or non-destructively create resized copies?

The macro works on the current (Pixel) layer, so it is destructive. I have not tried it on multi-layered objects. You will need to save or export your image under a different name.

John

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

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

Link to comment
Share on other sites

  • 4 months later...
  • 3 years later...
14 hours ago, JIPJIP said:

Hi,

I don't know if there is a more recent topic about this subject.

Is there now (2024) a way to resize an image AND specify the method (bilinear, bicubic, etc ..;) in a macro ?


Thanx

You can specify the resampling algorithm using Document > Resize in a macro, but the saved macro always uses the original image propeties when running, not those of the image you apply it to.

Using Equations avoids this problem, but you cannot specify the resampling algorthm.

John

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

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...

Important Information

Terms of Use | 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.