I am trying to append a caption to every image in a directory. The Caption is the filename without the extension. I use ImageMagick and a batch file to do the work. I cannot use Cygwin for the task, as I will give the batch file to other people, and I am already stretching it when asking them to install ImageMagick, so they can use the batch file I'm currently writing. I have read for /?
in cmd.exe
, but understood only half of it (or less, hard to determine).
I should probably use %~nI
somewhere, but this is currently more of a NB than anything else.
Q&A I rely on includes:
https://stackoverflow.com/a/41934445/7077264, https://stackoverflow.com/a/34301140/7077264 which contains a similar method to what I need to do, but it does it in Linux (which I'd be happy to use, but I need a solution working in MS Windows), https://www.robvanderwoude.com/for.php, https://legacy.imagemagick.org/script/command-line-options.php#caption, https://legacy.imagemagick.org/Usage/annotating/#labeling, https://stackoverflow.com/a/138581/7077264, https://www.youtube.com/playlist?list=PL35B3EFC3D6CD1DF7, Set the value of a variable with the result of a command in a Windows batch file, https://stackoverflow.com/a/42919993/7077264, https://stackoverflow.com/a/6362922/7077264, Set a variable based upon the result of a command that lonely guy's solution looks somehow promising, but I can't get my finger on the part that I'd want to use.
(I don't understand the options for for /f [options]
, specifically delims=
and usebackq
. If they are important to my problem, please tell me why, i.e. which delims are meant, and why would I need to specify them?)
The file names have blank spaces, because they are used for the image captions. "C:\Path\dir" (see C) has no spaces whatsoever. The files have the same filename length and look like so:
img1, ProjA.jpg
img2, ProjA.jpg
img3, ProjA.jpg
img4, ProjA.jpg
img5, ProjA.jpg
output (dir)
This is what I came up with up to now (5 a.m., I am wrecked, and I need to go to sleep), and frankly, I'm at an impasse. I get dizzy trying to find out which %%a and which %filename% needs quotes, which one should be escaped in which way and how I'm supposed to get the width of the image being handled, which is yet another problem I haven't even mentioned until now, but I hope I will understand how to incorporate when things here are sorted out.
Here goes for the command prompt:
A. command prompt
setlocal
set "yourExt=.jpg"
for /f "%a" in (*%yourExt%) do (
set filename="%a"
magick convert "%filename%" -background "#f3f3f3" -size 500x100 -fill "#111" -gravity Center label:"%filename%" -append "".\output\labelled-%filename%""
)
endlocal
After running version A in cmd.exe
I get output this:
...
...
C:\Path\dir> set filename="%a"
C:\Path\dir> magick convert "%filename%" -background "#f3f3f3" -size 500x100 -fill "#111" -gravity Center label:"%filename%" -append "".\output\labelled-%filename%""
convert: unable to open image '%a': No such file or directory @ error/blob.c/OpenBlob/3536.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/572.
convert: unknown image property "%a" @ warning/property.c/InterpretImageProperties/3762.
convert: no images defined `.\output\labelled-%a"' @ error/convert.c/ConvertImageCommand/3322.
Ideally, I would store all this in a handy script I can just double-click. Only difference: "%%a" instead of "%a". And also, when I call this in cmd.exe, nothing happens, no output, just naught, but the script seems to be running, since I can kill it with Ctrl+C and answering yes to whether I want to kill the script.
B. script
setlocal
set "yourExt=.jpg"
for /f "%%a" in (*%yourExt%) do (
set filename="%%a"
magick convert "%filename%" -background "#f3f3f3" -size 500x100 -fill "#111" -gravity Center label:"%filename%" -append "".\output\labelled-%filename%""
)
endlocal
Since it doesn't change anything (that I would know), if I include some more lines I found on SO in one of the links above, but there might be a good reason to include them, so here:
C. script again
setlocal
set "yourDir=C:\Path\dir"
set "yourExt=.jpg"
pushd %yourDir%
for /f "%%a" in (*%yourExt%) do (
set filename="%%a"
magick convert "%filename%" -background "#f3f3f3" -size 500x100 -fill "#111" -gravity Center label:"%filename%" -append "".\output\labelled-%filename%""
)
popd
endlocal