convert command gives error in bash script

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
manit
Posts: 123
Joined: 2009-01-30T22:31:26-07:00

convert command gives error in bash script

Post by manit »

I am using

$ convert --version
Version: ImageMagick 6.8.9-9 Q16 x86_64 2017-07-31 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib
in 64 bit ubuntu OS.

Here is my script
[script]
#!/bin/bash
img_file=$2
while read one_line; do
let "first_num = $(echo $one_line|cut -d ' ' -f1)"
let "second_num = $(echo $one_line|cut -d ' ' -f2)"
echo first num-$first_num
echo second num-$second_num
let "periphery = $((second_num+5))"
echo periphery-$periphery
convert /home/lxuser/Downloads/xyt/$img_file -fill none -strokewidth 1 -draw \'"circle $first_num,$second_num $first_num,$periphery"\' \'"/home/lxuser/Downloads/xyt/$img_file"\'
echo convert /home/lxuser/Downloads/xyt/$img_file -fill none -strokewidth 1 -draw \'"circle $first_num,$second_num $first_num,$periphery"\' \'"/home/lxuser/Downloads/xyt/$img_file"\'
done < $1
[/script]

I pass it two files
(1)sth.xyt which is of the form
13 146 202 6
23 251 22 15
26 51 180 17
30 224 34 15
33 263 202 34
35 455 225 7
46 176 202 67
58 109 191 33
61 151 202 33
63 47 180 35
(2)<image_file>

The script takes first two numbers and draws circle of radius 5 on the image_file passed as second argument .
Strangely, I get error (in red)
first num-13
second num-146
periphery-151
convert: non-conforming drawing primitive definition `circle 13,146 13,151' @ error/draw.c/DrawImage/3184.
convert /home/lxuser/Downloads/xyt/don.jpg -fill none -strokewidth 1 -draw 'circle 13,146 13,151' '/home/lxuser/Downloads/xyt/don.jpg'

If I simply copy paste the echoed command . it works.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert command gives error in bash script

Post by snibgo »

Your script has the -draw argument in both double-quotes and single-quotes. Why? This will cause problems because bash will remove only the outer quotes.
snibgo's IM pages: im.snibgo.com
manit
Posts: 123
Joined: 2009-01-30T22:31:26-07:00

Re: convert command gives error in bash script

Post by manit »

i had to use double quotes because I am using $first_num .
I will put just that variable in double quotes and see what happens.
manit
Posts: 123
Joined: 2009-01-30T22:31:26-07:00

Re: convert command gives error in bash script

Post by manit »

Now my script is

#!/bin/bash
img_file=$2
while read one_line; do
let "first_num = $(echo $one_line|cut -d ' ' -f1)"
let "second_num = $(echo $one_line|cut -d ' ' -f2)"
echo first num-$first_num
echo second num-$second_num
let "periphery = $((second_num+5))"
echo periphery-$periphery
convert /home/lxuser/Downloads/"$img_file" -fill none -strokewidth 1 -draw \'circle "$first_num","$second_num" "$first_num","$periphery"\' /home/lxuser/Downloads/"$img_file"
echo convert /home/lxuser/Downloads/"$img_file" -fill none -strokewidth 1 -draw \'circle "$first_num","$second_num" "$first_num","$periphery"\' /home/lxuser/Downloads/"$img_file"
done < $1


I get
first num-13
second num-146
periphery-151
convert: non-conforming drawing primitive definition `circle' @ error/draw.c/DrawImage/3184.
convert: unable to open image `13,146': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: unable to open image `13,146': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `13,151'': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: unable to open image `13,151'': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: non-conforming drawing primitive definition `circle' @ error/draw.c/DrawImage/3184.
convert /home/lxuser/Downloads/sth.png -fill none -strokewidth 1 -draw 'circle 13,146 13,151' /home/lxuser/Downloads/sth.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert command gives error in bash script

Post by snibgo »

All those quotes confuse me. Perhaps they also confuse bash and IM. This works for me, using Cygwin bash:

Code: Select all

 convert "$img_file" -fill None -strokewidth 1 -draw "circle $first_num,$second_num,$first_num,$periphery" "$img_file"
snibgo's IM pages: im.snibgo.com
manit
Posts: 123
Joined: 2009-01-30T22:31:26-07:00

Re: convert command gives error in bash script

Post by manit »

sorry for late reply.
Thanks a lot, snibgo.

It works.
Here is the working code
#!/bin/bash
img_file=$2
while read one_line; do
let "first_num = $(echo $one_line|cut -d ' ' -f1)"
let "second_num = $(echo $one_line|cut -d ' ' -f2)"
echo first num-$first_num
echo second num-$second_num
let "periphery = $((second_num+5))"
echo periphery-$periphery
echo convert "$img_file" -fill none -strokewidth 1 -stroke red -draw "circle $first_num,$second_num $first_num,$periphery" "$img_file"
convert "$img_file" -fill none -strokewidth 1 -stroke red -draw "circle $first_num,$second_num $first_num,$periphery" "$img_file"
done < $1
Post Reply