Identify command to output to Variable

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
Sergio
Posts: 3
Joined: 2010-12-27T23:24:13-07:00
Authentication code: 8675308

Identify command to output to Variable

Post by Sergio »

I'm so close, but the syntax must be off. I'm using the identify command to see the height & width of an image. I then need to see that as a variable so that it can be processed by an If/Then statement. I've tried the following things but they are all wrong. Does anyone know how to output the height & width (separately) so that I can process it with conditional logic?

Code: Select all

#!/bin/bash
identify sand.jpg -format "%f,%w,%h"
echo %w

Code: Select all

#!/bin/bash
identify sand.jpg -format "%f,%w,%h"
echo $%w

Code: Select all

#!/bin/bash
identify sand.jpg -format "%f,%w,%h"
echo $(%w)

Code: Select all

#!/bin/bash
identify sand.jpg -format "%f,%w,%h"
echo $(w)

Code: Select all

#!/bin/bash
identify sand.jpg -format "%f,%w,%h" > $newVariable
echo$newVariable

Code: Select all

#!/bin/bash
newVariable=identify sand.jpg -format "%f,%w,%h" 
echo$newVariable
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Identify command to output to Variable

Post by fmw42 »

try either

newVariable=`identify -format "%f,%w,%h" sand.jpg`

or

newVariable=`convert sand.jpg -format "%f,%w,%h"`


note these are not normal quotes, but slanted quotes - same key as the tilde - to the left of the 1 key

or

newVariable=$(convert sand.jpg -format "%f,%w,%h")

or

newVariable=$(identify -format "%f,%w,%h" sand.jpg)

but you will get 3 items in one variable, so you may want to make 3 variables to start
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Identify command to output to Variable

Post by anthony »

A special way of extracting three values (space separated) into three variables in bash is this special bash 'trick'

Code: Select all

read -r file width height <<< $( convert sand.jpg -format "%f %w %h" info:)
echo filename=$file
echo width=$width
echo height=$height
This only works with BASH, not an older bourne shell or ksh or zsh, php, perl etc. JUST BASH.
Other languages have other ways of parsing strings.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply