Circular "tiling" of an image

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
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Circular "tiling" of an image

Post by whugemann »

I would like to draw crosshairs like this
Image
and superpose them over an existing photograph. (It's still about laser speed guns / speed traps, i.e. civil application.) I generated the above illustration with ImageMagick, by

Code: Select all

convert -size 101x101 xc:white -strokewidth 1 -stroke black -fill none -draw "circle 50,50 50,56" -draw "line 50,56 50,101" -draw "line 48,75 52,75" -draw "line 46,100 54,100" temp.png
Convert temp.png -rotate 90 temp1.png
Convert -compose multiply -composite temp.png temp1.png temp2.png
Convert temp2.png -rotate 180 temp3.png
Convert -compose multiply -composite temp2.png temp3.png Crosshairs.png
Although this obviously works, I am looking for a way to spare the intermediate images. There should be a way by the use of SVG graphics or by combining the statements, storing the first result in mpr:, but I cannot seem to get the code runnning.

BTW: there seems to be a bug when I use xc:none (what I would actually like to do) and then draw black lines onto that: I get a pure black canvas as a result. If I choose any other colour (say: red) this doesn't happen.
Wolfgang Hugemann
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Circular "tiling" of an image

Post by fmw42 »

Wolfgang,

Try these. The first is white background, the second is transparent.


convert \( -size 101x101 xc:white -strokewidth 1 -stroke black -fill none \
-draw "circle 50,50 50,56" \
-draw "line 50,56 50,101" \
-draw "line 48,75 52,75" \
-draw "line 46,100 54,100" \) \
\( +clone -rotate 90 \) \
-compose multiply -composite \
\( +clone -rotate 180 \) \
-compose multiply -composite \
Crosshairs.png

convert \( -size 101x101 xc:none -strokewidth 1 -stroke black -fill none \
-draw "circle 50,50 50,56" \
-draw "line 50,56 50,101" \
-draw "line 48,75 52,75" \
-draw "line 46,100 54,100" -alpha on \) \
\( +clone -rotate 90 \) \
-compose multiply -composite \
\( +clone -rotate 180 \) \
-compose multiply -composite \
Crosshairs2.png
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Circular "tiling" of an image

Post by whugemann »

Thanks Fred,

your first script does the trick. The option '+clone' is the decisive hint!

However, 'xc:none' still produces the same undesired effect as in my approach: The result is a black canvas. The color depth differs for the two results: While 'xc:white' results in a colour depth of 8, 'xc:none' produces an image with 24 bit colour depth.
Wolfgang Hugemann
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Circular "tiling" of an image

Post by fmw42 »

whugemann wrote:Thanks Fred,

your first script does the trick. The option '+clone' is the decisive hint!

However, 'xc:none' still produces the same undesired effect as in my approach: The result is a black canvas. The color depth differs for the two results: While 'xc:white' results in a colour depth of 8, 'xc:none' produces an image with 24 bit colour depth.

Note the -alpha on in the second. That was needed to get the transparent background when using xc:none
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Circular "tiling" of an image

Post by anthony »

You could just draw it! You can make the draws all relative to the first 'move' coordinates, or use a translation setting to position absolute draw coordinates.

See IM Examples Draw, SVG Paths, also 'drawing circles'
http://www.imagemagick.org/Usage/draw/
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Circular "tiling" of an image

Post by whugemann »

fmw42 wrote:Note the -alpha on in the second. That was needed to get the transparent background when using xc:none
I noted it, and I in principle know what it is good for, but it doesn't work over here. If I use any color other than black, the transparent background works without '-alpha on'; if I use black, it doesn't work, no matter whether I switch transparency on or not.
Wolfgang Hugemann
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Circular "tiling" of an image

Post by anthony »

The -alpha on should not make any difference at that point!
You have one image, which was created with transparency, so alpha is already 'on'

Also note that multiplying the images results in the circle anti-aliasing becoming multiplied 4 times (doubled twice), effectively destroying the anti-aliasing pixels, and making it look rather horrible. As such the circle should be drawn ONCE, at the end only.

Finally there is no need for the initial set of parenthesis, though as a 'isolation' of tasks, it is acceptable practice.

So here is the refined solution....

Code: Select all

convert -size 101x101 xc:none -strokewidth 1 -stroke black -fill none \
      -draw "line 50,56 50,101" -draw "line 48,75 52,75" -draw "line 46,100 54,100" \
      \( +clone -flip \) -compose multiply -composite \
      \( +clone -transpose \) -compose multiply -composite \
     -draw "circle 50,50 50,56" crosshairs.png
Note: you can use rotates and or flips/transpose, they are effectively the same for this purpose.
See Image Warping, Simple Distortions
http://www.imagemagick.org/Usage/warping/#simple


NOTE: If you do want to draw the circle first, and retain correct anti-aliasing, then instead of multiply, -evaluate-sequence min. This has the advantage of letting you generate four images then just merging them all in one go, but you then need to generate 4 images, in memory, rather that two at the most.

It is like I often say in situations like this...
There are lots of ways to skin a cat, and what method you use depends
on what you want that skin for, and how messy you like the results!
-- Anthony Thyssen
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Circular "tiling" of an image

Post by whugemann »

anthony wrote:You could just draw it!
I have just tried that and it's even more straightforward. My Windows batch file is now:

Code: Select all

SET Line=path 'M 0,5 0,49 M -2,25 2,25 M -4,49 4,49'
imconv -size 101x101 xc:white -fill none -stroke black -draw "translate 50,50 circle 0,0 0,5 %LINE% rotate 90 %LINE% rotate 180 %LINE% rotate 270 %LINE%" Crosshairs.png
Wolfgang Hugemann
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Circular "tiling" of an image

Post by anthony »

Good use of variables :-)

The draw commands can be put into a mvg text file and then loaded using

Code: Select all

  -affine 1,0,0,1,50,50 -draw '@crosshairs.mvg'
The affine sets the center point (just a 'translate') appropriate for the image you are drawing on.
And you can draw directly on whatever image you want.
The colors can be set either before the -draw or in the MVG file.

You can then have a selection of different MVG files for different 'crosshairs'.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Circular "tiling" of an image

Post by whugemann »

Correction: Although the code

Code: Select all

SET Line=path 'M 0,5 0,49 M -2,25 2,25 M -4,49 4,49'
imconv -size 101x101 xc:white -fill none -stroke black -draw "translate 50,50 circle 0,0 0,5 %LINE% rotate 90 %LINE% rotate 180 %LINE% rotate 270 %LINE%" Crosshairs.png
gives the expected result, it's kind of wrong, as rotations are incremental. It thus rotates the object by
  • 90°
  • 90° + 180° = 270°
  • 90° + 180° + 270° = 180°
Hence, the code can be put even simpler

Code: Select all

SET Line=path 'M 0,5 0,49 M -2,25 2,25 M -4,49 4,49' rotate 90
imconv -size 101x101 xc:white -fill none -stroke black -draw "translate 50,50 circle 0,0 0,5 %LINE% %LINE% %LINE% %LINE%" Crosshairs.png
(The superfluous "rotate 90" at the end of the draw command does not produce any error.)
Wolfgang Hugemann
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Circular "tiling" of an image

Post by anthony »

whugemann wrote:(The superfluous "rotate 90" at the end of the draw command does not produce any error.)
It also does not take much time. less than most drawing operations. ;-)
All it does is generate a new rotation matrix which is multiplied with the current affine drawing matrix.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply