Imagemagick Add Border to Inner Rectangle Only - imagemagick

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.

Related

How to have padding and margin for a border when composite an image over another one?

I'm trying to composite a legend on top of a graph using ImageMagick:
magick composite -gravity southeast '.\legend.png' -bordercolor black -border 1x1 '.\graph.png' '.\final.png'
As you can see the border doesn't have any padding or margin so it's not quite nice looking. However searching in the ImageMagick – Command-line Options page I see no option for those. Is there a way to do that?
This is my desired output. Notice the padding inside the box and the margin outside the box:
In Imagemagick, set -gravity southeast for the bottom left alignment and also -geometry +X+Y for the X and Y offset from the bottom left to move it up and or to the left.
convert backgroundimage legendimage -geometry +10+10 -gravity southeast -compose over -composite resultimage
If you need to add a (10 px) border inside the borderline (assume 1 px thick) of your overlay (legend), then in Unix syntax:
convert backgroundimage \
\( legendimage -shave 1x1 \
-bordercolor white -border 10x10 \
-bordercolor black -border 1x1 \) \
-geometry +10+10 -gravity southeast \
-compose over -composite resultimage

Draw a rounded rectangle over an existing image in ImageMagick v6

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

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.

Add top and left borders with magick command

Is there a way to add 5 or any number of white/transparent pixels at the top and left borders of an image with the magick command in Linux?
Use the -splice operator. First make a solid magenta rectangle:
magick -size 100x50 xc:magenta image.png
Now splice on a yellow chunk (so you can see it) 10 wide and 20 tall:
magick image.png -background yellow -gravity northwest -splice 10x20 result.png
Change yellow to none for transparent pixels.
Change magick to convert for v6 ImageMagick.
If you just want to splice to the East side:
magick image.png -background yellow -gravity east -splice 10x east.png
If you just want to splice to the South side:
magick image.png -background yellow -gravity south -splice x10 south.png

Imagemagick - move/offset images within a tiled montage?

Consider this example (Ubuntu 18.04, ImageMagick 6.9.7-4 Q16 x86_64 20170114):
convert -size 300x100 xc:red red.png
convert -size 100x100 xc:blue blue.png
montage red.png blue.png -frame 5 -geometry '+0+0' -tile 1x redblue.png
This gives the following image:
What I'd like to do, is move the blue square on arbitrary x position "within its tile"; say align left edge of blue square to where 25% of the red rectangle width would be, or at 50% - or even align right edge of blue square with right edge of red rectangle.
I have seen that -tile-offset exists (https://imagemagick.org/script/command-line-options.php#tile-offset), and I've tried it with this example, but it doesn't look like it does anything.
How can I move an image, part of a ImageMagick montage, within its tile?
EDIT: it looks like -tile-offset can only be specified for explicit tile images (not as in -tile 1x, but as in -tile red.png), and:
Tile smaller image over a background with offsets? - ImageMagick
-tile-offset must come before the tiling. It represents a single global offset, not the spacing for the tiling.
Here's an example:
convert -size 300x100 radial-gradient:\#400-\#FAA red.png
convert -size 500x500 xc: -tile-offset +100+40 +size -tile red.png -border 5 -geometry +5+5 -draw "color 0,0 reset" out.png
then out.png is this (click for full image):
... so to clarify - I'd like to know is its possible to move an image within a tile as obtained in montage tile 1x
As suggested in the comment:
convert -background none red.png \( -size 25x xc:none blue.png +append \) -append result.png
Or with 2 different offsets:
convert -background none red.png \
\( -size 25x xc:none blue.png +append \) \
\( -size 50x xc:none blue.png +append \) \
-append result.png
Not sure what your end-goal is, but you can also do this:
convert -gravity east -background none red.png blue.png red.png blue.png -append result.png
Or this:
convert -gravity center -background none red.png blue.png red.png blue.png -append result.png
In ImageMagick 6, another way is to extend the transparent background, then composite the blue image in the center of the bottom half of the extended image.
convert -size 300x100 xc:red -background none -extent 300x200 -size 100x100 xc:blue -geometry +100+100 -composite result.png

Resources