Page 1 of 1

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

Posted: 2015-04-17T12:14:23-07:00
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.

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

Posted: 2015-04-17T12:41:12-07:00
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

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

Posted: 2015-04-17T12:48:44-07:00
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?

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

Posted: 2015-04-17T13:24:27-07:00
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

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

Posted: 2015-04-17T15:08:58-07:00
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

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

Posted: 2015-04-21T14:27:30-07:00
by Kree
Thank you guys! Sorry for the lately response. This -trim command works well, thanks again!