[SOLVED]convert parameters as a file

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
dna
Posts: 3
Joined: 2016-06-06T04:28:08-07:00
Authentication code: 1151

[SOLVED]convert parameters as a file

Post by dna »

Hi everyone.

I use linux sh langage.
Short Version : I want to write the parameters of the convert function in a file (or a variable), and load it when calling the convert fonction in a sh script.

Long Version :
In need (or more exactly, i want) to generate an image which is basically a series of verticals bar (think of something like a barcode).

Because reason, i want to use a loop (each bar is the same, and the number of bar depends of the size of the image that can vary).

The loop look like this.

Code: Select all

VerticalDraw(){
HeadPosition=100
while [ $HeadPosition -lt $(expr $MapVSize - 100 ) ];do
        Message="$Message -draw \"rectangle 50,$(echo $HeadPosition) $(expr $MapHSize - 50 ),$(expr $HeadPosition + 50 )\""
        HeadPosition=$(expr $HeadPosition + 100 )
done
}
and the full command is like this :

Code: Select all

DrawMessage(){
Message="-size $(echo $MapHSize)x$MapVSize xc:white"
Message="$Message -fill black"
VerticalDraw
Message="$Message $GifFileName"
}
if I do a

Code: Select all

echo "$Message"
and copy paste the result in command line (writing convert before, of course), if works fine.
exemple of echo "$Message"

Code: Select all

-size 1400x600 xc:white -fill black -draw "rectangle 50,100 1350,150" -draw "rectangle 50,200 1350,250" -draw "rectangle 50,300 1350,350" -draw "rectangle 50,400 1350,450" map.gif
But if I do in my script

Code: Select all

convert "$Message"
, i got an error (apparently, syntax error).
I tried different ways of doing it, but it fails (for one reason or another).

Is it possible to use convert this way ?
If yes, what is the correct syntax ?
Last edited by dna on 2016-06-07T02:09:53-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert parameters as a variable/file

Post by fmw42 »

Try escaping the double quotes in the text of the Message or use single quotes there
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert parameters as a variable/file

Post by snibgo »

dna wrote:convert "$Message"
Why do you have a quote at the start and end of $Message? Convert does not expect a quote in either position.
snibgo's IM pages: im.snibgo.com
dna
Posts: 3
Joined: 2016-06-06T04:28:08-07:00
Authentication code: 1151

Re: convert parameters as a variable/file

Post by dna »

Why the quote around message, well, it's an habit to ensure spaces are correctly managed in the script.
if i use quote, i get help page of convert (so syntax error i guess)
if i don't use quotes, i get

echo

Code: Select all

-size 1100x1000 xc:white -stroke black -fill Yellow -draw "rectangle 50,100 500,150"
convert

Code: Select all

convert: impossible d'ouvrir l'image `1050,800"': Aucun fichier ou dossier de ce type @ error/blob.c/OpenBlob/2588.
convert: pas de délégué pour décoder ce format d'image `1050,800"' @ error/constitute.c/ReadImage/532.
convert: Définition de primitive de dessin non conforme `rectangle' @ error/draw.c/DrawImage/3145.
convert: impossible d'ouvrir l'image `600,880': Aucun fichier ou dossier de ce type @ error/blob.c/OpenBlob/2588.
convert: pas de délégué pour décoder ce format d'image `600,880' @ error/constitute.c/ReadImage/532.
convert: impossible d'ouvrir l'image `1050,930"': Aucun fichier ou dossier de ce type @ error/blob.c/OpenBlob/2588.
convert: pas de délégué pour décoder ce format d'image `1050,930"' @ error/constitute.c/ReadImage/532.
convert: Définition de primitive de dessin non conforme `rectangle' @ error/draw.c/DrawImage/3145.
(sorry for language and encoding, basicaly : "impossible to open image, no such file or directory" and "no delegate for decoding this image format", the coordonates does not match because i put only a part of both result)

escaping the quotes in $Message is worse (i have an additional error, drawing primitive definition not correct)
echo

Code: Select all

 -stroke black -fill Yellow -draw \"rectangle 50,100 600,150\" 
convert

Code: Select all

convert: pas de délégué pour décoder ce format d'image `1250,800\"' @ error/constitute.c/ReadImage/532.
convert: Définition de primitive de dessin non conforme `\' @ error/draw.c/DrawImage/3145.
convert: impossible d'ouvrir l'image `700,880': Aucun fichier ou dossier de ce type @ error/blob.c/OpenBlob/2588.
using simple quotes is basicaly the same than using backslashes
echo

Code: Select all

 -stroke black -fill Yellow -draw 'rectangle 50,100 650,150' 
convert

Code: Select all

convert: pas de délégué pour décoder ce format d'image `750,1010' @ error/constitute.c/ReadImage/532.
convert: impossible d'ouvrir l'image `1350,1060'': Aucun fichier ou dossier de ce type @ error/blob.c/OpenBlob/2588.
convert: pas de délégué pour décoder ce format d'image `1350,1060'' @ error/constitute.c/ReadImage/532.
convert: Définition de primitive de dessin non conforme `rectangle' @ error/draw.c/DrawImage/3145.
using simple quotes in convert command :
convert '$Message' : syntax error (display the help page)

BUT.... WAIT FOR IT.... (next message)
dna
Posts: 3
Joined: 2016-06-06T04:28:08-07:00
Authentication code: 1151

Re: convert parameters as a variable/file

Post by dna »

I finally found one solution...

Code: Select all

Message="$Message -draw \"rectangle $TopLeftX,$TopLeftY $BotRightX,$BotRightY\""
[...]
echo "$Message" > file
cat file|xargs convert
May be not the prettiest solution... but it works fine enough for me

Thank you for everything
Post Reply