Goal: to round corners of a counter.
Relevant reading:
- ImageMagick docs, Duff-Porter Alpha Composition Methods, https://imagemagick.org/Usage/compose/#duff-porter
- ImageMagick docs, Primitive Draw Commands, https://imagemagick.org/Usage/draw/#primitives
- ImageMagick docs, FX special effects image operator, https://imagemagick.org/script/fx.php
The concept is to draw a square with rounded corners and logically multiply it against source counter:
magick <input-file> +antialias ^
( +clone -size %[fx:w]x%[fx:h] canvas:transparent -delete -2 -fill red -draw "roundrectangle 0,0 %[fx:w-1],%[fx:h-1] <radius,radius>" ) ^
+swap -gravity center -compose SrcIn -composite <output-file>
example
magick input\input.png +antialias ^
( +clone -size %[fx:w]x%[fx:h] canvas:transparent -delete -2 -fill red -draw "roundrectangle 0,0 %[fx:w-1],%[fx:h-1] 16,16" ) ^
+swap -gravity center -compose In -composite output\output.png
explanation:
-
+antialias
switches 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; -
+clone
clonesinput-file
and adds it to image sequence;
-
-size %%[fx:w]x%%[fx:h]
sets dimensions of a to-be-created new image to be exactly the same as theinput-file
.%[fx:w]
is ImageMagick idiom returning image width,%[fx:h]
returns image height; -
canvas:transparent
creates new image out of thin air with transparent background;
-
-delete -2
deletes last but one image from the sequence (this happens to be the one that we+clone
d, as per second bullet above;
-
-fill red
sets the foreground colour to red. In this case it is entirely unimportant as our newly created image would be, eventually, places beneath the actual counter, so the colour could not be seen anyway; -
-draw "roundrectangle 0,0 %%[fx:w-1],%%[fx:h-1] <radius,radius>"
draws rounded rectangle filling entirely the whole drawing area (from 0 towidth-1
horizontally and 0 toheight-1
vertically);<radius, radius>
sets the curvature of the corners;
-
+swap
swaps two images in the sequence; in our case we just swap the background with the foreground. Red rounded rectangle becomes first image in the queue withinput-file
taking its place;
-
-gravity center
aligns centres of the images; -
-compose SrcIn
specifies method of composing two overlaying images; -
-composite
overlaps one image over another usingSrcIn
as method for filtering overlapping pixels.
Alternatively: https://www.imagemagick.org/Usage/thumbnails/#rounded