1
votes

edit: End goal is a 2:1 aspect ratio, no cropping, and a max size of 1260x630. If the image dimensions are originally below 1260x630, they should stay below 1260x630.

Struggling to word this well (which very well may be the reason I haven't found a good answer).

With ImageMagick I'm trying to resize an image down to 1260x630 provided the image itself is larger than the given size (width OR height).

Once resized I need to change the aspect ratio to a 2:1 without cutting off the image. I've accomplished this well enough with larger images using the following:

convert foobar-original.jpg \
   -resize 1260x630\>       \
   -gravity center          \
   -extent 1260x630         \
    foobar.jpg

A 1500x500 picture, for example, using the above, becomes first 1260x420 (with the resize) and then 1260x630 (via the extent), and the extra width is filled in with white background.

However, if the image starts off smaller, 800x200 for example, the image is not resized (as expected) but then is made to fit within a full 1260x630. This is not the desired result.

I would rather -extent simply affect the aspect ratio, adding on white as needed. So rather than the 800x200 image converting to 1260x630, with the 800x200 part of the image in the center, I would like the image to be converted to 800x400 (that 2:1 ratio), the extra height filled in with white space.

Basically, I'm looking for a way to set -extent to an aspect ratio, rather than a specific size or percentage. Something like

convert foobar-original.jpg \
   -resize 1260x630\>       \
   -gravity center          \
   -extent 2:1              \
    foobar.jpg
2

2 Answers

3
votes

The first resize you are doing,

-resize 1260x630\>

operates as "resize to 1260x630, but only shrink larger images" (via the > flag).

There is a similar operation which you can apply,

-resize 1260x630\<

It operates as "resize to 1260x630, but only enlarge smaller images" (via the < flag).

If you combine both operations in one command you should be getting the result you want:

convert foobar-original.jpg \
   -gravity center          \
   -resize  1260x630\>      \
   -resize  1260x630\<      \
   -extent  1260x630        \
    foobar.jpg
0
votes

I hat a similar problem an ended up to write a batch script:

@echo off
set magick_exe="C:\Programme Portable\ImageMagick-7.0.3-1\magick.exe"


for /R %%f in (*.png) do (
  call :body "%%f"
)

pause
goto :eof

:body
set FILE=%1
echo %FILE%

:start

%magick_exe% identify -format "%%[w]" %FILE% > w.txt
%magick_exe% identify -format "%%[h]" %FILE% > h.txt
set /p WIDTH=<w.txt
set /p HEIGHT=<h.txt
set /a HEIGHT2=%HEIGHT%*2


echo "size: " %WIDTH% x %HEIGHT% 

if  %HEIGHT2% gtr %WIDTH% (

  if %HEIGHT% gtr 630 (
     echo Resize hight to 630
     %magick_exe% convert %FILE% -resize x630 %FILE%
    goto :start
  )

  set /a SIZE_X=%HEIGHT%*2
  set /a SIZE_Y=%HEIGHT%

) else (

  if %HEIGHT% gtr 1260 (
     echo Resize width to 1260
     %magick_exe% convert %FILE% -resize 1260 %FILE%
    goto :start
  )

  set /a SIZE_X=%WIDTH%
  set /a SIZE_Y=%WIDTH%/2


)

echo "new size :" %SIZE_X% x  %SIZE_Y%

if %WIDTH% NEQ %HEIGHT2% ( %magick_exe% convert %FILE% -background white -gravity center -extent "%SIZE_X%x%SIZE_Y%"  %FILE% )

echo.
goto :eof