Page 1 of 2
refactor multiple converts to one
Posted: 2012-06-30T13:26:47-07:00
by klh6686
Hey guys, I have a situation where I am adding text to an image.
So far I can perform:
'convert -size 600x600 xc:none -font Arial -pointsize 32 -gravity northwest \
-stroke black -strokewidth 1 -annotate 0 'This is line 1' \
-background none -shadow 100x2+0+0 \
+repage -stroke none -fill #FFFFFF -annotate +4+4 'This is line 1' \
-geometry +150+150 mypic.jpg +swap -gravity northwest -composite mypic.jpg'
And I will get a nice white text with a black drop shadow.
I am wanting to add up to 5 different pieces of text (all with different properties, font, size, color etc) in different locations and I can do that currently with 5 separate convert commands but I realize this is not optimal. However, I do not know enough about imagemagick commands to be able to sort it to one convert command as all my attempts have failed so far. Can this be done with one convert command for 5 separate layers of text w/ drop shadow?
Re: refactor multiple converts to one
Posted: 2012-06-30T13:52:54-07:00
by fmw42
should be doable by just writing the shadow text first in black (and possibly blurring it), then the text slightly offset on top of that for each line of text in sequence. I don't think that you should need any composite operation. Though if you use -shadow then you will have to composite or flatten.
see
http://www.imagemagick.org/Usage/fonts/#soft_shadow
http://www.imagemagick.org/Usage/fonts/#fuzzy_shadow
The second is more what I had in mind.
Re: refactor multiple converts to one
Posted: 2012-06-30T14:08:38-07:00
by klh6686
I really like the second example below soft_shadow (the montage example) but I was having trouble applying it to an image and not just the blue background.
Re: refactor multiple converts to one
Posted: 2012-06-30T15:12:26-07:00
by fmw42
convert -size 320x100 xc:lightblue -font Candice -pointsize 72 \
-annotate +30+70 'Anthony' -blur 0x4 \
-fill white -stroke black -annotate +25+65 'Anthony' \
font_shadow_fuzzy.jpg
Try replacing -size 320x100 xc:lightblue with your image.
If that does not work, then create -size 320x100 xc:none (but use -size WxH where they are your image size). Then after you create the text on the transparent image, composite it against you image.
Re: refactor multiple converts to one
Posted: 2012-06-30T15:14:55-07:00
by fmw42
You original command should work by creating the shadowed text over and over on the same starting transparent image for each section of text (just add more similar lines of -annotate with the colors and fonts you want). Then once you have placed all your text on the transparent background just do as you did to composite that over your real image.
Re: refactor multiple converts to one
Posted: 2012-07-01T10:25:19-07:00
by klh6686
fmw42 wrote:You original command should work by creating the shadowed text over and over on the same starting transparent image for each section of text (just add more similar lines of -annotate with the colors and fonts you want). Then once you have placed all your text on the transparent background just do as you did to composite that over your real image.
Doing this would I still be able to control all the properties of each new line of text font, size, color, etc?
fmw42 wrote:convert -size 320x100 xc:lightblue -font Candice -pointsize 72 \
-annotate +30+70 'Anthony' -blur 0x4 \
-fill white -stroke black -annotate +25+65 'Anthony' \
font_shadow_fuzzy.jpg
Try replacing -size 320x100 xc:lightblue with your image.
If that does not work, then create -size 320x100 xc:none (but use -size WxH where they are your image size). Then after you create the text on the transparent image, composite it against you image.
When I try this, the blur applies to the image and makes the image useless.
Re: refactor multiple converts to one
Posted: 2012-07-01T12:56:10-07:00
by fmw42
Try this:
convert logo: \
\( -clone 0 -fill none -colorize 100% \
-gravity northwest -fill red -font Arial -pointsize 18 -annotate +50+50 "Test1" \
-gravity center -fill blue -font Candice -pointsize 32 -annotate -100+0 "Test2" \
-gravity south -fill black -font Verdana -pointsize 24 -annotate +0+50 "Test3" \) \
-compose over -composite logo_annotate.png
if on windows remove the \ from parens and end the lines with ^ rather than \
Or with shadows:
convert logo: \
\( -clone 0 -fill none -colorize 100% -gravity northwest -fill red -font Arial -pointsize 24 -annotate +56+56 "Test1" -background red -shadow 100x2+0+0 \
-gravity northwest -fill red -font Arial -pointsize 24 -annotate +50+50 "Test1" \) \
\( -clone 0 -fill none -colorize 100% -gravity center -fill blue -font Candice -pointsize 32 -annotate -94+6 "Test2" -background blue -shadow 100x2+0+0 \
-gravity center -fill blue -font Candice -pointsize 32 -annotate -100+0 "Test2" \) \
\( -clone 0 -fill none -colorize 100% -gravity south -fill black -font Verdana -pointsize 24 -annotate +6+46 "Test3" -background black -shadow 100x2+0+0 \
-gravity south -fill black -font Verdana -pointsize 24 -annotate +0+50 "Test3" \) \
-background none -flatten logo_annotate.png
Re: refactor multiple converts to one
Posted: 2012-07-01T13:36:13-07:00
by klh6686
fmw42 wrote:Try this:
convert logo: \
\( -clone 0 -fill none -colorize 100% \
-gravity northwest -fill red -font Arial -pointsize 18 -annotate +50+50 "Test1" \
-gravity center -fill blue -font Candice -pointsize 32 -annotate -100+0 "Test2" \
-gravity south -fill black -font Verdana -pointsize 24 -annotate +0+50 "Test3" \) \
-compose over -composite logo_annotate.png
if on windows remove the \ from parens and end the lines with ^ rather than \
Or with shadows:
convert logo: \
\( -clone 0 -fill none -colorize 100% -gravity northwest -fill red -font Arial -pointsize 24 -annotate +56+56 "Test1" -background red -shadow 100x2+0+0 \
-gravity northwest -fill red -font Arial -pointsize 24 -annotate +50+50 "Test1" \) \
\( -clone 0 -fill none -colorize 100% -gravity center -fill blue -font Candice -pointsize 32 -annotate -94+6 "Test2" -background blue -shadow 100x2+0+0 \
-gravity center -fill blue -font Candice -pointsize 32 -annotate -100+0 "Test2" \) \
\( -clone 0 -fill none -colorize 100% -gravity south -fill black -font Verdana -pointsize 24 -annotate +6+46 "Test3" -background black -shadow 100x2+0+0 \
-gravity south -fill black -font Verdana -pointsize 24 -annotate +0+50 "Test3" \) \
-background none -flatten logo_annotate.png
I tried the first script and i got:
The second script takes way too long (right over 2 minutes) and returns a black image.
I ran both of them as you gave them except I replaced the output image with my output image. Should I replace 'logo:' with my image i want the text on?
Re: refactor multiple converts to one
Posted: 2012-07-01T14:05:57-07:00
by fmw42
Should I replace 'logo:' with my image i want the text on?
Yes, it is the input image you want to replace and you can certainly rename the output. But my coordinates for annotate were specific for the size of the logo:, so if your image is smaller, then that could be a problem.
Test on my command exactly with logo:. Then modify it for your input image and adjust the coordinates as fits your image.
what version of IM? what platform and what output image format? Those commands work just fine for me on IM 6.7.7.10 Q16 Mac OSX Snow Leopard. And the second one took just a fraction of a second.
real 0m0.763s
user 0m0.718s
sys 0m0.032s
Re: refactor multiple converts to one
Posted: 2012-07-01T14:07:49-07:00
by klh6686
fmw42 wrote:what version of IM? what platform and what output image format? Those commands work just fine for me on IM 6.7.7.10 Q16 Mac OSX Snow Leopard. And the second one took just a fraction of a second.
real 0m0.763s
user 0m0.718s
sys 0m0.032s
EDIT: centos6.2 not 5.5 my bad. I have version 6.7.7-9 on a CentOS 6.2 server jpg output.
Re: refactor multiple converts to one
Posted: 2012-07-01T14:10:33-07:00
by fmw42
Read my edited comments above.
Re: refactor multiple converts to one
Posted: 2012-07-01T17:33:33-07:00
by klh6686
I ran this:
Code: Select all
convert R34_02.jpg \
\( -clone 0 -fill none -colorize 100% \
-gravity northwest -fill red -font Arial -pointsize 18 -annotate +50+50 "Test1" \
-gravity center -fill blue -font Candice -pointsize 32 -annotate -100+0 "Test2" \
-gravity south -fill black -font Verdana -pointsize 24 -annotate +0+50 "Test3" \) \
-compose over -composite R34_02.jpg
The output was the same as the image I show earlier.
My image is about 800 x 300px.
As a side note, when I run a script on my server, multiple processes run, is this correct or incorrect and is it why my scripts are taking so long?
see:

It's a VPS w/ 2.5GHZ dual cpu available and 2.5gb of memory. Running this script above takes approx 10 seconds.
EDIT: I suspect it might be an OpenMP issue similar to this thread:
viewtopic.php?f=1&t=14307
I'm not terribly familiar with recompiling imagemagick but I'm going to give a shot and disable openMP and hopefully install the most recent version of imageMagick.
Re: refactor multiple converts to one
Posted: 2012-07-01T17:58:43-07:00
by fmw42
Run my exact command with logo:
However, something is wrong on your system, I suspect. Perhaps OpenMP? You are not getting any transparency. And your result cuts off the bottom (3rd line of text), which it should not as it is gravity south relative.
You also need the same fonts I used or replace them with your own fonts. It would appear that you don't have the font Candice, also.
Did you get any error messages at the terminal? Were you running the command in command line mode from a terminal or using some other scripting tool or application? Try running it straight from the bash shell terminal.
Re: refactor multiple converts to one
Posted: 2012-07-01T18:19:03-07:00
by klh6686
fmw42 wrote:Run my exact command with logo:
However, something is wrong on your system, I suspect. Perhaps OpenMP? You are not getting any transparency. And your result cuts off the bottom (3rd line of text), which it should not as it is gravity south relative.
You also need the same fonts I used or replace them with your own fonts. It would appear that you don't have the font Candice, also.
Did you get any error messages at the terminal? Were you running the command in command line mode from a terminal or using some other scripting tool or application? Try running it straight from the bash shell terminal.
It was openMP, I just compiled imageMagick 6.7.8-0 disabling openMP and I can now run my 5 separate converts for 5 separate text lines, as well as full image manipulation effects in 4 seconds! Also the cpu barely budges!
EDIT: I ran your command and it happens very fast (I can barely notice it) and the image is being generated properly now:

Re: refactor multiple converts to one
Posted: 2012-07-01T18:36:29-07:00
by fmw42
Glad to hear you solved by disabling OpenMP. Note you can compile IM with it enabled and disable it for any given command line. see
viewtopic.php?f=2&t=20756&p=83407&hilit=thread#p83407