Drawing a complex image with many (lots of) shapes

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
iwtfa
Posts: 2
Joined: 2013-08-22T11:45:17-07:00
Authentication code: 6789

Drawing a complex image with many (lots of) shapes

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

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

Post by GreenKoopa »

IM also has a PHP interface.

snibgo, is the @file feature documented somewhere? I've never seen that before.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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".
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
Last edited by fmw42 on 2013-08-22T18:40:30-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: Drawing a complex image with many (lots of) shapes

Post 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.
Last edited by fmw42 on 2013-08-22T18:45:15-07:00, edited 6 times in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
iwtfa
Posts: 2
Joined: 2013-08-22T11:45:17-07:00
Authentication code: 6789

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

Post 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... :)
Post Reply