Polyline with multiple gradient stops

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
Paralon
Posts: 6
Joined: 2014-07-03T03:05:05-07:00
Authentication code: 6789

Polyline with multiple gradient stops

Post by Paralon » 2014-07-03T03:52:37-07:00

Greetings, magickans!
I have never ever used IM and for now I'm reading the examples section which looks a little bit overwhelming for now :)

The problem I'm trying to solve is the following:
Having a PNG picture of a given size and an array of points data (each having XY-coordinates and a color value) build a connected path with multiple gradient stops on it.
It should look smth like this:
Image

I guess the simpliest way to do it is to draw separate lines with rotated gradient (so i'll have to compute the angle somewhere in my app). But that way lines would not be connected and there would be ugly overlaps in cases where lines connect this way: /\
Also it would be very cool to round the corners a little bit (or make some kind of joint there), but that's optional.
Is it even possible to draw? Maybe there are some parts of documentation I should concentrate my attention on?

Paralon
Posts: 6
Joined: 2014-07-03T03:05:05-07:00
Authentication code: 6789

Re: Polyline with multiple gradient stops

Post by Paralon » 2014-07-03T04:54:38-07:00

Oh, thank you very much! That should do it. Not sure how it would look when there are several points nearby though. Gona play around with it and draw path now.

snibgo
Posts: 12152
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Polyline with multiple gradient stops

Post by snibgo » 2014-07-03T05:46:39-07:00

I've just noticed than in your example result, where two lines intersect the two lines have different colours. With my scheme, they would have the same colour.

I guess the result is a visualisation of some physical process, and perhaps the colours should be different. If so, then the solution would be more complex, perhaps by creating one image per line, each with transparent background, and finally merging them.
snibgo's IM pages: im.snibgo.com

Paralon
Posts: 6
Joined: 2014-07-03T03:05:05-07:00
Authentication code: 6789

Re: Polyline with multiple gradient stops

Post by Paralon » 2014-07-03T06:04:32-07:00

This way the lines would intersect with right colors, but how do I connect this lines then?
Wouldn't the intersect like this?
Image

Also, I didn't mention, but there could be a pretty big number of points (~3000). It may come up to the situation where command line reaches it's maximum value. Or if I'll be drawing an image per line it would be around 3000 calls to convert - wouldn't it hurt performance? (not mentioning the amount of temporary files created)

Looks like I'll have to use some programm interface for that (Magick .NET in my case)

snibgo
Posts: 12152
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Polyline with multiple gradient stops

Post by snibgo » 2014-07-03T07:00:30-07:00

If command-line length becomes a problem, then:

Code: Select all

%IM%convert ^
  -size 300x300 xc: ^
  -sparse-color shepards @s.txt ^
  s.png
... where s.txt contains:

Code: Select all

20,20 Orange
20,300,Blue
150,150,Purple
20,200,Green
280,280 Khaki
... etc, to any size. Likewise for the draw.

Rounded lines will join nicely, eg:

Code: Select all

%IM%convert ^
  -size 300x300 xc:Black ^
  -strokewidth 20 ^
  -stroke White -fill None ^
  -draw "stroke-linecap round stroke-linejoin round path 'M20,20 L20,300 L150,150 L20,200 L280,280'" ^
  -draw "stroke-linecap round stroke-linejoin round path 'M280,280 L280,50'" ^
  l.png
Note the double and single quotes. If you don't use Windows, you might need to swap these, or something.
snibgo's IM pages: im.snibgo.com

snibgo
Posts: 12152
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Polyline with multiple gradient stops

Post by snibgo » 2014-07-03T08:39:56-07:00

One way of drawing each line separately, calling convert only once, with no temporary files and not much memory use:

Code: Select all

%IM%convert ^
  -size 300x300 ^
  -strokewidth 20 ^
  -stroke White -fill None ^
  @coll.txt ^
  -compose Over -composite ^
  -background Black ^
  -flatten ^
  sl2.png
... where coll.txt is:

Code: Select all

  (
    ( xc:White
      -sparse-color shepards "20,20 Orange 20,280,Blue"
    )
    (
      xc:Black
      -draw "stroke-linecap round stroke-linejoin round path 'M20,20 L20,280'"
    )
    -alpha off
    -compose CopyOpacity -composite
  )
  (
    ( xc:White
      -sparse-color shepards "20,280,Blue 150,150,Purple"
    )
    (
      xc:Black
      -draw "stroke-linecap round stroke-linejoin round path 'M20,280 L150,150'"
    )
    -alpha off
    -compose CopyOpacity -composite
  )
  -compose Over -composite
  (
    ( xc:White
      -sparse-color shepards "150,150,Purple 20,200,Green"
    )
    (
      xc:Black
      -draw "stroke-linecap round stroke-linejoin round path 'M150,150 L20,200'"
    )
    -alpha off
    -compose CopyOpacity -composite
  )
  -compose Over -composite
  (
    ( xc:White
      -sparse-color shepards "20,200,Green 280,280 Khaki"
    )
    (
      xc:Black
      -draw "stroke-linecap round stroke-linejoin round path 'M20,200 L280,280'"
    )
    -alpha off
    -compose CopyOpacity -composite
  )
In coll.txt, indentation doesn't matter, are there are no end-line markers.
snibgo's IM pages: im.snibgo.com

Paralon
Posts: 6
Joined: 2014-07-03T03:05:05-07:00
Authentication code: 6789

Re: Polyline with multiple gradient stops

Post by Paralon » 2014-07-04T08:20:25-07:00

Once again - thank you very much. It works like a charm. And I even start to understand the way IM works :D

snibgo
Posts: 12152
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Polyline with multiple gradient stops

Post by snibgo » 2014-07-04T08:37:28-07:00

I made a minor copy-paste error: the command contains "-compose Over -composite ^". This should be deleted, and the line "-compose Over -composite" added to the end of the text file. This makes no difference to the result, but might confuse you when scaling up to thousands of lines.

When generating the text file, you can merge lines together, but ensure there is white space on both sides of ( and ).

If you have success with this, please report back (ideally with a pretty picture!). There is the possibility, with thousands of lines, that a memory leak might occur. It would be gratifying to know that this works.
snibgo's IM pages: im.snibgo.com

Paralon
Posts: 6
Joined: 2014-07-03T03:05:05-07:00
Authentication code: 6789

Re: Polyline with multiple gradient stops

Post by Paralon » 2014-07-15T13:08:50-07:00

snibgo wrote:I made a minor copy-paste error: the command contains "-compose Over -composite ^". This should be deleted, and the line "-compose Over -composite" added to the end of the text file. This makes no difference to the result, but might confuse you when scaling up to thousands of lines.

When generating the text file, you can merge lines together, but ensure there is white space on both sides of ( and ).

If you have success with this, please report back (ideally with a pretty picture!). There is the possibility, with thousands of lines, that a memory leak might occur. It would be gratifying to know that this works.
I'll try to post the first presentable image ASAP. For now - it's just gray lines (I don't have module that calculates gradient values implemented yet) that look pretty dull.
And yes - rendering of ~7000 lines takes quite a while and this process requires around 650mb of memory for 640x640 images. Probably a good idea would be to filter some points to reduce their amount.

Post Reply