Set 4 borders of different widths

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Locked
User avatar
whugemann
Posts: 313
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Set 4 borders of different widths

Post by whugemann »

How can I create four borders of different widths?
I would have expected this to be something along the lines of

Code: Select all

magick in.jpg -border 20x50+10+20 out.jpg
but the usual geometry nomenclature doesn't seem to work over here. And the alternative -frame seems to use the geometry settings for the 3D design. Is there a simply one-line to achieve this?
Wolfgang Hugemann

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Set 4 borders of different widths

Post by fmw42 »

You can only do the same size top/bottom and a different size left/right with -border. If you want 4 different borders, then you will need to use -splice 4 different times. See https://imagemagick.org/Usage/crop/#splice.

Or you can use -extent and compute the WxH+X+Y that gives you the desired border size on each side. See https://imagemagick.org/Usage/crop/#extent. It does not mention the offsets +X+Y but you can use them.

User avatar
whugemann
Posts: 313
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Set 4 borders of different widths

Post by whugemann »

I couldn't verify the secret -extent options you mentioned, but anyway -splice is probably the more direct approach. Thus the code is something like

Code: Select all

magick -size 400x300 xc:yellow -background gray -splice 50x100 -gravity southeast -splice 20x200  test.jpg
and you have to use -splice only twice.
Wolfgang Hugemann

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Set 4 borders of different widths

Post by snibgo »

This can be done in a single operation, using "-extent" with %[fx:] parameters. For example, a border of (10,20,30,40) at (left,top,right,bottom):

Code: Select all

magick rose: -background Blue -extent %[fx:w+10+30]x%[fx:h+20+40]-10-20 x.png
snibgo's IM pages: im.snibgo.com

User avatar
whugemann
Posts: 313
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Set 4 borders of different widths

Post by whugemann »

OK, I see: The offsets must be negative, then they do the trick! Your code is possibly somewhat more transparent than that for -splice.
Wolfgang Hugemann

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Set 4 borders of different widths

Post by snibgo »

The offsets are the position of the extent top-left in relation to the input image top-left, not the position of the image in relation to the extent. When creating a border, we usually want the extent to start above and to the left of the input image.
snibgo's IM pages: im.snibgo.com

Locked