How to -draw text and -shadow it in ImageMagick?
Asked Answered
E

3

5

I'm printing some text on an image with convert and I would like to decorate the text with a black shadow, I tried -blur or -gaussian but I cannot apply to the text, it is applied to the background image only. I need to use -draw command and not -annotate.

And this is the code I need to update for shadowing

-font "geometricslab703bt-bold-webfont.ttf" -fill White -pointsize 18 -draw "rotate -4 text 350,250 '---- mijn ideale ----'"

thanks in advance

Exultant answered 31/12, 2013 at 13:45 Comment(0)
I
6

A better, more flexible, way to work with text shadow is to render the shadow on a new layer. This method will allow you to manipulate the shadow-text, as needed, without affecting the background. Finally draw the actual text on top of the shadow after adjusting any geometric offsetting. Here's an example:

convert -size 280x100 pattern:SMALLFISHSCALES \
  \( xc:transparent -font "Menlo" -pointsize 32 -fill black -draw "rotate -4 text 20,60 'ImageMagick'" -blur 0x1 \) \
  -geometry +2+2   -composite \
  -font "Menlo" -fill white -pointsize 32 -draw "rotate -4 text 20,60 'ImageMagick'" \
  example.png

The escaped braces "\( \)" will create a new sub-image; which, will be applied to the background with -composite flag.

enter image description here

This solution is a little bit more labor intensive, but keeps all your effects isolated.

Idiosyncrasy answered 31/12, 2013 at 17:54 Comment(0)
I
3

You can use caption to draw text, and make a shadow layer with clone then merge the two layer.

convert logo: -resize 40%x40 \
    \( -size "80x40" -background none -gravity west  -fill green caption:"Caption text" \
    \( +clone -background navy -shadow 80x3+5+5  \) +swap -background none -layers merge +repage \) -composite \
    \( -size "80x40" -background none -gravity east  -fill green caption:"Caption text"  \
    \( +clone -background red -shadow 80x3+5+5  \) +swap -background none -layers merge +repage \) -composite \
    out.png

enter image description here

Inactivate answered 1/8, 2019 at 12:51 Comment(0)
A
0

@emcconville's answer have helped me with this transparency implementation:

if [ ! -f ../watermark_template.png ]; then convert -size 511x81 xc:transparent ../watermark_template.png; fi

convert ../watermark_template.png -size 511x81 \
  \( xc:transparent -font "Arial" -pointsize 20 -fill black -draw "text 40,35 '$osname $osversion Insider Preview'" -blur 0x1 \) \
  -geometry +2+2   -composite \
  -font "Arial" -fill white -pointsize 20 -draw "text 40,35 '$osname $osversion Insider Preview'" \
  ../watermark.png
#- from https://mcmap.net/q/1967312/-how-to-draw-text-and-shadow-it-in-imagemagick

convert ../watermark.png -size 511x81 \
  \( xc:transparent -font "Arial" -pointsize 20 -fill black -draw "text 40,60 'Evaluation compilation. Build $osbuild $osbuildcodename'" -blur 0x1 \) \
  -geometry +2+2   -composite \
  -font "Arial" -fill white -pointsize 20 -draw "text 40,60 'Evaluation compilation. Build $osbuild $osbuildcodename'" \
  ../watermark.png
  
convert ../watermark.png -size 511x81 \
  \( xc:transparent -font "Arial" -pointsize 16 -fill black -draw "text 180,76 'Build attempt $specialbuildattempt'" -blur 0x1 \) \
  -geometry +2+2   -composite \
  -font "Arial" -fill white -pointsize 16 -draw "text 180,76 'Build attempt $specialbuildattempt'" \
  ../watermark.png

Which (locally) results in:

enter image description here

PS: this will vary depending on how variables are declared.

You can put your own custom variables so the image generation become dynamic and can be used programmatically.

Also, this implementation doesn't require, necessarilly, having an existing image to work from.

Anear answered 16/1, 2023 at 22:45 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.