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?".
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Placen a text over a image

Post by Bonzo »

Everybody has to learn at some time Emilio; I am not a windows dos/bat expert either.

I can not open your files; I assume as the file extension is not recognised by my browser and I can not download them either.

Anyway the last file I uploaded needs saving as a .bat file as you have done. Then just drag and drop the image over the .bat file icon. I thought that would be more useful than hardcoding the image each time. From memory you may be able to call the code from run and include the file name something like: c://Documents/test1.bat path/to/image/image.jpg
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 »

evilaro wrote: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.
The first command that is executed is:

Code: Select all

convert -gravity Center -crop 184x113+0+0 -format "%fx:(mean<0.5)?1:0]" info:
Where is the input image? You haven't provided one. So ImageMagick tells you:

Code: Select all

no images defined
snibgo's IM pages: im.snibgo.com
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:

I did not made myself understood...

On the error code I put refers to the error I get where I did NOT
put a image, but on the second and third .bat I put images and the result
was the same.

I was not putting the image in the proper place.

Now with the Bonzo indication All works.

Thanks
-------

Bonzo:

Al last it works, thanks.
I am putting the test result.

http://www.evilfoto.eu/pagina_cuentos/v ... xx1111.jpg

Emilio

PS I have a couple of question regarding this forum.
1) I assume that if I want to prent a image or a file I have to upload to MY server... not IM server?
2) I have put the link to this image, then I Preview it and then close the image I loose the message I
was writting (pity me)... what is the procedure to retun to the message without loosing it.
Save draft as a precaution?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Placen a text over a image

Post by Bonzo »

1/ Yes or another photo storage site
2/ Not quite sure but if I am writing a long message I try remember to copy the message into the clipboard as a precaution.

I am glad the method is now working and you just need to refine the code to your exact output.
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 »

Bonzo wrote: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"
Perhaps I am missing something as I do not read Windows code that well, but I do not see how this code can generate a result such as posted by emilio.

The code only tests the average graylevel of image subsection where the text is to be placed. Then it writes either all black (000%) or all white (100%) text. I do not see any transparent text, nor a decision on a pixel-by-pixel basis.

What am I missing?
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 presume Emilio has replaced rgb(%%j00%%,%%j00%%,%%j00%%) with something like rgba(%%j00%%,%%j00%%,%%j00%%,0.5) for the transparency; apart from that the code posted should produce the image posted.

The code works now and Emilio will need to decide his next step, a different method, add an undercolor etc.
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 »

Just adding 0.5 for transparency will only fade the text some. It will not make parts transparent, since the mean is over the whole subsection. One would have to make an image that tested the color at each pixel using -fx rather than [fx:...].

I converted your code to unix with the 0.5 and put the text over a gradient and it produces all black text.

Code: Select all

convert -size 200x200 gradient: -rotate 90 grad.png
size=`convert -background none -pointsize 36 -font arial -fill white label:"Test" -format "%wx%h" info:`
j=`convert grad.png -gravity center -crop ${size}+0+0 +repage -format "%[fx:(mean<0.5)?1:0]" info:`
convert -gravity center -background none -pointsize 36 -font arial -fill "rgba($j00%,$j00%,$j00%,0.5)" label:"Test" \
grad.png -reverse -compose over -gravity center -geometry +0+0 -composite contrast_text3.jpg
Image
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Placen a text over a image

Post by Bonzo »

It looks like you have missed the a off rgb - "rgba($j00%,$j00%,$j00%,0.5)"
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 »

You are right about that, but after correcting, it only fades the text and does not make parts transparent. See corrected image above
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 »

To do what you want, you need to test every pixel in the background image subsection to compare to the mean and create a mask.

This works for me on my gradient background (unix syntax). Not sure which polarity you want, so here is both.

Code: Select all

convert -size 200x200 gradient: -rotate 90 grad.png
size=`convert -background black -pointsize 36 -font arial -fill white -gravity center label:"Test" -write text.png -format "%wx%h" info:`
convert grad.png -gravity center -crop ${size}+0+0 +repage -fx "(u<0.5)?0:1" \
text.png -compose over -compose multiply -composite mask.png
convert grad.png text.png mask.png \
\( -clone 1 -clone 2 -compose multiply -composite -alpha copy -channel a -evaluate multiply 0.5 +channel \) \
-delete 1,2 -gravity center -compose over -composite -write show: contrast_text4.jpg
Image

Code: Select all

convert -size 200x200 gradient: -rotate 90 grad.png
size=`convert -background black -pointsize 36 -font arial -fill white -gravity center label:"Test" -write text.png -format "%wx%h" info:`
convert grad.png -gravity center -crop ${size}+0+0 +repage -fx "(u<0.5)?1:0" \
text.png -compose over -compose multiply -composite mask.png
convert grad.png text.png mask.png \
\( -clone 1 -clone 2 -compose multiply -composite -alpha copy -channel a -evaluate multiply 0.5 +channel \) \
-delete 1,2 -gravity center -compose over -composite -write show: contrast_text5.jpg
Image
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 »

Perhaps the OP should post the actual code he used to create his image and the input image.

I may have missed this above, but what version of IM are you using?
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 my code to put text in the upper left corner of his output image. But this works only to make the text transparent where the image is nearly black.

Code: Select all

size=`convert -background black -pointsize 200 -font arial -fill white \
    -gravity center label:"Test" -write text.png -format "%wx%h" info:`
convert xxxx1111.jpg -gravity northwest -crop ${size}+150+10 +repage \
    -fuzz 30% -fill white +opaque black -fill black +opaque white \
    text.png -compose over -compose multiply -composite mask.png
convert xxxx1111.jpg text.png mask.png \
    \( -clone 1 -clone 2 -compose multiply -composite \
        -alpha copy -channel a -evaluate multiply 0.5 +channel \) \
   -delete 1,2 -gravity northwest -geometry +150+10 -compose over -composite \
   xxxx1111_contrast.jpg
rm -f mask.png text.png
Image

The above could be combined into two command line, but I left it as three command lines for easier interpretation.
evilaro
Posts: 27
Joined: 2015-08-11T02:17:47-07:00
Authentication code: 1151

Re: Placen a text over a image

Post by evilaro »

Hi:

I have posted the code I used as text3.bat, but since there is a problem
to read it I am putting it here as .txt, you will have to rename it.

Also I am copying the code here.

>>>>
SETLOCAL ENABLEDELAYEDEXPANSION

SET gravity=-gravity Center
SET Option=-pointsize 200 -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 foto.jpg %Option% rgb(%%j00%%,%%j00%%,%%j00%%) label:"Molinos del Quijote" %1 -reverse -compose over %gravity% -geometry +0+0 -composite "_xxxx1111.jpg"
<<<<<



Also I am puting the original image.


http://www.evilfoto.eu/pagina_cuentos/varios/text3.text

http://www.evilfoto.eu/pagina_cuentos/varios/foto.jpg

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 »

An interesting example fmw42; it looks like the text is behind the windmill.
evilaro
Posts: 27
Joined: 2015-08-11T02:17:47-07:00
Authentication code: 1151

Re: Placen a text over a image

Post by evilaro »

Hi:

Just to clarify.

The way I activated the text3.bat was by
pasting the image FOTO.JPG over the .bat ico, as Bonzo suggested.

Emilio
Post Reply