How can I make border for a transparent image, and increase it's size?

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?".
Post Reply
Kree
Posts: 7
Joined: 2015-04-17T12:04:41-07:00
Authentication code: 6789

How can I make border for a transparent image, and increase it's size?

Post by Kree »

Hello!
I try to add border to a transparent png image, and I found some solutions here

But my problem is I can't increase the image size in the same amount like the border size.

Currently I use this code:

Code: Select all

convert stone.png -bordercolor none -border 15 -background red -alpha background -channel A -blur 15x15 -level 0,0% result.png
First I create a transparent border to increase the image size, and it's works fine. But when I try to add the visible border with the blur command, I can't figure out, what parameter I should use, to set a border, what will have the size like my "transparent border".

original image:
Image
my result with the code above:
Image
As you can see, the bottom corners are cutted, and the little the top too. They should be rounded.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I make border for a transparent image, and increase it's size?

Post by snibgo »

You are capping the blur radius at 15. And you are adding a border of the same size.

I'm not sure what you want, but try a zero radius (so not capped), a smaller sigma and larger border, eg:

Code: Select all

convert stone.png -bordercolor none -border 30 -background red -alpha background -channel A -blur 0x5 -level 0,0%% r.png
snibgo's IM pages: im.snibgo.com
Kree
Posts: 7
Joined: 2015-04-17T12:04:41-07:00
Authentication code: 6789

Re: How can I make border for a transparent image, and increase it's size?

Post by Kree »

It is much better, the corners aren't cutted, but now the image size a little bit too big for the image. There is some space around the borders.

How could I calculate the amount of sigma, depend on the border width?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How can I make border for a transparent image, and increase it's size?

Post by fmw42 »

IM computes the radius automatically from the sigma, but is a rather complex and perhaps iterative process. But typically, the value will be somewhere around r=2*sigma+1 to about r=3*sigma

This seems to work for me in Unix. (For a pad/border of 15, the radius would correspond to 7.5 = 2*4 - 0.5, where sigma is 4)

Code: Select all

convert stone.png -bordercolor none -border 15 -background red -alpha background -channel A -blur 0x4 -level 0,0% +channel result.png
Another way that may be more precise to your pad (but not quite as rounded as -blur 0xsigma) is one of the following in Unix syntax. See http://www.imagemagick.org/Usage/morphology/#edgeout
http://www.imagemagick.org/Usage/morphology/#kernel

Code: Select all

convert stone.png -bordercolor none -border 15 \
\( -clone 0 -alpha off -fill red -colorize 100% \) \
\( -clone 0 -alpha extract -morphology edgeout octagon:15 \) \
-compose over -composite result.png

Code: Select all

convert stone.png -bordercolor none -border 15 \
\( -clone 0 -alpha off -fill red -colorize 100% \) \
\( -clone 0 -alpha extract -morphology edgeout disk:15 \) \
-compose over -composite result.png
pipitas
Posts: 168
Joined: 2012-07-15T14:06:46-07:00
Authentication code: 15

Re: How can I make border for a transparent image, and increase it's size?

Post by pipitas »

Kree wrote:It is much better, the corners aren't cutted, but now the image size a little bit too big for the image. There is some space around the borders.
You could append a final '-trim' operator (plus a '+repage') to get rid of the extra space:

Code: Select all

convert stone.png -bordercolor none -border 30 -background red \
   -alpha background -channel A -blur 0x5 -level 0,0%% -trim +repage r.png
Kree
Posts: 7
Joined: 2015-04-17T12:04:41-07:00
Authentication code: 6789

Re: How can I make border for a transparent image, and increase it's size?

Post by Kree »

Thank you guys! Sorry for the lately response. This -trim command works well, thanks again!
Post Reply