ImageMagick white out border stripes - imagemagick

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

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.

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: 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: inter-line spacing with captions creates cutoff's

I'm using the following command to create a caption. But I want to control the interline spacing as well. When I set the value to negative (to get the lines closer to eachother), the first line gets it's capital letters cut off. I tried added a "border", but that just adds a border - doesn't add "padding". Is there a way to add "padding"?
convert templates/input.pdf -font test.ttf
-size 1245x283! -background red -fill white -gravity northwest
-interline-spacing -25
caption:"Interline-Spacing NOT WORKING\r\nwith Captions"
-geometry +129+129 -composite output/temp.png
In ImageMagick, you can set the -gravity center, then use -trim +repage, if you want to remove excess around the text and then add any amount of border back using -border or -extent if you know the desired output size.
Try this using unix syntax:
convert \( -size 2115x560 xc:skyblue \) \( -size 1245x283! -background red -fill white -gravity center -font arial -interline-spacing -25 caption:"Interline-Spacing NOT WORKING\r\nwith Captions" -trim +repage -background red -extent 1245x283! \) -gravity northwest -geometry +129+129 -composite temp.png
or this in Windows:
convert ( -size 2115x560 xc:skyblue ) ( -size 1245x283! -background red -fill white -gravity center -font arial -interline-spacing -25 caption:"Interline-Spacing NOT WORKING\r\nwith Captions" -trim +repage -background red -extent 1245x283! ) -gravity northwest -geometry +129+129 -composite temp.png
If you want the white text left justified, then use -gravity west before -extent and -gravity west before the caption:
convert \( -size 2115x560 xc:skyblue \) \( -size 1245x283! -background red -fill white -gravity west -font arial -interline-spacing -25 caption:"Interline-Spacing NOT WORKING\r\nwith Captions" -trim +repage -background red -gravity west -extent 1245x283! \) -gravity northwest -geometry +129+129 -composite temp2.png
Replace your input image in place of my -size 2115x560 xc:skyblue and your font for mine.
I would guess the interline spacing is from the base line of the text up. Lower letters will protrude below the base line. I suppose it is more of a "line height"
The obvious solution is to either increase the spacing to allow for capital letters or do not use capital letters.
A setting of -15 seems to work for what you are doing.

How to -trim this image using ImageMagick?

I understand -trim can be used to remove extra whitespace.
How can Imagemagick be used to convert:
Note: The size of the bottom border to be removed is not known.
to
Note: the default -trim flag does not work.
The output of convert -trim pre-trim.png post-trim.png is:
which is missing the borders on the left and right.
Updated Answer
You can add some strips of colour down one side to protect the other 3 sides, then trim the side you want to trim and then remove the protective strips.
magick frame.png -gravity north \
-background cyan -splice x10 \
-background magenta -splice x10 \
-rotate 90 -trim +repage \
-gravity east -chop 10x -rotate -90 result.png
Here is the intermediate image of how it looks with the protective strips prior to trimming:
Kudos to Anthony Thyssen for his excellent ImageMagick Usage pages here.
Original Answer
You can chop 68 pixels off the bottom with:
convert frame.png -gravity south -chop x68 result.png

Resources