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:
-
+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; -
+cloneclonesinput-fileand 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: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 redsets 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-1horizontally and 0 toheight-1vertically);<radius, radius>sets the curvature of the corners;
-
+swapswaps 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-filetaking its place;
-
-gravity centeraligns centres of the images; -
-compose SrcInspecifies method of composing two overlaying images; -
-compositeoverlaps one image over another usingSrcInas method for filtering overlapping pixels.
Alternatively: https://www.imagemagick.org/Usage/thumbnails/#rounded