Using the Filename as Text on the 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?".
kb18951452

Using the Filename as Text on the image.

Post by kb18951452 »

So, i have been trying to get this done all day long, and i must be missing something
I am running Vista 64 (and using the CMD prompt)

I have a set of files. I need to add the name of the file to the bottom of the image. Actually i need to have a bit of white space added tot he bottom of the image, and on that white space, on the left side, a text string, and on the right side, the name of the file, minus the extension. All the images are either Tif or JPG
So if i have an image abc001.tif, when its done, i need a line of text over white to the at the base of the image, with one text label on the left (This one never changes), and on the right side "abc001" (without quotes).

So far, i have been trying this
convert abcoo1.tif -pointsize 20 -gravity southeast -label "%f" - append -pointsize 20 -gravity southwest -annotate 0 "textlabel" abc001.tif

What i am getting is, a label on the left, but no added canvas, and no filename on the right. I don't think i knwo how to use the escape characters correctly. I've been looking thru the help files,but i am lost on a few things. i can't even figure out why sometimes a label: is sued and other times its -label.......

Hoping to get a fix.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using the Filename as Text on the image.

Post by fmw42 »

In unix:

filename=`convert lena.jpg -ping -format "%t" info:`
convert lena.jpg -fill black -undercolor white \
-gravity southwest -annotate +0+0 "$filename" \
-gravity southeast -annotate +0+0 "abc001" lena_label.jpg

Image


You can adjust the offset of the text from the corners by changing +0+0

For windows users, see http://www.imagemagick.org/Usage/windows/
kb18951452

Re: Using the Filename as Text on the image.

Post by kb18951452 »

Here is my attempt:

filename=`convert lena.jpg -ping -format "%t" info:` (I have no idea how to do this first line)

convert C:\testa\GTN035-00000016.TIF -fill black -pointsize 40 -undercolor white -gravity southwest -annotate +0+0 "Protected Document - Attoyneys Eye's Only" -gravity southeast -pointsize 40 -annotate +0+0 "$filename" C:\testa\GTN035-00000016append.TIF

I don't know how to store a parameter like that used to create the "filename" in the first line.
Ohh, and one more thing, I actually need the image to get longer (as in, add a bar of white to the bottom) I can't cover anything on the image.

I am sure i am close. (using windows. sorry)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using the Filename as Text on the image.

Post by fmw42 »

see the windows reference I posted above. it has what you need. otherwise hopefully a windows expert can show you how to do that. the first line in windows is quite different from that in unix.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using the Filename as Text on the image.

Post by fmw42 »

Ohh, and one more thing, I actually need the image to get longer (as in, add a bar of white to the bottom) I can't cover anything on the image.
try the windows equivalent of this:

filename=`convert lena.jpg -ping -format "%t" info:`
width=`convert lena.jpg -ping -format "%w" info:`
height=20
convert lena.jpg \( -size ${width}x${height} xc:white \) -append \
\( -background white -size x${height} -fill black label:"$filename" \) \
-gravity southwest -compose over -composite \
\( -background white -size x${height} -fill black label:abc001 \) \
-gravity southeast -compose over -composite \
lena_label2.jpg

Image

Can some Windows expert convert this for this person? He needs the Windows equivalent. Thanks
Last edited by fmw42 on 2010-05-04T10:47:38-07:00, edited 1 time in total.
kb18951452

Re: Using the Filename as Text on the image.

Post by kb18951452 »

snibgo or el_supremo or bonzo? Help anyone?
I can't even get
convert lena.jpg -ping -format "%t" info:
to work.....

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

Re: Using the Filename as Text on the image.

Post by Bonzo »

Try this:

Code: Select all

FOR /F %%x IN ('convert %1 -ping -format "%%t" info:') DO SET name=%%x
FOR /F %%x IN ('convert %1 -ping -format "%%w" info:') DO SET width=%%x
SET height=20
convert %1 ( -size %width%x%height% xc:white ) -append ^
 ( -background white -size x%height% -fill black label:%name% ) ^
 -gravity southwest -compose over -composite ^
 ( -background white -size x%height% -fill black label:abc001 ) ^
 -gravity southeast -compose over -composite ^
 "th_%~n1.jpg"
This is a batch file - save as something like text.bat on your desktop. Drag and drop an image over it and it will be modified and saved to the same directory whith th_ added to the front of the name.
You may want to add some font size etc. depending on your original image. It could be modified to run from the command prompt but I think this is more user freindly.
kb18951452

Re: Using the Filename as Text on the image.

Post by kb18951452 »

This makes a jpg as a result. How might i have it give me the result the same name as the original? I am sure it has something to do with %~n1.jpg

nevermind, figured that out.
Now, about how to get the text to "match" the image, as in, its always the right size...
Last edited by kb18951452 on 2010-05-04T14:08:42-07:00, edited 1 time in total.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Using the Filename as Text on the image.

Post by Bonzo »

I do not use windows much and have to look back at my old tests; try changing:

Code: Select all

"th_%~n1.jpg" TO "th_%1"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using the Filename as Text on the image.

Post by fmw42 »

are you trying to use the same output name and suffix as the input or only the name or only the suffix?
kb18951452

Re: Using the Filename as Text on the image.

Post by kb18951452 »

actually, what i want to do is, change apply the label, and save it over the original file (same file name, same location).

But more than that, I want the text to always be a certain size.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using the Filename as Text on the image.

Post by fmw42 »

just make the output name the same as the input name. as long as the height of the white part is the same, all text will have the same height but will be longer or shorter due the number of characters. If you want the text to have the same length, then the height of the text will vary and may be too tall to fit or too short to read.
kb18951452

Re: Using the Filename as Text on the image.

Post by kb18951452 »

I need to apply this to 250k images, across some 180k sub directories...

This script works when i drop a file on it, but not if i drop a folder.

I tries dropping several files at a time (doing a search for the first 10 records (across 10 directories, but they are all in the search results, and drap/dropping them onto the bat file. Only the first one processed. And i doing something wrong? Maybe someone can explain or show me how to have this apply to all sub directories?
I tried changing the /F to a /R (Read it someplace that that means recursive,) but i don't think thats exactly what i need, there is priobably aother change, cause when i dropped a folder after that, it started doing all kinds of crazy stuff...

I don't know what half this stuff means, like the
%%X, and the %%t %%w, what are those? Where can i see a list?
kb18951452

Re: Using the Filename as Text on the image.

Post by kb18951452 »

I can't get this to process multiple images at once.

Code: Select all

    FOR /F %%x IN ('convert %1 -ping -format "%%t" info:') DO SET name=%%x
    FOR /F %%x IN ('convert %1 -ping -format "%%w" info:') DO SET width=%%x
    SET height=60
    convert %1 ( -size %width%x%height% xc:white ) -append ^
    ( -background white -size x%height% -fill black label:%name% ) ^
    -gravity southeast -compose over -composite ^
    ( -background white -size x%height% -fill black label:"Protected Document - Attorney Eye's Only" ) ^
    -gravity southwest -compose over -composite ^
    "%1"
Ohh, and BTW, i greatly appreciate the help on this. I had no idea so much could be done with DOS
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using the Filename as Text on the image.

Post by fmw42 »

The script from bonzo was only designed for one image at a time. It would need to be modified to loop over all images dropped on it.
Post Reply