Question

I need to convert an image based on its aspect ratio.

Portrait images need to be converted to 500px width.

Landscape need to be converted to 800px width.

Is this possible with a single command line.

I am trying to figure this out for a while now but the only way o found is a sh script that gets the size using identify and calls the appropriate convert.

Était-ce utile?

La solution

if anyone is looking for this, here is how i find weather the image is landscape or portrait

a=$(identify -format "w=%w;h=%h" l.jpg)
eval $a

if [ "$w" -ge "$h" ]
then
  echo 'landscape'
else
  echo 'portrait'
fi

Autres conseils

You can do that in ImageMagick 7 in once command line, but in ImageMagick 6, you would need a shell script if conditional test.

In ImageMagick 7,

magick image -resize "%[fx:(w/h)>=1?800:500]x" result

Thanks for the script, here is a version to detect ratio taller or equal and wider than 16:9 and calls appropriate resize command (to bulk resize images for 4k wallpapers folder)

a=$(identify -format "w=%w;h=%h" $1)
eval $a

mh=$(($h * 16))

mw=$(($w * 9))

if [ "$mw" -ge "$mh" ]
then
  magick $1 -resize '3840' $1
else
  magick $1 -resize 'x2160' $1
fi
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top