get heigth/width

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
goutosig
Posts: 3
Joined: 2011-01-05T10:43:11-07:00
Authentication code: 8675308

get heigth/width

Post by goutosig »

Hi all,

I am new to Image Magick and am trying to retrieve the width and height of an image so that I can name it in a different way whether it is a landscape or portrait image.
Here is my code:

Code: Select all

#!/bin/sh
	
for x in `ls originaux`
do

	width=`convert originaux/$x -format "%[fx:w]" info:`
	height=`convert originaux/$x -format "%[fx:h]" info:`

	if [ $height -lt $width ]
	then
		convert originaux/$x -rotate "2" - | convert - -shave 98x119 rotation/pa_$x
	else
		convert originaux/$x -rotate "2" - | convert - -shave 119x98 rotation/po_$x
	fi
done
I do get values for width and height, but the width is always 3456px and height always 2592px whereas my images definitely have different orientations, therefore, some images should be 2592px wide and 3456px high.

Can anyone help me?

G.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: get heigth/width

Post by fmw42 »

width=`convert originaux/$x -format "%[fx:w]" info:`
height=`convert originaux/$x -format "%[fx:h]" info:`
This appears to be the correct format and syntax to get the width and height for a given image. It should work. However, you might use miff:- in place of - for the output before the pipe.

Otherwise, check your IM version and be sure you are on a current version and not one that is very old.

If any images have spaces in them, then you should use quotes around the filenames
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: get heigth/width

Post by anthony »

Better still use the simplier more direct format %w and %h
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
goutosig
Posts: 3
Joined: 2011-01-05T10:43:11-07:00
Authentication code: 8675308

Re: get heigth/width

Post by goutosig »

Hi all,

actually it seems that my IM does not know whether the picture was taken in a landscape or portrait mode; it handles the portrait pictures in a landscape orientation.

Finally, this is not so disturbing to me... though it is strange and I was not able to correct it (I updated from IM 6.6.5 to 6.6.6).

thx for your replies
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: get heigth/width

Post by Bonzo »

Out of interest you are rotating the image no mater what oriantation it is ?

Code: Select all

if [ $height -lt $width ]
   then
      convert originaux/$x -rotate "2" - | convert - -shave 98x119 rotation/pa_$x
   else
      convert originaux/$x -rotate "2" - | convert - -shave 119x98 rotation/po_$x
goutosig
Posts: 3
Joined: 2011-01-05T10:43:11-07:00
Authentication code: 8675308

Re: get heigth/width

Post by goutosig »

Bonzo wrote:Out of interest you are rotating the image no mater what oriantation it is ?

Code: Select all

if [ $height -lt $width ]
   then
      convert originaux/$x -rotate "2" - | convert - -shave 98x119 rotation/pa_$x
   else
      convert originaux/$x -rotate "2" - | convert - -shave 119x98 rotation/po_$x
yes; I finally removed the IF block and kept the first instruction.
Post Reply