I need to dinamically compose an image using Imagemagick with a canvas as a base and some other images on top of that.
Currently, the most simple scenario just works (please notice you will need two images named src1.png
and src2.png
for this to work):
convert src1.png -gravity northwest -draw 'image Over 10,10 0,0 "src2.png"' result.png
However, I need to dinamically forge these parameters to be able to draw one, two or more images on top of src1.png
.
To do that, I tried storing the parameters in a variable, and then doing a substitution. I tried two versions of this, both with arrays and with simple strings:
DRAWOPTS=(-draw 'image Over 10,10 0,0 \"src2.png\"')
convert src1.png -gravity northwest ${DRAWOPTS[@]} result.png
DRAWOPTS="-draw 'image Over 10,10 0,0 \"src2.png\"'"
convert src1.png -gravity northwest $DRAWOPTS result.png
But I always get these errors:
convert.im6: non-conforming drawing primitive definition `image' @ error/draw.c/DrawImage/3160.
convert.im6: unable to open image `Over': No such file or directory @ error/blob.c/OpenBlob/2641.
convert.im6: no decode delegate for this image format `Over' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `10,10': No such file or directory @ error/blob.c/OpenBlob/2641.
convert.im6: no decode delegate for this image format `10,10' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `0,0': No such file or directory @ error/blob.c/OpenBlob/2641.
convert.im6: no decode delegate for this image format `0,0' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `"src2.png"'': No such file or directory @ error/blob.c/OpenBlob/2641.
convert.im6: no decode delegate for this image format `"src2.png"'' @ error/constitute.c/ReadImage/544.
convert.im6: non-conforming drawing primitive definition `image' @ error/draw.c/DrawImage/3160.
And I can't get this to work. Need some help with this quoting hell, please.
You need to quote the expansion of the array.
DRAWOPTS=( -draw 'image Over 10,10 0,0 "src2.png"' )
convert src1.png -gravity northwest "${DRAWOPTS[@]}" result.png