Question

Using SVG, you can define a clipping path and then apply it when drawing a path. Can the same effect be achieved using ImageMagick command-line options?

The only way I have been able to do it is to create the clipping path as a separate file and use -clip-mask to apply it. Here's a contrived example that works:

convert -size 150x150 xc:none -draw "path 'M50,50 h50 v50 -h-50 z'" clip.png
convert -size 150x150 xc: -fill 'blue' -clip-mask clip.png \ 
        -draw "path 'h150,0 l-75,75 z" image.png

What I want to do is define the clip path in the same command as the drawing, ideally somethig like this:

convert -size 150x150 xc: -fill 'blue' \
        -clip-path  "path 'M50,50 h50 v50 -h-50 z'" \
        -draw "path 'h150,0 l-75,75 z" image.png

Which doesn't work. To try and avoid using an intermediate file, I tried using a stacked image but that doesn't seem to work as a parameter (the below did not work either):

convert -size 150x150 xc: -fill 'blue' \
        -clip-mask \( -size 150x150 xc:none -draw "path 'M50,50 h50 v50 -h-50 z'" \) \
        -draw "path 'h150,0 l-75,75 z" image.png

This can be done succinctly with SVG - can the ImageMagick command-line do it?

Was it helpful?

Solution

You can create intermediate files using the mpr pseudo format like this:

convert \( -size 150x150 xc:none -draw "path 'M50,50 h50 v50 -h-50 z'" \
           -write mpr:clip \) +delete \
        -size 150x150 xc: -fill 'blue' -clip-mask mpr:clip \
        -draw "path 'h150,0 l-75,75 z" image.png

This uses -write to cause the clip image to be written immediately (otherwise it is stacked until the end of the command sequence). It's then removed from the stack with +delete (otherwise it'll also be written at the end of the command sequence). The -clip-mask then reads the pseudo-file.

The pseudo file mpr is Magick Persistent Registry and is documented here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top