Page 1 of 1

Detect if a picture is in landscape or portrait mode? find out width+height?

Posted: 2015-07-18T23:04:03-07:00
by bensto
Assume I have a big picture in portrait format. I want to resize it so that the height is exactly 800 pixel.
Therefore I can use the following command:

convert <myfilename> -resize x800 -quality 97 <myfilenamenew>

I put this command (with corresponding variables) into a dos batch script. So I can simply drag a file on it and it will be resized.

Fine.

However sometimes I have not a picture in portrait but in landscape mode.

These kind of pictures should be resized so that the width should be 800 pixel.

How can I find out if a picture is in landscape or portrait mode? I need something like:

Code: Select all

if getwidth(<myfilename>)  >  getheight(<myfilename>) then
   convert <myfilename> -resize y800 -quality 97 <myfilenamenew>
else
   convert <myfilename> -resize x800 -quality 97 <myfilenamenew>
Ben

Re: Detect if a picture is in landscape or portrait mode? find out width+height?

Posted: 2015-07-18T23:22:15-07:00
by fmw42
convert <myfilename> -resize y800 -quality 97 <myfilenamenew>
This should be

Code: Select all

convert <myfilename> -resize 800x -quality 97 <myfilenamenew>
Here is how I do it:

Test the w/h aspect ratio. If larger than 1, it is landscape. If smaller than 1, it is portrait. If 1, it is square.

unix syntax:

Code: Select all

aspect=$(convert image -format "%[fx:w/h]" info:)
echo "aspect=$aspect"
Or more completely:

If test=1, then landscape. If test=0, then portrait or square

Code: Select all

test=`convert image -format "%[fx:(w/h>1)?1:0]" info:`
if [ $test -eq 1 ]; then
convert image -resize 800x result
else
convert image -resize x800 result
fi

Re: Detect if a picture is in landscape or portrait mode? find out width+height?

Posted: 2015-07-19T03:23:32-07:00
by bensto
Ok, thank you.

However I am working under Windows 7
Your code is unix shell script style.
So backticks do not work here.
Similarly a % percentage sign has a different meaning under Windows

You have a corresponding version for DOS batch scripts?

Re: Detect if a picture is in landscape or portrait mode? find out width+height?

Posted: 2015-07-19T04:42:14-07:00
by Bonzo
You could try researching batch scripts as you are only reading the format into a variable and then and then using an if statement.

To get a correct answer you should have stated your operating system or preferred language at the start, as there are so many way to do things.

Re: Detect if a picture is in landscape or portrait mode? find out width+height?

Posted: 2015-07-19T10:48:13-07:00
by fmw42
bensto wrote:Ok, thank you.

However I am working under Windows 7
Your code is unix shell script style.
So backticks do not work here.
Similarly a % percentage sign has a different meaning under Windows

You have a corresponding version for DOS batch scripts?
Sorry, I do not program DOS batch scripts. One of the Windows users would need to provide that. This is why it is important that you always provide your IM version and platform, since even IM syntax may be different.

Re: Detect if a picture is in landscape or portrait mode? find out width+height?

Posted: 2017-05-03T12:36:24-07:00
by twobob

Code: Select all

import os, subprocess
subprocess.run(['Magick', './IMG_3160.jpg' ,'-identify' , '-format' ,'"%[fx:(w/h>1)?1:0]"','info:'], stdout=subprocess.PIPE).stdout.decode('utf-8')[-2]
Quick hack on pyhon (on windows)

Returns 1 or 0 - Zero is portrait.
think it returns 1 on a perfectly square one...

Hope it helps
https://gist.github.com/twobob/32c7d301 ... f2f504592a

Re: Detect if a picture is in landscape or portrait mode? find out width+height?

Posted: 2017-05-03T12:36:59-07:00
by twobob

Code: Select all

import os, subprocess
subprocess.run(['Magick', './IMG_3160.jpg' ,'-identify' , '-format' ,'"%[fx:(w/h>1)?1:0]"','info:'], stdout=subprocess.PIPE).stdout.decode('utf-8')[-2]
Quick hack on python (on windows)

Returns 1 or 0 - Zero is portrait. 1 is landscape
think it returns 1 on a perfectly square one too

Hope it helps
https://gist.github.com/twobob/32c7d301 ... f2f504592a

Re: Detect if a picture is in landscape or portrait mode? find out width+height?

Posted: 2017-05-03T12:46:09-07:00
by snibgo
twobob wrote:think it returns 1 on a perfectly square one too
A square image will have w==h, so w/h==1, so (w/h>1) is false, so it will return zero.

Re: Detect if a picture is in landscape or portrait mode? find out width+height?

Posted: 2017-05-03T13:43:58-07:00
by twobob
nice catch. thanks for the heads up. the FX: INFO: syntax is day 1 for me, but Boolean logic I should have got ;)