Page 1 of 1

Drawing a complex image with many (lots of) shapes

Posted: 2013-08-22T12:49:45-07:00
by iwtfa
Hi all,

Despite reading many pages in the doc and seeing examples, it is still unclear to me how to achieve this:

I want to draw many shapes in an image created with ImageMagick through the command line, so empty from the start. I wish to create the empty image in memory, draw shapes in it and when all is done save to a file. I don't know how to pipe the commands so that they are all executed before the file is saved.

In the bash script, the commands are executed inside a loop, as the shapes are calculated from an input text file whose lines are read, parsed and then used to issue commands to imagemagick.

I could in theory pipe all the commands in a single statement, but I don't think it's an option as there are more than a hundred thousand shapes to draw.

So basically, here is the flow I wish to achieve:

* create a temporary image
* loop in shape definitions and draw each shape in the image
* save the image to disk

From what I understand I could use miff:- to save the image in memory for further manipulation, but I have yet to find a comprehensive example on how to use it. Everything that I find is always a couple of statements piped in the same command, I did not find an example of very complex drawing.

To be honest, I had this done in PHP with GD and it was quite straightforward, but GD is not very powerful to draw (it does not have antialiasing for one thing) so that's why I wanted to translate that in ImageMagick.

Thank you

Re: Drawing a complex image with many (lots of) shapes

Posted: 2013-08-22T13:48:45-07:00
by snibgo
You are probably using "-fill" with "-draw", on some initial canvas.

For small numbers of drawing primitives, you can build up a command in a string.

I use Windows, which has quite a low limit on the size of strings, so I write them to a text file. For example, the file might be stuff.txt:

Code: Select all

xc:White
-fill Read -draw "Rectangle 10,20 30,40"
-fill Line -draw "Rextangle 445,67 123,456"
The command which uses this file might be:

Code: Select all

convert -size 1000x1000 @stuff.txt out.png
An alternative would be to update an image an image within a script loop. But that would probably be much slower.

Re: Drawing a complex image with many (lots of) shapes

Posted: 2013-08-22T13:59:27-07:00
by GreenKoopa
IM also has a PHP interface.

snibgo, is the @file feature documented somewhere? I've never seen that before.

Re: Drawing a complex image with many (lots of) shapes

Posted: 2013-08-22T14:22:32-07:00
by snibgo
For "@", see http://www.imagemagick.org/script/comma ... essing.php under "Filename References". In theory, the feature is for creating a list of filenames. It can do far more, but the syntax requires it to start with something that commences an image, such as "xc:" but not "-size".

If there is a limit to how much stuff we can put in files, I've never hit it.

V7 will have a far better scripting facility, of course.

EDIT: In my example above, "-fill Read" should be "-fill Red", and "fill Line" should be "-fill Lime".

Re: Drawing a complex image with many (lots of) shapes

Posted: 2013-08-22T15:50:40-07:00
by fmw42
Note that you can put all the commands into one -draw if you want.

xc:white -draw "fill red rectangle 10,20 30,40 fill lime rectangle 445,67 123,456"

eg

convert -size 500x500 xc:white -draw "fill red rectangle 10,20 30,40 fill lime rectangle 445,67 123,456" result.gif

So you can create one variable with all the draw commands rather than having multiple draw commands.

Re: Drawing a complex image with many (lots of) shapes

Posted: 2013-08-22T15:59:59-07:00
by fmw42
GreenKoopa wrote:snibgo, is the @file feature documented somewhere? I've never seen that before.
see filename references at http://www.imagemagick.org/script/comma ... .php#input

but it also applies to using a file of arguments or settings.

Re: Drawing a complex image with many (lots of) shapes

Posted: 2013-08-22T16:12:22-07:00
by snibgo
My typing is terrible today. Sorry.

As Fred says.

Another way of using a file:

Code: Select all

convert -size 123x456 xc:White -draw "@dd.txt" out.png
... where dd.txt contains anything that would go in a single "-draw" command.

Re: Drawing a complex image with many (lots of) shapes

Posted: 2013-08-22T18:45:29-07:00
by fmw42
Here is Anthony's notes on using @textfile.txt with -draw

http://www.imagemagick.org/Usage/draw/#reading

You may also want to see the information about syntax differences at http://www.imagemagick.org/Usage/draw/#settings

Re: Drawing a complex image with many (lots of) shapes

Posted: 2013-08-26T09:04:26-07:00
by iwtfa
Thank you for your responses.

The idea to put all the commands in a separate file was a good idea, though I chose in the end to install PHP's interface for IM as per GreenKoopa's recommendation. There were a lot of other things involved in the process, notably trig calculations, that were far easier to perform in PHP (or with many other languages that have an interface for IM for that matter) than in bash. I chose the short & sweet way... :)