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

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

Related

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.

ImageMagick: How to create torn page effect for specifc edges?

The ImageMagick documentation provides guidance on how to create torn page effects (https://www.imagemagick.org/Usage/thumbnails/#torn). However, in their implementation, all edges are torn. Suppose I wish to tear off only the bottom or top part of the image. How can I achieve such a thing using ImageMagick?
Simply grow the top & sides by using -extent operator.
convert zelda.png -background pink -extent 148x138-10-10 extent.png
(Adding pink background for visibility on stack)
Apply the effect from the Usage documentation.
convert extent.png \( +clone -alpha extract -virtual-pixel black \
-spread 10 -blur 0x3 -threshold 50% -spread 1 -blur 0x.7 \) \
-alpha off -compose Copy_Opacity -composite torn.png
Then crop back to original image size.
convert torn.png -crop 128x129+10+10 output.png
Update
If you do not want to use geometry, you can use a combination of -border, -shave & -chop.
convert zelda.png -bordercolor pink -border 10x10 -gravity South -chop 0x10 extent.png
convert extent.png \( +clone -alpha extract -virtual-pixel black \
-spread 10 -blur 0x3 -threshold 50% -spread 1 -blur 0x.7 \) \
-alpha off -compose Copy_Opacity -composite torn.png
convert torn.png -shave 10x -chop 0x10 output.png
.. And of course, this all can be done with one command.
convert zelda.png -bordercolor pink -border 10x10 -gravity South -chop 0x10 \
\( +clone -alpha extract -virtual-pixel black -spread 10 -blur 0x3 -threshold 50% \
-spread 1 -blur 0x.7 \) -gravity Forget -alpha off -compose Copy_Opacity -composite \
-shave 10x -chop 0x10 output.png
There are several ways to create a torn edge effect using ImageMagick. Here is another example command using IM version 6 and *nix syntax. This should apply a torn effect to just the top edge of any input image while keeping the original dimensions of the image.
convert input.png -alpha set -background black -fill white \
\( +clone -colorize 100 -gravity south -chop 0x6 -splice 0x6 \
-spread 6 -paint 2 +transparent white -blur 0x0.5 \) \
-background none -compose dstin -composite torn.png
That creates a white mask inside the parentheses. Then a small amount of the "torn" edge is chopped off and a black strip is spliced on to replace it. The random-ish torn edge is created using "-spread" and "-paint" between the white and black areas of the mask. After that, outside the parentheses, that mask is used to apply the transparent torn area to the input image.
To apply the effect to the bottom edge, just change the "-gravity north" to "-gravity south".
To make the torn edge on the left or right, change the gravity setting to "west" or "east", and change the values of the "-chop" and "-splice" operations from "0x6" to "6x0".
This should work the same way using ImageMagick version 7 by changing the "convert" command to "magick".
To use it in Windows, remove the backslashes that escape the parentheses from "\(...\)" to "(...)", and change the continued line backslashes "\" to carets "^".

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.

ImageMagick white out border stripes

I am trying to white out borders of an image. That is, white out 100 px vertical stripe from left, and similarly from right, top, bottom. The following works for left:
mogrify -crop +100+0 -background white -gravity west -splice 100x aaa.tif
But I cannot figure out how to do the same with other sides. I tried many geometries, east, west, this, that, no success. Also please let me know if there is a better alternative than the above command.
Start with a rose:
I'll do the borders with yellow and magenta so you can see what I am doing on StackOverflow's white background.
All Sides
Shave 10px off all sides and then put 10px back on all sides:
convert rose: -shave 10x10 -bordercolor magenta -border 10 result.png
Right Side
convert rose: -gravity east -chop 10x -background yellow -splice 10x result.png
Left Side
convert rose: -gravity west -chop 10x -background yellow -splice 10x result.png
Top
convert rose: -gravity north -chop x10 -background yellow -splice x10 result.png
Bottom
convert rose: -gravity south -chop x10 -background yellow -splice x10 result.png
Left and Right
convert rose: -shave 10x -bordercolor magenta -border 10x result.png
Top and Bottom
convert rose: -shave x10 -bordercolor magenta -border x10 result.png
Tags: ImageMagick, border, bordering, inside, gravity, one side, multiple sides, edges, framing, frame, overpaint, white-out
If you want the equivalent of Photoshop's "Border Outside" just omit the -shave or -chop.
Might be worth exploring -extent option, but I feel it could be quicker just to append padding.
For example...(using blue for visual example)
convert -background blue \
-size 100x xc:blue \
\( rose: -crop +50+0 \) \
-size 100x xc:blue \
+append \
output.png

Create an image with text over filling space

I want to create a flare image using imagemagick and then add text on top of it. I was thinking that it was going to be quite easy, but I am having issues adding the text on top of the generated image.
This is so far what I have (keep in mind that the text as well as the dimension are dynamic):
FLARE:
\(
-fill transparent -size 300x1 xc: +noise Random -channel G -separate +channel \
-scale 300x300\! \
\( -size 300x300 gradient: -evaluate cos .5 \) \
-compose hardlight -composite \
-virtual-pixel HorizontalTileEdge -distort Polar -1
\)
TEXT
-fill red \
-gravity center \
-font Arial \
annotate:"Logo 12345678"
EDIT:
The text is showing, but I can't figure out how to overimpress the text on top of the image in a way that it will fill the image for the full width (from left to right, with some padding if possible)
Figured out:
convert \
\( -background transparent -size 300x1 xc: +noise Random -channel G -separate +channel -scale 300x300\! \( -size 300x300 gradient: -evaluate cos .5 \) -compose hardlight -composite -virtual-pixel HorizontalTileEdge -distort Polar -1 \) \
\( -background transparent -size 300x300 -fill blue -gravity center -font Arial caption:"Logo \\n13474899" \) \
-gravity center -composite /tmp/flare_2_final.png
Now I need to figure out how to change the background color of the flare from black to gray and I am done.

Resources