You are not logged in.

#1 2012-02-29 20:53:57

inkdinky
Member
Registered: 2011-12-08
Posts: 62

[solved] passing ImageMagick options in script : unrecognized command

I'm working on a script that modifies a lot of images. I am trying to add the flexibility to pass in any valid IM argument and have it tacked on to a default resize operation.
I'm running into the problem where certain options give me the 'unrecoginized command' error even though they are valid.
I've included a test snippet. I'd be interested to see if my examples do/don't work for others.

#!/bin/bash
mogrify "${1}" -resize 800x900 "${2}"
#this won't work : bash ../IMargumenttest.sh '-modulate 150,50' img001.jpg 
#and gives :mogrify: unrecognized option `-modulate 150,50' @ error/mogrify.c/MogrifyImageCommand/5147.
#this will work : bash ../IMargumenttest.sh '-contrast' img001.jpg

I've tried passing the arguments as single and double quoted options to no avail.
Thanks in advance.
Nick

Last edited by inkdinky (2012-02-29 22:30:22)

Offline

#2 2012-02-29 20:56:22

inkdinky
Member
Registered: 2011-12-08
Posts: 62

Re: [solved] passing ImageMagick options in script : unrecognized command

just extra details as far as versioning goes:

convert -version
Version: ImageMagick 6.7.5-3 2012-02-11 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP

bash -version
GNU bash, version 4.2.20(2)-release (i686-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Offline

#3 2012-02-29 21:21:31

Xyne
Moderator/TU
Registered: 2008-08-03
Posts: 5,798
Website

Re: [solved] passing ImageMagick options in script : unrecognized command

When you run the command "mogrify -modulate 150,50 ...", mogrify receives a list of arguments: "-modulate", followed by "-150,50", followed by whatever else you passed it. When it sees the "-modulate" argument, it knows that the next argument is a parameter for the modulate argument.

With your quoting it instead receives a single argument "-modulate 150,50". There is no option "-modulate 150,50", so it throws the unrecognized error.

There are a few ways to handle this, depending on what you need to do. If the order of the arguments doesn't matter, then you can do this:

mogrify -resize 800x900 "$@"

then run the script with "script -modulate 150,50 /path/to/image".  This works because "$@" is expanded by bash into separate words, so mogrify receives all of them as separate arguments.

If the resize command has to be right at the end, then you can do this:

img="$1"
shift
mogrify "$@" -resize 800x900 "$img"

In that case, you have to call the script with the file path as the first argument, followed by any other arguments to mogrify. It assigns the file path to the variable "img", then it shifts it off the array of input arguments.


You could also do it with array indexing, but I think this is simpler.


edit: Just in case you were wondering why "$@" is expanded and "$1" isn't, it's because "$@" is treated specially to enable this type of usage. "$*" also contains all of the input arguments, but it is not expanded into separate words when quoted.

Last edited by Xyne (2012-02-29 21:23:40)

Offline

#4 2012-02-29 21:39:14

inkdinky
Member
Registered: 2011-12-08
Posts: 62

Re: [solved] passing ImageMagick options in script : unrecognized command

Thanks for the explanation. That makes sense.
My sample script is a bit simplistic. In reality I am using getopt and OPTARG as there are several switches that I am allowing.
So your proposed solution doesn't really work. Also the image filenames aren't entered on the command line in my full script. They are pulled from directories that are recursed into.
Is there any other way around this problem?

Offline

#5 2012-02-29 22:02:08

Xyne
Moderator/TU
Registered: 2008-08-03
Posts: 5,798
Website

Re: [solved] passing ImageMagick options in script : unrecognized command

For anything more complicated than a simple batch script, consider using one of the ImageMagick scripting APIs.

If you prefer to stick with Bash, you can construct your own arrays:

#!/bin/bash

# Create an array.
ARR=(a b c)

# Append values to the array.
ARR+=(e f g)
ARR+=("don't forget to quote when necessary")

# Pass the values of the array to a command as separate arguments.
cmd "${ARR[@]}"

# You can also assign values using array indices.
ARR[0]="alpha"

# The size of the array is given by ${#ARR[@]}.
# You can use it to append an item to the array:
ARR[${#ARR[@]}]="omega"

Last edited by Xyne (2012-02-29 22:02:53)

Offline

#6 2012-02-29 22:29:54

inkdinky
Member
Registered: 2011-12-08
Posts: 62

Re: [solved] passing ImageMagick options in script : unrecognized command

Xyne, thanks for all your help. I really appreciate it.
I'll attempt sticking with bash for now but will definitely look into the API for future use!

Offline

#7 2012-03-01 01:26:56

inkdinky
Member
Registered: 2011-12-08
Posts: 62

Re: [solved] passing ImageMagick options in script : unrecognized command

As the saying goes there is more than one way to skin a cat:
to overcome the way that the variables are passed to IM I simply created a string from the IMcommand I wanted to use and then ran that command through eval.
This worked like a charm. It allowed the desired flexibility (use of any and all IM comands) that I wanted without involving a rewrite etc.

string="mogrify ${1} -resize 800x900 ${2}"
eval "${string}"

Offline

#8 2012-03-01 03:01:35

Xyne
Moderator/TU
Registered: 2008-08-03
Posts: 5,798
Website

Re: [solved] passing ImageMagick options in script : unrecognized command

You don't even need eval if you want to do that. You can do

string="mogrify ${1} -resize 800x900 ${2}"
$string

in that case.

Just watch out for unwanted word expansion of $1. For example, if you want to include a background image on a path with a space, you would have to both quote it and escape the quotes when constructing the command string. If you don't expect to encounter such a situation, then you can ignore it.

Offline

Board footer

Powered by FluxBB