Page 1 of 1

TEXTBOX with alpha background and rounded corners

Posted: 2018-07-29T21:50:57-07:00
by _merlim_
Hello magicians,
I am trying to generate text with alpha bg ('#00000080') and then make it rounded.
But all of the examples from https://www.imagemagick.org/Usage/thumbnails/#rounded messes up with the alpha chanell and the result does not looks nice.
Any ideas to achieve this ? :shock:

Re: TEXTBOX with alpha background and rounded corners

Posted: 2018-07-30T00:00:54-07:00
by Bonzo
What version of imagemagick are you using and what command?

Re: TEXTBOX with alpha background and rounded corners

Posted: 2018-07-30T05:15:44-07:00
by _merlim_
Version: ImageMagick 7.0.8-7 Q16 x86_64 2018-07-24

I generate the text with:
convert -size 800x -background '#00000080' -fill white -pointsize 24 pango:@./text.txt text.png

And then I have tried to make it rounded with all of the examples from: https://www.imagemagick.org/Usage/thumbnails/#rounded

For example:
convert text.png \
\( +clone -alpha extract \
-draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0' \
\( +clone -flip \) -compose Multiply -composite \
\( +clone -flop \) -compose Multiply -composite \
\) -alpha off -compose CopyOpacity -composite rounded_corners.png

Re: TEXTBOX with alpha background and rounded corners

Posted: 2018-07-30T09:27:11-07:00
by fmw42
Post your text.png image, so we can test your rounded corner command. Note that ImageMagick 7 uses magick in place of convert. If your text image has transparency, then you likely need to save the alpha channel and then after making the rounded corners, combine it with the new alpha channel in the rounded_corners.png. The alpha off in your command is turning the transparency in your text.png image off and then adding only transparency for the rounded corners.

Try this:

Code: Select all

convert -size 300x300 -background "#00000080" -font ubuntu -fill white -gravity center caption:"This is the time for all good men to come to the aid of their country" text.png

convert text.png \
\( +clone -alpha extract -write mpr:alpha +delete \) \
\( +clone -alpha opaque -alpha extract \
-draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0' \
\( +clone -flip \) -compose Multiply -composite \
\( +clone -flop \) -compose Multiply -composite \
mpr:alpha -compose multiply -composite \
\) -alpha off -compose CopyOpacity -composite rounded_corners.png

If that works, then replace my text.png with yours.

Re: TEXTBOX with alpha background and rounded corners

Posted: 2018-07-30T12:10:13-07:00
by GeeMack
_merlim_ wrote:
2018-07-30T05:15:44-07:00
I generate the text with: [...]
And then I have tried to make it rounded with all of the examples from: https://www.imagemagick.org/Usage/thumbnails/#rounded
I don't know if you're locked into that particular sequence, but if not, I'd consider taking a somewhat different approach. With a single command you can create the text caption on a transparent background, create a rounded-corner, semi-transparent background image, and composite the text over that background. A command would look something like this...

Code: Select all

convert -size 300x300 -background none -gravity center \
   xc:none -fill '#00000080' -draw 'roundrectangle 0,0 299,299 15,15' \
   -fill white caption:"This is the time for all good men to come to the aid of their country" \
   -composite rounded_corners.png

Re: TEXTBOX with alpha background and rounded corners

Posted: 2018-07-30T12:25:52-07:00
by fmw42
Nice clean approach, GeeMack!

Re: TEXTBOX with alpha background and rounded corners

Posted: 2018-08-01T14:08:41-07:00
by _merlim_
Nedless to say, both solutions were magic.
Thank you!