What is the best way to create dashed borders?

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
tdan

What is the best way to create dashed borders?

Post by tdan »

Goal: Draw a dashed border around an existing photo with known dimensions.
Problem: My method creates a dashed border which is too thin. It does not appear like a border.
Progress:
- I drew a transparent border around the image because I did not want the border drawn on the image itself.
I wanted it to act like a border or frame, hugging the image.
- I used 4 draw commands so that the pathlines were straight and would not 'jump' between edges (try it with 1 path command).


For testing try this:

Code: Select all

convert rose: -resize 300x200! –background none –border 20 –bordercolor none
-strokewidth 20 –stroke blue –fill none
-draw “stroke-dasharray 20 20 path ‘M 0,0 h 300”
-draw “stroke-dasharray 20 20 path ‘M 300,0 v 200’”
-draw “stroke-dasharray 20 20 path ‘M 300, 200 h -300’”
-draw “stroke-dasharray 20 20 path ‘M 0, 200 h -200’”

If you try this from the command line, you will see the dashed line really is not 20px wide (at least on my monitor).
Instead, it appears offset from the image ~9 pixels or so.
Why?
Is this an issue with SVG having different resolution than IM's native resolution?
tdan

Post by tdan »

Simple solution -
path defines the centerline of the stroke.
I was defining the path too far away from the image.
That's rather intuitive - somehow I missed it.

The correct drawing should be:

Code: Select all

convert rose: -resize 300x200! –background none –border 20 –bordercolor none
-strokewidth 20 –stroke blue –fill none
-draw “stroke-dasharray 20 20 path 'M 10,10 h 330' ”
-draw “stroke-dasharray 20 20 path 'M 330,10 v 230' ”
-draw “stroke-dasharray 20 20 path 'M 330, 230 h -300' ”
-draw “stroke-dasharray 20 20 path 'M 10, 230 h -200' ”
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

WARNING: you are using word meta characters in your commands which look correct by IM will not understand. EG double quotes and dashes are not what they seem!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
tdan

Post by tdan »

You are right - the code doesn't work if you copy it as is.

How do I make sure the characters are what they should be?
I'm not quite sure what a meta character is. . . is there an extra bit which modifies the character for certain programs?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: What is the best way to create dashed borders?

Post by anthony »

Replace them with the right ones and DO NOT use word to edit text files used for code.

Under linux I use charcater translations...
For example... fix quotes and dashes....

Code: Select all

tr '\021\022\023\024' \'\'\"\" <file_in |\
  tr '\221\222\223\224\226\227\213' \'\'\"\"-  > file_out
Note I do this a lot for english text stories I download, and quotes stuff up many other programs, even though they look fine on most browser setups.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply