Adding filename to 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?".
Post Reply
Flaver-D
Posts: 19
Joined: 2015-05-21T12:27:39-07:00
Authentication code: 6789

Adding filename to image

Post by Flaver-D »

Hi!
I'm working on a project and I'm really stuck on this one.
I have a bunch of Jpegs on which I would like to write the file name (minus the full path and extension) on the bottom left of the image (leaving a small gap so that it doesn't get cut out by the printer).
To be more precise, I have many photographers who sent me some pictures. The images names are in the (Author - Title.jpg ) format.
What I want is to write the filename on the bottom left of the image, but I have not the slightest idea of how I'd do this so far... I'm thinking about some complex batch script to get the effect I want (like getting all filenames and looping through them), but even them I'm not sure if there isn't an easier way to do it as I'm not very at ease using imagemagick (at least not so far). All I know is that it's the best tool for the job.

I'm using a windows batch file to do this. The main command I'm using so far is
convert *.jpg -verbose -filter cubic -resize 3300x3300 -gravity center -extent 2550x3300 -negate -gaussian-blur 20x20 img%%04d.mpc
which I need to change in some way.
After that, I do some compositing to put the original image back in the center of the page.
François
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding filename to image

Post by fmw42 »

I would write a script loop over each image in some directory and then do all you processing in one command line.

Windows syntax for command line (from your other post).

Code: Select all

convert image.jpg -set filename:name '%t' ^
( -clone 0 -bordercolor "#000000" -border 10x10 ) ^
( -clone 0 -filter cubic -resize 3300x3300 -negate -crop 2500x3300 +repage -blur 0x10 ) ^
-delete 0 -gravity center -compose over -composite ^
-gravity southwest -fill red -font arial -pointsize 28 -annotate +0+0 "%[filename:name]" ^
"%[filename:name]_labeled.jpg"
I do not understand your commands and why you want to blur the image. Do you want the label in the black border area or inside the image before the border.


You could just do:



See
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/Usage/files/#save_escapes
http://www.imagemagick.org/Usage/text/#annotate

Sorry I am a unix person and know little about batch file scripting for doing the looping.

I could do it in a Unix shell script.
Flaver-D
Posts: 19
Joined: 2015-05-21T12:27:39-07:00
Authentication code: 6789

Re: Adding filename to image

Post by Flaver-D »

Sorry for some of the missing explanations and details. I'm new to IM so bare with me.
I'm running IM 6.9.2-10 Q16 x64 on Windows 10

What I'm planning on doing is taking the original image (which will be square) and blowing it up to fit an 11x11 inch page, cropping it back to 8½x11 so that it fits proper on a letter size page, invert it and blur it. Then add the file title, possibly in a white box. Then take the original image, add a frame on the outside and do a composite overlay on top of the inverted page background.

I already got a bit of code that works, but for some reason I want to do more (OK, this is how all my projects end-up... I start by saying that zip ties would work and end-up making a custom metal bracket to do the same thing... and it's the same with computer stuff...)

I first thought of doing this as a photoshop action... but it would require some user input and that's no fun.

So far, this is the code I've got

Code: Select all

convert *.jpg -verbose -filter cubic -resize 3300x3300 -gravity center -extent 2550x3300 -negate -gaussian-blur 20x20 img%%04d.mpc
convert *.jpg -verbose -resize 2000 -bordercolor #000000 -border 20x20 border%%04d.mpc
convert img*.mpc null: border*.mpc -gravity center -layers composite output%%04d.png
but I'm nor married to it...

Thanks.
François
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding filename to image

Post by fmw42 »

Have you tried my single command line to see if that works for one image?
Flaver-D
Posts: 19
Joined: 2015-05-21T12:27:39-07:00
Authentication code: 6789

Re: Adding filename to image

Post by Flaver-D »

OK, I've been working on it and have gone 99% of the way using this

Code: Select all

for %%f in (*.jpg) do (
		echo %%~nf
		convert %%~nf.jpg -verbose -filter triangle -resize 3300x3300 -gravity center -extent 2550x3300^
		+repage -negate -blur 20x20 -gravity southwest -undercolor white -fill black -font arial^
		-pointsize 50 -bordercolor white -border 20 -annotate +200+200 "%%~nf" img%%~nf.mpc
		convert %%~nf.jpg -verbose -resize 2000 -bordercolor #000000 -border 20x20 border%%~nf.mpc
		convert img%%~nf.mpc null: border%%~nf.mpc -gravity center -layers composite output%%~nf.png
)
So far, so good. Now all I need to do is figure out how to make the white box bigger than the text that's in it... the font hugs the box.

I'm pretty proud of myself so far since I barely touched batch files since I was running MD-DOS 6.22 :D
François
Flaver-D
Posts: 19
Joined: 2015-05-21T12:27:39-07:00
Authentication code: 6789

Re: Adding filename to image

Post by Flaver-D »

OK! I finally got it to do what I wanted to... almost.
The script works super well but chokes on filenames containing dashes and spaces, especially the first command.
Granted I didn't make it pretty, but it almost works.
Here's what I do:
I first make a file containing the text that I want using

Code: Select all

convert -size 800x120 xc:white -font arial -pointsize 50 -annotate +20+80 "%%~nf" -trim +repage -bordercolor white -border 10 text%%~nf.mpc
This gives me a bit of space all around the white label so that it looks nice.

Then I create the inverted blurred background without any text like this

Code: Select all

convert %%~nf.jpg -verbose -filter triangle -resize 3300x3300 -gravity center -extent 2550x3300 +repage -negate -blur 20x20 back%%~nf.mpc
Third step is to take the original image and add a black border around it

Code: Select all

convert %%~nf.jpg -verbose -resize 2000 -bordercolor black -border 20x20 img%%~nf.mpc
Fourth step is to put the image in the center of the background

Code: Select all

convert back%%~nf.mpc null: img%%~nf.mpc -gravity center -layers composite output%%~nf.mpc
Last but not least, I insert the text label at the exact location where I want it to be and save the image as a PNG

Code: Select all

convert output%%~nf.mpc null: text%%~nf.mpc -gravity southwest -geometry +200+200 -layers composite output%%~nf.png
All that's left right now is to remove all the MPC and Cache files using the del command and I have the final thing.
if I could just get it to munch through the filenames...
François
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding filename to image

Post by fmw42 »

With -annotate, you can use -undercolor to put a color under the text. See
http://www.imagemagick.org/Usage/text/#annotate
http://www.imagemagick.org/Usage/text/#autosize_box
Flaver-D
Posts: 19
Joined: 2015-05-21T12:27:39-07:00
Authentication code: 6789

Re: Adding filename to image

Post by Flaver-D »

I tried but the box in which the text was was still too small...
François
Flaver-D
Posts: 19
Joined: 2015-05-21T12:27:39-07:00
Authentication code: 6789

Re: Adding filename to image

Post by Flaver-D »

Finally nailed it... all I needed to do was quote the files like this

Code: Select all

for %%f in (*.jpg) do (
echo %%~nf
convert -quiet -size 2550x120 xc:white -font arial -pointsize 50 -annotate +20+80 "%%~nf" -trim +repage -bordercolor white -border 10 text%%~nf.mpc"
convert "%%~nf.jpg" -quiet -filter triangle -resize 3300x3300 -gravity center -extent 2550x3300 +repage -negate -blur 20x20 "back%%~nf.mpc"
convert "%%~nf.jpg" -quiet -resize 2000 -bordercolor black -border 20x20 "img%%~nf.mpc"
convert "back%%~nf.mpc" null: "img%%~nf.mpc" -quiet -gravity center -layers composite "output%%~nf.mpc"
convert "output%%~nf.mpc" null: "text%%~nf.mpc" -quiet -gravity southwest -geometry +200+200 -layers composite "output%%~nf.png"
rem cleanup
del *.mpc
del *.cache
)
So this is the final leg... now working 100%.

Thanks for all the help.... it might have taken me the day to do it but it's finally working fine, that's the main thing.
Now to introduce this in the proper batch file :wink:
François
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding filename to image

Post by fmw42 »

Flaver-D wrote:I tried but the box in which the text was was still too small...
You should be able to make it bigger, by using a stroke of the same color as the undercolor and a larger strokewidth.
Post Reply