如何使GraphicsMagick生成的ALPHA水印具备75%透明度?
Question
I'm trying to add a large, semi-transparent "ALPHA" watermark to an image using GraphicsMagick. Here's the code I've put together so far:
gm convert \ -background transparent \ -fill '#5968c3' \ -font Arial \ -size 140x80 \ -pointsize 190 \ -gravity center \ -draw "text 0,0 ALPHA" \ -matte \ -pointsize 16 \ -gravity southeast \ -draw "text 10,10 \"©2018 Hunter Ltd\"" \ input.png \ output.png
Right now the watermark is solid, no transparency at all. How can I tweak this to make the "ALPHA" text 75% transparent?
Answer
Hey, nice start with your code! Getting that semi-transparent watermark is straightforward once you know that GraphicsMagick lets you include transparency directly in your color definition. Here's how to fix it:
The core fix: Add alpha to your fill color
Instead of using a plain hex color like #5968c3, you need to add an alpha component. There are two easy ways to do this:
Hex RGBA format: Append two hex digits to your color code to set opacity. Since you want 75% transparency, that means the text should be 25% opaque. Hex values for alpha range from
00(fully transparent) toFF(fully solid). 25% of 255 (the max value) is ~64, which translates to40in hex. So your fill color becomes#5968c340.RGBA function syntax: Use
rgba(r,g,b,alpha)where alpha is a decimal between 0 (transparent) and 1 (solid). For 75% transparency, alpha is0.25(since 1 - 0.75 = 0.25 opacity). Your fill color here would bergba(89,104,195,0.25)(those numbers are the RGB values of#5968c3converted to decimal).
Updated code example
Here's your full code with the hex RGBA fix applied to the "ALPHA" watermark:
gm convert \ -background transparent \ -fill '#5968c340' \ # Added 40 for 25% opacity (75% transparency) -font Arial \ -size 140x80 \ -pointsize 190 \ -gravity center \ -draw "text 0,0 ALPHA" \ -matte \ -pointsize 16 \ -gravity southeast \ -draw "text 10,10 \"©2018 Hunter Ltd\"" \ input.png \ output.png
If you prefer the RGBA syntax, just swap the -fill line with:
-fill 'rgba(89,104,195,0.25)' \
Quick tip
If you ever want to adjust the transparency level later, just remember:
- Solid (100% opacity) =
FF(hex) or1.0(RGBA) - 50% transparent =
80(hex) or0.5(RGBA) - Fully transparent =
00(hex) or0.0(RGBA)
And if you want the copyright text to be transparent too, just apply the same alpha trick to its -fill value (though it looks like you probably want that one solid, so you can leave it as is).
内容的提问来源于stack exchange,提问作者Sardathrion - against SE abuse

