TEXTBOX with alpha background and rounded corners

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
_merlim_
Posts: 3
Joined: 2018-07-29T21:47:04-07:00
Authentication code: 1152

TEXTBOX with alpha background and rounded corners

Post 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:
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: TEXTBOX with alpha background and rounded corners

Post by Bonzo »

What version of imagemagick are you using and what command?
_merlim_
Posts: 3
Joined: 2018-07-29T21:47:04-07:00
Authentication code: 1152

Re: TEXTBOX with alpha background and rounded corners

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: TEXTBOX with alpha background and rounded corners

Post 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.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: TEXTBOX with alpha background and rounded corners

Post by GeeMack »

_merlim_ wrote: 2018-07-30T05:15:44-07:00I 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: TEXTBOX with alpha background and rounded corners

Post by fmw42 »

Nice clean approach, GeeMack!
_merlim_
Posts: 3
Joined: 2018-07-29T21:47:04-07:00
Authentication code: 1152

Re: TEXTBOX with alpha background and rounded corners

Post by _merlim_ »

Nedless to say, both solutions were magic.
Thank you!
Post Reply