Draw a rounded rectangle over an existing image in ImageMagick v6 - imagemagick

It is straightforward to draw a filled rounded rectangle in ImageMagick v6:
convert -size 800x600 xc:transparent -fill white -stroke black -strokewidth 20 -draw "roundRectangle 50,100,600,500 32,33" output.png
But how do you draw a rounded rectangle with transparent fill over an existing image? (Only drawing the outside borders, no filling of the insides.)
Input image:
Desired output image:

Here is how to draw round rectangle on transparency in Imagemagick 6. The draw command has 3 arguments: top-left corner, bottom-right corner, corner radii.
convert -size 500x500 xc:transparent -fill transparent -stroke black -strokewidth 10 -draw "roundrectangle 100,100 399,399 30,30" roundrectangle.png
See https://imagemagick.org/Usage/draw/#primitives

Use imagemagick and differentiate between inner and outer corners:
magick input.png +write mpr:input ( +clone -alpha off -fill black -colorize 100 -fill white -draw "roundRectangle 64,71 663,504 28,28" -fill black -draw "roundRectangle 83,92 641,484 8,8" ) -alpha off -compose copy_opacity -composite mpr:input -compose over -composite output.png
Update in July 5 2022 AM:
magick input.png +write mpr:input( mpr:input -alpha off -threshold 101% -fill white -draw "roundRectangle 64,72 660,502 26,26" -fill black -draw "roundRectangle 84,92 640,482 6,6" ) -channel-fx "| gray=>alpha" mpr:input -compose over -composite output.png

Related

Image Magick - Cropping picture into circle

I have 100+ png files all 500x500, I'm trying to crop a circle in the centre of the image 400x400 and save it into a separate png, keeping the 500x500. example below
Does anyone know how to use image magick to achieve this and run through each file in the folder? - thanks
Source
Desired output
Make a 500x500 image with a 400px opaque circle in it (mask.png) (this is of course going to be same image for all your input):
Then from a source:
convert source.png mask.png -compose copy-opacity -composite result.png
yields:
You can find answers in How can I combine these commands to achieve circular crop in ImageMagick?
magick convert input.jpg -gravity center ( -size 480x480 xc:black -fill white -draw "circle 240 240 240 0" -alpha copy ) -compose copyopacity -composite output.png
magick input.jpg ( +clone -threshold 101% -fill white -draw "circle %[fx:int(w/2)],%[fx:int(h/2)] %[fx:int(w/2)],%[fx:int(h)]" ) -channel-fx "| gray=>alpha" output.png
magick input.jpg ( +clone -threshold 101% -fill white -draw "roundRectangle 0,0 %[fx:int(w)],%[fx:int(h)] 50,50" ) -channel-fx "| gray=>alpha" output.png
magick input.jpg ( +clone -threshold 101% -fill white -draw "roundRectangle %[fx:int((w-h)/2)],0 %[fx:int((h+w)/2)],%[fx:int(h)] 50,50" ) -channel-fx "| gray=>alpha" -trim output.png
You can process every (png) image in a folder using mogrify. Assuming Imagemagick 6. Just change directories to the folder and add the path to your mask.png image, which should not be in that directory.
mogrify -alpha set -draw "image copy_opacity 0,0 0,0 'mask.png'" *.png

Imagemagick Add Border to Inner Rectangle Only

I am trying to add a border to the 100x100 blue square only, without adding it to the 200x200 background red frame. How would I achieve this? I've experimented with many configurations including parentheses ().
exec("convert -size 200x200 xc:red -size 100x100 -draw rectangle -bordercolor yellow -border 2 xc:blue -geometry +5+5 -composite temp_bg.png ")
Try starting with the blue box, adding a border and then extending like this:
magick -size 100x100 xc:blue -bordercolor lime -border 10 -gravity northwest -background red -extent 400x400 -bordercolor yellow -border 10 result.png
Sorry, no time, gotta dash - add in a -page ... or -geometry ... before extent to set the position of the blue box.

How to draw a image on top of a shape

Simplified example:
I do create a cirlce like following:
magick convert -size 256x256 xc:none -fill #FFFFFF -draw "circle 128,128 256,128" +profile "icc" out.png
And I do convert a given image like following:
magick convert in.png -background none -gravity center -resize 181x181 +profile "icc" out.png
Question:
In my examples above I do have following "core" functions:
-size 256x256 xc:none -fill #FFFFFF -draw "circle 128,128 256,128" +profile "icc"
in.png -background none -gravity center -extent 181x181 +profile "icc"
How can I combine those images WITHOUT saving the first one to a temporary file? I want to create a 256x256 output image, draw the circle to this image and then draw the converted input image on top of the circle (centered).
My current solution
Create 2 different images and combine them like following:
magick convert -composite -gravity Center out1.png out2.png out.png
EDIT - FULL EXAMPLE
PS Script looks like following:
$in = ".\in.png"
$out1 = ".\tmp1.png"
$out2 = ".\tmp2.png"
$out = ".\out.png"
// 1) create circle image
magick convert -size 256x256 xc:none -fill #FFFFFF -draw "circle 128,128 256,128" +profile "icc" PNG32:$out1
// 2) convert input image
magick convert $in -background none -gravity center -resize 181x181 +profile "icc" -colorspace Gray -fill "#E91FCB" -colorize 50 PNG32:$out2
// 3) combine circle + converted image
magick convert -composite -gravity Center $out1 $out2 PNG32:$out
// 4) delete temp. images
Remove-Item $out1
Remove-Item $out2
Input Image:
Output Image (not visible, but it has the white circle as background + a transparent background otherwise):
You can use parenthesised processing like this:
magick -size 256x256 xc:none -fill white -draw "circle 128,128 256,128" \( in.png -background none -gravity center -resize 181x181 -colorspace Gray -fill "#E91FCB" -colorize 50 \) -composite result.png
On Windows, omit the \ before parentheses.

Use Imagemagick to create a bar with text at top of image and a different text bar at the bottom

Trying to create a solid 1920X1920 transparent canvas, then put a 1920X420 colored bar at the top with centered text and the same at the bottom with different centered text.
What I am doing now, when it gets created creates the 1920X1920 transparent image and puts the bar at the top with the text, but the bar at the bottom is non-existent.
convert -size 1920x1920 xc:"transparent" -size 1920x420 -font
Din-Condensed-Bold -pointsize 84 canvas:#800000 -fill black -
gravity center caption:"TEXT FOR THE BOTTOM" -gravity south
-composite -size 1920x420 -font Din-Condensed-Bold -pointsize 84
canvas:#800000 -fill black -gravity center caption:"TEXT FOR THE
TOP" -gravity north -composite newfile.png
A few things...
The main issue is that you set the background colour for a label with -background rather than canvas:.
Note that -font, -size, -pointsize and -fill are settings, as such they only need setting once and then they remain set until changed, so you don't need to keep repeating them.
Your command then becomes like this:
convert -size 1920x1920 xc:yellow \
-size 1920x420 -font "AppleChancery" -pointsize 84 -background "#800000" -fill black \
-gravity center caption:"TEXT FOR THE BOTTOM" -gravity south -composite \
-gravity center caption:"TEXT FOR THE TOP" -gravity north -composite result.png
Very Spanish, don't you think? :-)
In case you were actually looking for -undercolor:
convert -size 1920x1920 xc:yellow \
-size 1920x420 -font "AppleChancery" -pointsize 84 -background magenta -undercolor cyan -fill black \
-gravity center caption:"TEXT FOR THE BOTTOM" -gravity south -composite \
-gravity center caption:"TEXT FOR THE TOP" -gravity north -composite result.png
You say you want some "breathing space" around the edges, so I am adding a further example. I make the red and yellow boxes slightly smaller than the magenta background and position the top one inset 5 pixels from the top and 5 from the left using -geometry. Likewise, I set gravity to SouthWest and position the yellow box 2 pixels from the right edge and 25 from the bottom:
convert -size 100x100 xc:magenta -size 90x20 \
-gravity northwest xc:red -geometry +5+5 -composite \
-gravity southeast xc:yellow -geometry +2+25 -composite result.png

ImageMagick draw rectangle with special corners

Hi I would like to create a mask image with "special corners" I am calling them special because I don't really know how to call them in english here is what I would like to achieve:
what I am using now is
convert xc:black -size 300x300 -fill white -draw "roundrectangle 3,3,296,296,5,5"
but this gives me rounded corners. Thank you in advance for any suggestions.
Here's one way of doing it.
convert -size 300x300 xc:none \
-shave 10 -bordercolor black -border 10 \
-fill black -draw "polyline 0,0 30,0 0,30" \
\( +clone -flip \) -gravity north -composite \
\( +clone -flop \) -gravity south -composite -background white -flatten result.png
That says... "Draw a rectangle your full size and transparent, shave 10 pixels off all round and add a 10 pixel black border (easier than doing the maths and making a 280x280 and adding 10 on each side). Draw a triangle in the top-left. Copy the whole shape and flip it and draw it on top of the original. Copy the whole shape and flop it and draw it again on the original. Now make all the transparent areas white."
Here is another way - maybe a little easier. Draw the original square, then copy it, enlarge it by square-root(2) (i.e. 141%), thicken the borders, rotate 45 degrees and composite it onto itself. Kinda depends how your brain works!
magick -size 300x300 xc:none -shave 10 -bordercolor black -border 10 \
\( +clone -scale 142% -shave 30 -border 30 -rotate 45 \) \
-gravity center -composite -background white -flatten result.png
Here is a link to a page showing how to do what you want along with other effects https://www.imagemagick.org/Usage/thumbnails/#rounded
This is the code from the page:
convert thumbnail.gif -alpha set -compose DstOut \
\( -size 20x15 xc:none -draw "polygon 0,0 0,14 19,0" \
-write mpr:triangle +delete \) \
\( mpr:triangle \) -gravity northwest -composite \
\( mpr:triangle -flip \) -gravity southwest -composite \
\( mpr:triangle -flop \) -gravity northeast -composite \
\( mpr:triangle -rotate 180 \) -gravity southeast -composite \
corner_cutoff.png
You should check out the examples as there is another method you could use for smaller images.

Resources