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

Batch resize of images with variable size and aspect ratio to new common aspect ratio & size?


Recommended Posts

11 minutes ago, Lagarto said:

I'm on Windows, but if I put w300 and h200 (ran a test with smaller sizes) and keep the aspect ratio, I get 200px high images width of which varies depending on the original aspect ratio. I get exactly same output if I use min(w,300)+max(w,300)-w and min(h,200)+max(h,200)-h. Does not any w and h put in these formulae produce 300 and 200?

if w = 100: min(100,300)+max(100,300)-100 = 100 + 300 - 100 = 300
if w = 1000: min(1000,300) + max(1000,300)-1000 = 300 + 1000 - 1000 = 300
if h = 100: min(100,200)+max(100,200)-100 = 100 + 200 - 100 = 200
if h = 1000: min(1000,200) + max(1000,200)-1000 = 200 + 1000 - 1000 = 200

I may have failed to understand OP's request but I think that they want to have a canvas with fixed size (3000 x 2000), and I think this requires two rounds, one with batch resize and another with a macro that fixes the canvas size. Perhaps they can be done in one go using a macro, but I have barely had a look on macro feature of Photo so cannot help there.

Sorry, I do not understand what you are trying to say

But if I use your formulas of...

min(w,300)+max(w,300)-w and min(h,200)+max(h,200)-h

on my test image of 6000 x 2920px

I get a final image size of 300 x 145px, which is more or less what I expect***

Under no circumstances would I expect to see 300 x 200px

 

*** It should be 300 x 146px, not sure why we lose a pixel

To save time I am currently using an automated AI to reply to some posts on this forum. If any of "my" posts are wrong or appear to be total b*ll*cks they are the ones generated by the AI. If correct they were probably mine. I apologise for any mistakes made by my AI - I'm sure it will improve with time.

Link to comment
Share on other sites

43 minutes ago, Franzi von Fragenfeld said:

As to clarify as to what I would like to have, final dimension of all images 3000x2000. BUT, and this is a big BUT, all original images have to fit each horizontally on a 3000 x 2000 canvas while maintaining their original aspect ratio. No matter if they where bigger or smaller before.

Here is a first pass at a macro to do what I think you want. It is based on my macros for resizing to a fixed maximum width or height.

Layer Unlock
Layer > Rasterize
Filter > Distort > Equations 
x=x*max(w/3000,h/2000)
y=y*max(w/3000,h/2000)
Document > Clip Canvas
Layer > Rasterize
Select > Alpha Range > Select Partially Transparent
Edit > Fill> Inpainting
Select > Deselect

 

I have tried it on a few images larger than the target frame size. It will not work properly if the original image is smaller than the target frame.

Here is the single macro to perform the task. If it is OK, I will see if I can provide a library of macros to perform resizing to different sizes and possibly one with variable parameters.

Resize to 3000-2000 frame.afmacro

 

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

31 minutes ago, Lagarto said:

I'm on Windows, but if I put w300 and h200 (ran a test with smaller sizes) and keep the aspect ratio, I get 200px high images width of which varies depending on the original aspect ratio. I get exactly same output if I use min(w,300)+max(w,300)-w and min(h,200)+max(h,200)-h. Does not any w and h put in these formulae produce 300 and 200?

if w = 100: min(100,300)+max(100,300)-100 = 100 + 300 - 100 = 300
if w = 1000: min(1000,300) + max(1000,300)-1000 = 300 + 1000 - 1000 = 300
if h = 100: min(100,200)+max(100,200)-100 = 100 + 200 - 100 = 200
if h = 1000: min(1000,200) + max(1000,200)-1000 = 200 + 1000 - 1000 = 200

Yes, that's what I've been telling @carl123: his formulae add redundant complexity. They evaluate to the same value for any w and the same value for any h.

16 minutes ago, carl123 said:

I may have failed to understand OP's request but I think that they want to have a canvas with fixed size (3000 x 2000), and I think this requires two rounds, one with batch resize and another with a macro that fixes the canvas size.

Yes, there needs to be a second batch that simply resizes the canvas without distorting the image.

 

Link to comment
Share on other sites

29 minutes ago, carl123 said:

Sorry, I do not understand what you are trying to say

But if I use your formulas of...

min(w,300)+max(w,300)-w and min(h,200)+max(h,200)-h

on my test image of 6000 x 2920px

I get a final image size of 300 x 145px, which is more or less what I expect***

Under no circumstances would I expect to see 300 x 200px

 

*** It should be 300 x 146px, not sure why we lose a pixel

1. The formulas are redundant: entering 300 in the output width field and 200 in the output height field is all that is required to get exactly the same result as the formulae produce. I suggest you test that yourself if you remain unconvinced.

2. Regarding the 300 x 146 output: you already know that a second batch job must be run with a macro to simply resize the canvas to 300 x 200 without distorting the image.

 

Edited by anon2
Link to comment
Share on other sites

2 hours ago, John Rostron said:

It will not work properly if the original image is smaller than the target frame

You may(?) be able to get around that by first ensuring all images are resized larger than the target frame

e.g. In a macro....

1. Resize the canvas to a large size
2. Resize all images to 3 times existing size (e.g. this should then work with images as small as 1000px width)
resize the images with a Polar Equation of r=r/3

3. Clip Canvas & Rasterize (cleaning up steps) 
4. Then run your original macros steps

How much you resize the canvas (in step 1) will depend on the OP's largest image size they use. e.g. if they have an image of 5000px width the resized canvas will need to be a minimum of 15000px to accommodate the 3 times enlargement done in step 2.

To save time I am currently using an automated AI to reply to some posts on this forum. If any of "my" posts are wrong or appear to be total b*ll*cks they are the ones generated by the AI. If correct they were probably mine. I apologise for any mistakes made by my AI - I'm sure it will improve with time.

Link to comment
Share on other sites

5 minutes ago, carl123 said:

You may(?) be able to get around that by first ensuring all images are resized larger than the target frame

I had thought about that, but I had forgotten the trick of using the polar co-ordinates expansion. 

I will see if I can put it all together.

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

I have modified my macro to perform an initial canvas enlargement to a width of 20000px by 15000px. The central lock must be unlocked and the position set to the centre. Note that whatever dimensions you specify here get baked into the macro. It then applies Equations using polar co-ordinates and r=r/3. This enlarges the image threefold. If one of your images is less than a third of the 3000x2000 frame, then you need to increase the 3 to (say) 4.

The next steps simply trim the canvas and rasterise it.

Now apply Equations again using the same scaling factor for each axis. This preserves the Aspect Ratio.

The next steps are to trim and rasterise the image again.

The actions with the alpha are to remove any stray blank areas around the edges.

An optional final stage is to resize the canvas to 3000x2000 and add a white fill layer behind it.

Here are the macro steps to create an image that will fit within a 3000x2000 frame:

Layer > Unlock
Layer > Rasterise
Document > Resize Canvas     
Unlock, centre and set dimensions to 20000 x 15000
Filter > Distort > Equations      Set Polar Co-ordinates and r=r/3, then Apply
Document > Clip Canvas
Layer > Rasterise and Trim
Filter > Distort > Equations       
Enter the following in the x and y fields, then Apply
     x*max(w/3000,h/2000)
     y*max(w/3000,h/2000)
Document > Clip Canvas
Layer > Rasterise
Select > Alpha Range > Select Partially Transparent
Edit > Fill> Inpainting
Select > Deselect

If you want a white background, add the following steps to the Macro:

Document > Resize Canvas     Unlock and centre, set dimensions to 3000 and 2000
Layer > New Fill Layer              This will be white by default
Arrange > Move to Back
Document > Flatten                  
(Optional)

Here is the macro without adding the final steps:

Resize to 3000x2000 frame.afmacro

 

EDIT:

I have just tried my original macro above on a wider variety of sizes and shapes (orientation). It seems to work for many but not others. The problem seems to be that the canvas expansion in step 3 was insufficient. I have re-written the macro using a canvas expansion of 20000 x 15000. With this modification it works well. However, if you have original images greater than 20000/3=6666px by 15000/3=5000px, you will need to enlarge the canvas even more! I could make the macro to use an even bigger canvas, but the bigger the canvas, the slower it operates.

Edited by John Rostron
Errors detected and corrected

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

I have just tried my macro above on a wider variety of sizes and shapes (orientation). It seems to work for many but not others. I will report back when I have a definitive answer.

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

The final macro works, but slowly. The problem is the enlargement of the canvas followed by the enlargement of the image using equations on this large canvas. If the original image is larger than the target frame (3000 by 2000 in this case), then this prior enlargement is not necessary and could be omitted using my original macro. In that case it would run fairly quickly.

I would recommend to @Franzi von Fragenfeld (the OP) that, if possible, you separate the larger images from the smaller images and use the appropriate macro. Alternatively, you could run the smaller images through a simple batch process which enlarges them sufficiently. You could even run a batch process on all your images, large and small, to resize everything to some convenient intermediate size which would guarantee that the aspect ratio is not compromised, perhaps 5000 width. 

The problem is that the Filter > Distort > Equations operations will only operate on the existing canvas. It cannot write to pixels outside this canvas. Thus the canvas smaller than the target needs to be enlarged prior to applying the 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

@Lagarto

You are right! I had been assuming all along that specifying exact values in the W and H columns would coerce the output file to those exact dimensions. I have just tried a few trial runs with files larger and smaller than the target, and it all works perfectly!

So forget all my fancy equations and macros. @anon2had already suggested this earlier in this thread!

John

@carl123, @Franzi von Fragenfeld

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

It also occurs to me that my various macros to resize images to a fixed maximum size could be more simply be done using  batch job and specifying that value for both W and H. 

I will try it and see.

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

Well such things like Franziska want's to do can IMO be much better done with other batch processing tools, some which offer here more capabilities and a much better dynamic manipulation handing. So I would let ImageMagick or GraphicsMagick etc. do such tasks.

  • Resize or Scaling
    Quote

    convert input.jpg -resize 3000x2000 -background white -gravity center output.jpg

     

@Franzi von Fragenfeld Kleiner Tip probier es mal hiermit, gib die neue gewünschte Bilddimension an, bei Beibehaltung des Aspekt Ratio des Ursprungbildes und setze den zu verwendenden Füllhintergrund (Dein quasi Canvas) auf weiss ...

 

☛ Affinity Designer 1.10.8 ◆ Affinity Photo 1.10.8 ◆ Affinity Publisher 1.10.8 ◆ OSX El Capitan
☛ Affinity V2.3 apps ◆ MacOS Sonoma 14.2 ◆ iPad OS 17.2

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.