Add shadow only to text rather than box the text is within

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
xstatic
Posts: 13
Joined: 2014-11-13T00:48:47-07:00
Authentication code: 6789

Add shadow only to text rather than box the text is within

Post by xstatic »

Can't figure out why this won't work, but I can't get it to add the shadow just to the text. It shadows the box the text is in instead:

/usr/bin/convert -size 1920x1920 xc:transparent -size 1920.00000X420.00000 -size 1328.89888x289.07865 -background red \( -font Oswald.ttf -pointsize 195 -fill black -gravity center label:"cat" \( +clone -background black -shadow 60x10+0+0 \) +swap -background none -layers merge +repage \) -gravity northwest -geometry +314.96629+56.08989 -composite -alpha on job-220-blank.png

It would seem that by adding the \( right before the -font designator, that the +clone, etc. would only affect the text and not the background that is outside of it? Would love to understand why that isn't working.

My bonus second question would be, with the red box, how can I add rounded corners to it?

Thanks!

User avatar
fmw42
Posts: 26281
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Add shadow only to text rather than box the text is within

Post by fmw42 »

-size 1920.00000X420.00000 -size 1328.89888x289.07865
Why do you have -size twice with nothing between them? Which one do you want?

Also the first -size has X rather than x between the numbers.

Your issue is that you need to make your font background transparent not red.

Here is what I have done below:

Code: Select all

1) create black text on transparent background
2) create shadow
3) merge the shadow with the text
4) create a red image
5) swap the images and overlay the shadowed text on the red image
6) create a transparent image
7) swap the images and composite the red box with the black shadowed text onto it

Code: Select all

convert \
\( -background none -font Oswald.ttf -pointsize 195 -fill black -gravity center label:"cat" \) \
\( +clone -background black -shadow 80x5+5+5 \) \
-background none -layers merge +repage \
-size 1328.89888x289.07865 xc:red \
+swap -gravity center -compose over -composite  \
-size 1920x1920 xc:transparent \
+swap -gravity northwest -geometry +314.96629+56.08989 \
-compose over -composite result.png
(It is usually a good idea to write you code with line breaks so it is more readable)

xstatic
Posts: 13
Joined: 2014-11-13T00:48:47-07:00
Authentication code: 6789

Re: Add shadow only to text rather than box the text is within

Post by xstatic »

Thank you! It works!
But, I am not entirely sure why and it seems everything is backwards from the way I was doing it previously, so if you don't mind me asking a couple of follow ups.

What is the layers merge repage doing exactly? Is that the 'creating a read image' part?

Also, in this case, what is 'compose over' doing? At first I thought maybe compose over was taking the read image and composing it over the red background, but then I see you use it in the final part as well without a corresponding "read image", so that threw me off.

Thanks so much for your help!

Craig

User avatar
fmw42
Posts: 26281
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Add shadow only to text rather than box the text is within

Post by fmw42 »

The layers merge is adding the shadow under the text with automatic placement via page offsets stored when creating the shadow (since the shadow image size is not the same as the text).

-composite used various compose methods. -compose over is telling -composite to overlay one image onto the other.

I have done the processing in the reverse order so that I do not need to use too many clones. I first create the text image and add the shadow. Then I composite it over the red image. Then I created your transparent background image and composite the red image with the text over that background image at your desired offset. Note the use of +swap to reverse the order of the images before the compositing, since they were created in the reverse order.

xstatic
Posts: 13
Joined: 2014-11-13T00:48:47-07:00
Authentication code: 6789

Re: Add shadow only to text rather than box the text is within

Post by xstatic »

Ah! I was just about to ask about the swap. Thanks for thoroughly helping me with this and helping me understand what's going on here. I think this should do it for me. Thanks so much! :):)

xstatic
Posts: 13
Joined: 2014-11-13T00:48:47-07:00
Authentication code: 6789

Re: Add shadow only to text rather than box the text is within

Post by xstatic »

My last question would be, and I can create another post if needed. In this same instance, how could I add rounded corners to the red box?

User avatar
fmw42
Posts: 26281
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Add shadow only to text rather than box the text is within

Post by fmw42 »

You do that by blurring and thresholding the alpha channel of the red box. See https://imagemagick.org/Usage/thumbnails/#soft_edges and https://imagemagick.org/Usage/thumbnails/#rounded

Code: Select all

convert \
\( -background none -font Oswald.ttf -pointsize 195 -fill black -gravity center label:"cat" \) \
\( +clone -background black -shadow 80x5+5+5 \) \
-background none -layers merge +repage \
\( -size 1328.89888x289.07865 xc:red \
-alpha set -virtual-pixel transparent -channel A \
-blur 0x15 -threshold 50% +channel \) \
+swap -gravity center -compose over -composite  \
-size 1920x1920 xc:transparent \
+swap -gravity northwest -geometry +314.96629+56.08989 \
-compose over -composite result.png

xstatic
Posts: 13
Joined: 2014-11-13T00:48:47-07:00
Authentication code: 6789

Re: Add shadow only to text rather than box the text is within

Post by xstatic »

Fantasmic. Blessings upon you and yours. :)

Post Reply