Goal: to add outline around irregular (rounded counter?) shape
- ImageMagick docs, Difference Morphology Methods, https://www.imagemagick.org/Usage/morphology/#difference
I admit I do not exactly know why it works. But it works so if that is enough for you don't let your urge to fight ignorance prevent you from using it.
magick <input-file>^ ( -clone 0 -fill <color> -colorize 100 )^ ( -clone 0 -alpha extract -virtual-pixel black -morphology edgein octagon )^ -compose over -composite <output-file>
magick input\rounder-corners.png^ ( -clone 0 -fill #000000 -colorize 100 )^ ( -clone 0 -alpha extract -virtual-pixel black -morphology edgein octagon )^ -compose over -composite output\output.png
<color> can be in fact any color so be creative:
This time we create a rounded rectangle of dimensions slightly bigger than the counter itself and then we overlay the bigger rectangle with the counter.
magick input\rounder-corners.png +antialias ^ ( +clone -size %%[fx:w+6]x%%[fx:h+6] ^ canvas:transparent -delete -2 -fill black ^ -draw "roundrectangle 0,0 %%[fx:w-1],%%[fx:h-1] 18,18" ) ^ +swap -gravity center -composite output\output.png
+antialiasswitches off antialiasing; should you leave it on, the rounded corners will be softened, giving somewhat blurry look. Some of you may like, some of you (myself included) not. I prefer it without antialiasing;
input-fileand adds it to image sequence;
-size %%[fx:w+6]x%%[fx:h+6]sets the image size to be slightly (by 3 pixels per each side) bigger than cloned one;
canvas:transparentcreates new image out of thin air with transparent background;
-delete -2deletes last but one image from the sequence (this happens to be the one that we
+cloned, as per second bullet above;
-fill blacksets fill colour to black. Black will be the colour of the outline; if you wish to use another colour, that's the place to change it;
-draw "roundrectangle 0,0 %%[fx:w-1],%%[fx:h-1] 18,18"draws rectangle with rounded corners. Two things worth emphasizing:
%[fx:w], which is ImageMagick idiom resolved to image width, now refers to new image that is 6 pixels wider and 6 pixels higher. Also, the radius that we use this time (
18) is slightly larger of what was used in tutorial on rounding corners. This is because we want the outer border be of slightly larger curvature so that outline is of uniform width;
+swapswaps two images in the sequence; in our case we just swap black rounded rectangle (which should be placed on the bottom) with rounded counter (which should go on top);
-gravity centeraligns centres of the images;
-compositeoverlaps one image over another. With no method specified with
-composeImageMagick simply puts one image on another.
If we didn't factor the large curvature of the external border, the results would be somewhat less eye-pleasing: