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

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
bensto
Posts: 31
Joined: 2012-07-02T00:32:10-07:00
Authentication code: 13

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
bensto
Posts: 31
Joined: 2012-07-02T00:32:10-07:00
Authentication code: 13

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

Post 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?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

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

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
twobob
Posts: 4
Joined: 2017-05-03T07:27:18-07:00
Authentication code: 1151

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

Post 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
twobob
Posts: 4
Joined: 2017-05-03T07:27:18-07:00
Authentication code: 1151

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
twobob
Posts: 4
Joined: 2017-05-03T07:27:18-07:00
Authentication code: 1151

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

Post 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 ;)
Post Reply