Placen a text over a image

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?".
evilaro
Posts: 27
Joined: 2015-08-11T02:17:47-07:00
Authentication code: 1151

Placen a text over a image

Post by evilaro »

Hello:

I wonder what will be the simplest way to
add a text over a image.

Lets say I want to add «This is my cat»
on the bottom center of the image.

I really would like to make the text transparent
so it would integrate much better with the image

But since the image on that place could be of any color
if I make the text White, then sometimes I could not see it.

I remember that one way to solve this was tha the letter
woud have 2 colors, so at lest one of them
would be seen.

Maybe IM solves this issue in another way.

Also the other problema is how to place the image
on a certain possition regardless of its size.

Thanks

Emilio



I
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Placen a text over a image

Post by snibgo »

snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Placen a text over a image

Post by fmw42 »

IM does not support two color text that would automatically use say black where the image is light and white where the image is dark.

However, this could be scripted by creating both white and black text on a transparent background. Then make a mask the identifies whether the image under each letter is brighter or darker than mid gray. Then composite the two text images according to the mask. Then compose the composite text image over the image.

For positioning, you can use -gravity and -geometry when compositing. -annotate allow -gravity and has offset arguments.
evilaro
Posts: 27
Joined: 2015-08-11T02:17:47-07:00
Authentication code: 1151

Re: Placen a text over a image

Post by evilaro »

snibgo and fmw42:

I got using IM to place a text on top a images, no problem.
Still I got the problem of the color of the text on top of a similar color on the image.
And I will try to learn how to do it using fmw42 suggestion.

At the momento I have a problem of choosing colors in the -fill ( i got while, black, red, blue) is there a list
of the usable colors... I have not been able to finf it.

Thanks

Emilio
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Placen a text over a image

Post by Bonzo »

I believe the user whugemann wrote some code to set the background colour light or dark depending on the background colour. You could search his posts and see if you can find it. At a guess it was four or more years ago.

To find the colours available:

Code: Select all

convert -list color
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Placen a text over a image

Post by snibgo »

is there a list of the usable colors... I have not been able to finf it.

Code: Select all

convert -list color
For colors, I often use "#123" etc. Each digit is for one of the RGB channels. It is a hex digit, one of: 0, 1, ... 9, a, b, c, d, e, f.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Placen a text over a image

Post by fmw42 »

Here is a simple example using a gradient as background.

Code: Select all

convert -respect-parenthesis \( -size 200x200 gradient: -rotate 90 \) \
\( -clone 0 -background none -pointsize 36 -font arial -fill white label:"Testing" \
    -gravity center -compose over -composite \) \
\( -clone 0 -background none -pointsize 36 -font arial -fill black label:"Testing" \
    -gravity center -compose over -composite \) \
\( -clone 0 -threshold 50% \) \
-delete 0 -compose over -composite contrast_text.jpg
Image



Here is another with a checkerboard background.

Code: Select all

convert -respect-parenthesis \( -size 60x60 pattern:checkerboard -resize 200x200 \) \
\( -clone 0 -background none -pointsize 36 -font arial -fill white label:"Testing" \
    -gravity center -compose over -composite \) \
\( -clone 0 -background none -pointsize 36 -font arial -fill black label:"Testing" \
    -gravity center -compose over -composite \) \
\( -clone 0 -threshold 50% \) \
-delete 0 -compose over -composite contrast_text2.jpg
Image


You can also do it perhaps simpler using -annotate rather than label:, or caption: if you needed multi-line text

But you may need to blur the image under the text. As you can see in the checkerboard example, it is harder to read when each letter may be broken up into white and black parts.

Personally, I think that unless you have slowly varying background colors, you may be better off with an undercolor as background to the text and then put that over your image.
evilaro
Posts: 27
Joined: 2015-08-11T02:17:47-07:00
Authentication code: 1151

Re: Placen a text over a image

Post by evilaro »

bonzo and snibgo:

Thanks the "-list color" is what I needed.

bonzo:
Yes I found the message from whugemann
Re: Labelling in either black or White
That is the idea... but at the momento toooooo complex for me ;)

fmw42:
I reproduced your first Testing Gradient... most impresive.

"Personally, I think that unless you have slowly varying background colors, you may be better off with an undercolor as background to the text and then put that over your image"

I completelly agree with this statement, and is one of the systems I have been using. I put a rectangle, with a black background and a White text.
But.... it is not as elegant as transparent text over a image.

Still how do I make the text placed on a image to have a rectangle around it with a given color?

Thanks to all

Emilio
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Placen a text over a image

Post by Bonzo »

You do not say how you are using the code but here is whugemann's code in php.
I have also had to input an if statement in as I am having a problem getting the %%j00%% part to work

Code: Select all

$image="in.jpg";
$gravity="-gravity center";
$Option="-pointsize 100 -background \"#00000000\" -fill"; 

// Create the label and get the size for calculating the averidge background colour
$data = exec("convert $Option white label:\"Test\" -format %wx%h info:");
// Crop the background and find the averidge colour - light and the text is black otherwise the colour is white
$background = exec("convert $image $gravity -crop $data+0+0 -format \"%[fx:(mean<0.5)?1:0]\" info:");
// Write the colour onto the image with the correct colour
if ( $background == 0 ) { $text = "0,0,0";}
else $text = "256,256,256";
$cmd = " $Option rgba\($text,0.5\) label:\"Test\" $image -reverse -compose over $gravity -geometry +0+0 -composite ";
exec("convert $cmd c_image.jpg");
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Placen a text over a image

Post by fmw42 »

Still how do I make the text placed on a image to have a rectangle around it with a given color?
It depends upon what command you are using.

If label: then you must use a background color other than none (-background somecolor) and then compose the result of label: over your background image.

If -annotate, then you just add -undercolor somecolor and apply the command to your background image.

See
http://www.imagemagick.org/Usage/text
http://www.imagemagick.org/Usage/compose/#compose
http://www.imagemagick.org/Usage/layers/#convert
evilaro
Posts: 27
Joined: 2015-08-11T02:17:47-07:00
Authentication code: 1151

Re: Placen a text over a image

Post by evilaro »

bonzo:

I am using wWindows, and basically to activate the IM code I use

RUN convert.....

I do not know PHP, which looks like it is a great language but above my posibilities.

Sorry not to be able to use your code.

fmw42

I have used your code and it works.

run "convert foto.jpg -fill "#123" -stroke black -font Arial -pointsize 100 -gravity south -annotate 0 "Hola" annotate_rose.jpg"

But if I try to use your suggestion no image is create
run "convert foto.jpg -fill "#123" -stroke black -font Arial -pointsize 100 -gravity south -annotate 0 -undercolor blue "Hola" annotate_rose.jpg"

Also I have tried with label, with the same result.

I feel I am not placing the suggestions in the proper place.

Emilio
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Placen a text over a image

Post by Bonzo »

whugemann's code is a batch file; save the below and drop an image over it for the watermark.

Currently saves as a jpg and the text does not have transparency.

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION

SET gravity=-gravity Center
SET Option=-pointsize 100 -background "#00000000" -fill 

FOR /F %%i IN ('convert %Option% white label:"Test" -format %%wx%%h info:') DO ^
FOR /F %%j IN ('convert %1 %gravity% -crop %%i+0+0 -format "%%[fx:(mean<0.5)?1:0]" info:') DO ^
convert %Option% rgb(%%j00%%,%%j00%%,%%j00%%) label:"Test" %1 -reverse -compose over %gravity% -geometry +0+0 -composite "th_%~n1.jpg"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Placen a text over a image

Post by fmw42 »

try putting undercolor before -annotate

Code: Select all

convert foto.jpg -fill "#123" -stroke black -font Arial -pointsize 100 -gravity south -undercolor blue -annotate +0+0 "Hola" annotate_rose.jpg
evilaro
Posts: 27
Joined: 2015-08-11T02:17:47-07:00
Authentication code: 1151

Re: Placen a text over a image

Post by evilaro »

Bonzo:

I feel you got a very bad student ;)

But I try hard.

I have tried to activate your code which I made it as a .bat = text1.bat
I got this error.

C:\GEST03B10>FOR /F %j IN ('convert -gravity Center -crop 184x113+0+0 -format "%
[fx:(mean<0.5)?1:0]" info:') DO convert -pointsize 100 -background "#00000000" -
fill rgb(%j00%,%j00%,%j00%) label:"Test" -reverse -compose over -gravity Cente
r -geometry +0+0 -composite "th_.jpg"
convert.exe: no images defined `info:' @ error/convert.c/ConvertImageCommand/318
7.

So since the error states that no images are defined, I place one in text2.bat and 2 in text3.bat

The result is the same.
I feel the code you placed I have to modify it somehow, maybe wordrap or the DO in another line.

Sorry I am not to good at this.

Thanks for your help.
Really the fact you state that your code will make the text NOT transparent would be great.

fmw42:

Yes that is perfect, now I have 2 options to offer the users.
1) Elegant (transparent) BUT not always visible
2) Very readable BUT not elegant.

Y wish I get Bonzo's solution to offer the best of the 2 worlds ;)

Thanks both of you for the help.

Emilio
Post Reply