Annotate with drop shadow on a complex background.

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
mrbeatnik
Posts: 2
Joined: 2011-08-18T08:32:54-07:00
Authentication code: 8675308

Annotate with drop shadow on a complex background.

Post by mrbeatnik »

Hi folks.

Loving ImageMagick - having great fun creating some dynamic stuff with PHP.
I am, of course, stuck on something and I hope you can help.

Basically I have a background image to which I am wanting to place some text, but the text has various effects.
Normally, these are done manually in PhotoShop, so I am trying to create a similar experience from PS.

Now, although using PHP, mod_php is not supported on my server, so I have to use php-cgi: it will be command driven.

I basically want the text beveled (inner and outer), with a drop shadow.
The basic text has a stroke and a fill.
I'm having some trouble getting the shadow to work correctly - I tried to create the text and shadow seperately on a transparent background, but I need the shadow to work on the main image background (being a bit translucent if you like).

Where I am at the moment is:
Create Main text on transparent back.
Create text shadow on transparent back.
Merge shadow onto main image. <--- the shadow is not right on the main image when using -blur feature on the shadow.
Merge main text onto main image.


I'm guessing there is an easier way to do what I want to do... any suggestions?

edit -----------------------------------------------------------------------------------------------------------

I thought I would strip out the commands from the PHP for clarity.

Code: Select all

mogrify -font myfont.TTF -pointsize 24 -fill '#ffffff' -stroke '#999999' -strokewidth 0.5  -annotate 0x0-3-3 '". $text . "' -gravity center temptransfile.png
mogrify -bevel -w 10 -f inner -i hard -s 5 temptransfile.png
mogrify -font myfont.TTF -pointsize 24 -fill '#000000' -annotate -1+1 '". $text. "' -blur 0x10 -gravity center temptransfileshad.png
convert " . $passedImg . " -modulate 100,100," . $hue . " -quality 100 -density 288 tempfile.png
composite -gravity center temptransfileshad.png tempfile.png tempfile2.png
composite -gravity center temptransfile.png tempfile2.png tempfile3.png
edit -----------------------------------------------------------------------------------------------------------


Here is what I have so far:

Code: Select all

<?php
$text = "hello world"
$hue = "100"

//Copy transparent image to a temp file.
copy("images/trans_01.png","temptransfile.png");

//Add text to copy of transparent image.
$convert="/usr/bin/mogrify -font myfont.TTF -pointsize 24 -fill '#ffffff' -stroke '#999999' -strokewidth 0.5  -annotate 0x0-3-3 '". $text . "' -gravity center temptransfile.png";
exec ($convert);
print "<br>NAME MAIN<br><img src=temptransfile.png>"; //Show current output.

//BEVEL text - doing this on previous line makes text not appear for some reason.
$convert="/usr/bin/mogrify -bevel -w 10 -f inner -i hard -s 5 temptransfile.png";
exec ($convert);
print "<br>BEVEL<br><img src=temptransfile.png>"; //Show current output.

//SHADOW - Copy transparent file again, and create a shadow effect.
copy("images/trans_01.png","temptransfileshad.png");
$convert="/usr/bin/mogrify -font myfont.TTF -pointsize 24 -fill '#000000' -annotate -1+1 '". $text. "' -blur 0x10 -gravity center temptransfileshad.png";
exec ($convert);
print "<br>NAME SHADOW<br><img src=temptransfileshad.png>"; //Show current output.

//BACKGROUND - Copy master file to temporary working file, change hue of working file as needed.
copy($passedImg,"tempfile.png");
$convert="/usr/bin/convert " . $passedImg . " -modulate 100,100," . $hue . " -quality 100 -density 288 tempfile.png";
exec ($convert);
print "<br>BACK<br><img src=tempfile.png>"; //Show current output.

//MERGE SHADOW - Put shadow on background.
$convert="/usr/bin/composite -gravity center temptransfileshad.png tempfile.png tempfile2.png";
exec ($convert);

//MERGE MAIN TEXT - Put main text on the background.
$convert="/usr/bin/composite -gravity center temptransfile.png tempfile2.png tempfile3.png";
exec ($convert);
print "<br>FINAL<br><img src=tempfile3.png>";
?>
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Annotate with drop shadow on a complex background.

Post by anthony »

A few points.
  • Why use mogrify! Sure it will read and write to the same image file, though its design is for a list or sequence of multiple image files. Convert can read and write to the same image file too, you just need to specify it as both input and output, AND you can use parenthesis and cloning operations to do everything in one command!
  • You do not define your initial image you are working with. Makes it very hard to follow what your example is actually doing.
  • What is myfont? is it a thick font, or a small thin font?
  • The -bevel operator is a stupid simple operation. It can NOT bevel a random shape, only a assumed opaque rectangular image.
    Use morphology distance method to generate bevels in random shapes
    http://www.imagemagick.org/Usage/morphology/#distance
    Practical example using fonts
    http://www.imagemagick.org/Usage/fonts/#conical
  • Finally you dont seem to have any shadow effects in your example, though you do seem to be doing some hue modifications which only work on colorful images, which none of the example code produces.
A image is worth a thousand words! Perhaps an actual example of what you are trying to achieve from your input?

I suggest you look at IM examples, Basics, Complex Image Processing and Debugging
http://www.imagemagick.org/Usage/basics/#complex
and the various examples in Compound Fonts
http://www.imagemagick.org/Usage/fonts/
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mrbeatnik
Posts: 2
Joined: 2011-08-18T08:32:54-07:00
Authentication code: 8675308

Re: Annotate with drop shadow on a complex background.

Post by mrbeatnik »

Thanks for the advice - I have now found the power of parenthesis!
I think I'm pretty much ok now, but would like a little further advice (images included this time); the images I'm producing are as close as I can get, but there are some very very small differences. I can live without an exact match, but it would be nice to learn how to get them a bit better.

Basically I have a colourful background image.
I may change the hue on this image, and I also want to overlay some text (but wanted the text shadow to work well with the colour image behind).

myfont.ttf is a thick font, I guess. It's not a normal font at all.
It's actually this font (BORG9): http://www.grsites.com/archive/fonts/view/752/

I have changed bevel to -shade instead and this seems to do the job.


Here is the original image, made in Photoshop:
Image

Here is what I have with IM:
Image


As you can see, that's pretty close, but the text is not very nice - not as good as the PS version.



The following code creates the text.

Code: Select all

convert \
\( -background none -fill '#ffffff' -stroke '#A2A2A2' -strokewidth 0.8 -unsharp 0x.3 -shade 120x60 -font myfont.TTF -pointsize 24 -gravity West label:'mytext' \) \
\( -clone 0 -background '#707070' -shadow 100x2+4+4 -blur 0x10 \) \
-reverse -background none -layers merge tmp.png
And then I just have basic code to overlay one image on the other.

How can I get the text the same?
The Photoshop text has the following applied:
  • Drop Shadow
    Inner Shadow
    Bevel and Emboss
    Color Overlay
    Stroke
I guess the two issues I have are:
  • The text does not seem as "wide" (note the width of the vertical stroke on the 'T' for example)
    It seems dirty or not smooth.
I'm trying various tweaks to get this sorted, but haven't managed as yet.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Annotate with drop shadow on a complex background.

Post by anthony »

Using -shade on text that has two different colors is what is causing the differences.

-shade is designed to work on a 'hight field' which for font should be simply 'white on black', or for rounded effect a 'blurred white on black'. But basically shade is ment to be applied to a shape, not to a

There is a colored shape on transparency technique too..
http://www.imagemagick.org/Usage/transform/#shade_shape

Basically the 'stroke' outline is what is giving you the problems, and should be either added AFTER shading (as a separate stroke) or added as a color modification, again after shading.

Rather than simply drawing the border, you can also just add a border darkening step, such as done as a final step with 'Aqua' and 'Gel' advanced processing
http://www.imagemagick.org/Usage/advanced/#gel_effects

However as the 'Borg' font has a sort of very tight and narrow 'dip' in the middle a drawn stroke (perhaps separately blurred before overlaying with mask) may work better.

It is like I have said many times.
Anthony Thyssen wrote: There are lots of ways to skin a cat, and what method you use depends
on what you want that skin for, and how messy you like the results!
No offence to cats :lol:
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply