Discussion:
[GM-help] Antialias transparency
(too old to reply)
Cook Darren
2012-12-05 22:21:23 UTC
Permalink
Hello,

I have been using GraphicsMagick for a few days now and have been having a lot of fun.

But there is one thing I haven't been able to figure out. I am generating text strings into a new image file. By default, antialiasing is on, but the font when drawn antialiases to some default background color, so the letters are trimmed in a different color. What I want is semi-transparent pixels, i.e. antialiased to transparent.

Here is the command I am using:

gm convert ^
-size 2000x300 "xc:transparent" ^
-font %3 -pointsize 120 ^
-stroke #587076 -strokewidth 3 ^
-fill transparent ^
-draw "gravity Center rotate 0 text 0,0 'Sample Text'" ^
-crop 0x0 ^
-bordercolor transparent ^
-border 10x10 ^
sample_text.png

With "xc:transparent" (as shown) I get the stroke antialiased to a black background, so the pixels grow darker. With "xc:#FFFFFFFF" I get the stroke antialiased to white, so the pixels grow lighter. If I add:
-background=transparent
...it seems to make no difference.

How to simply get the antialiased pixels to be semi-transparent so they will blend with whatever background I place the image on later?

Thanks!
~Darren
Bob Friesenhahn
2012-12-07 02:53:21 UTC
Permalink
Post by Cook Darren
-background=transparent
...it seems to make no difference.
How to simply get the antialiased pixels to be semi-transparent so
they will blend with whatever background I place the image on later?
This does not seem to be currently possible in GraphicsMagick. In
order for this to work, the rendering would need to be in the opacity
channels rather than (or in addition to) the color channels.
Currently the rendering is done using only the color channels.

There is a current bug report that rendering semi-transparent text
results in distortion of the image near the text (and without any
transparency). Regardless, this would be a different issue since the
intent is still to render color pixels.

With some work (possibly requiring programming) you could render white
text onto a black image and then create an RGBA image with that black
image copied into the opacity channel.

Bob
--
Bob Friesenhahn
***@simple.dallas.tx.us, http://www.simplesystems.org/users/bfriesen/
GraphicsMagick Maintainer, http://www.GraphicsMagick.org/
Cook Darren
2012-12-10 16:53:51 UTC
Permalink
" you could render white text onto a black image and then create an RGBA image with that black image copied into the opacity channel."

That was the trick I needed! This is what I came up with:

:: Write text as white on black with antialiasing:
gm convert ^
-size 2000x300 "xc:#00000000" ^
-font %3 -pointsize 120 ^
-stroke white-strokewidth 3 ^
-fill transparent ^
-draw "gravity Center text 0,0 'Sample Text'" ^
sample_text.png
:: copy it onto itself as opacity info: black becomes fully transparent, antialiasing semi-transparent
gm composite ^
-compose CopyOpacity ^
sample_text.png ^
sample_text.png ^
sample_text.png

An alternate for the second step if you want colorized text:
gm composite ^
-compose CopyOpacity ^
sample_text.png ^
-size2000x300 "xc:#587076" ^
sample_text.png

Thanks Bob :)
~Darren
Post by Cook Darren
-background=transparent
...it seems to make no difference.
How to simply get the antialiased pixels to be semi-transparent so they will blend with whatever background I place the image on later?
This does not seem to be currently possible in GraphicsMagick. In order for this to work, the rendering would need to be in the opacity channels rather than (or in addition to) the color channels. Currently the rendering is done using only the color channels.
There is a current bug report that rendering semi-transparent text results in distortion of the image near the text (and without any transparency). Regardless, this would be a different issue since the intent is still to render color pixels.
With some work (possibly requiring programming) you could render white text onto a black image and then create an RGBA image with that black image copied into the opacity channel.
Bob
--
Bob Friesenhahn
GraphicsMagick Maintainer, http://www.GraphicsMagick.org/
Loading...