[Solved] Add padding to a label with translucent background

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
sas
Posts: 44
Joined: 2009-11-28T16:35:46-07:00
Authentication code: 8675309

[Solved] Add padding to a label with translucent background

Post by sas »

When using a solid background color, I can add padding to a label like this:

Code: Select all

color="#0000ff"
convert -size 100x -background "$color" label:"Hello" -bordercolor "$color" -border 20x20 label.png
Image

But when using a translucent background color like...

Code: Select all

color="rgba(0,0,255,0.5)"
...then the inner area for some reason gets the background color blended in twice:
Image

Adding -background none just before the -border command doesn't help.

What's the correct way to it?

(This is with ImageMagick 7.0.3-0 Q32 x86_64 2016-09-07 on Linux.)
Last edited by sas on 2016-09-07T06:38:19-07:00, edited 1 time in total.
sas
Posts: 44
Joined: 2009-11-28T16:35:46-07:00
Authentication code: 8675309

Re: [IM7] Add padding to a label with translucent background

Post by sas »

OK, I managed to find a solution:

Code: Select all

color="rgba(0,0,255,0.5)"
convert -size 100x -background none label:"Hello" -bordercolor none -border 20x20 -background "$color" -flatten label.png
Image

However, I'd still like to know why the behavior shown in the previous post (double-blended background color in the inner rectangle) occurs.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [IM7] Add padding to a label with translucent background

Post by snibgo »

An alternative to do what you want is to insert "-compose atop" before "-border".

The confusion is that "-border" doesn't simply add a border. It makes a canvas of the total size, in the requested bordercolor, and composites the image on it. See http://www.imagemagick.org/script/comma ... php#border
snibgo's IM pages: im.snibgo.com
sas
Posts: 44
Joined: 2009-11-28T16:35:46-07:00
Authentication code: 8675309

Re: [IM7] Add padding to a label with translucent background

Post by sas »

@snibgo: With atop the result isn't quite right, either:

Code: Select all

color="rgba(0,0,255,0.5)"
convert -size 100x -background "$color" label:"Hello" -bordercolor "$color" -compose atop -border 20x20 label.png
Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [IM7] Add padding to a label with translucent background

Post by snibgo »

You are correct. I meant "-compose copy", not "-compose atop". Sorry about that.
snibgo's IM pages: im.snibgo.com
sas
Posts: 44
Joined: 2009-11-28T16:35:46-07:00
Authentication code: 8675309

Re: [IM7] Add padding to a label with translucent background

Post by sas »

With copy it works:
Image

Thanks for the help.
Post Reply