Page 1 of 1
Bat file help
Posted: 2009-09-11T14:12:37-07:00
by Bonzo
I am trying a bat file to resize and watermark images. All works up to a point and I have two problems:
1/ I want to scale the text but can only use whole numbers.
2/ For some reason I can not use a dynamic filename.
Can anyone point me in the correct direction to sort these problems ?
Code: Select all
:: Resize the photo and add the border to the larger version - save both versions ( one is a tempory file )
convert.exe %1 ( +clone -thumbnail 600x600 -strip -bordercolor Black -border 2x2 -write "temp.png" +delete ) -thumbnail 150x150 -strip -unsharp 1.5x1+0.7+0.02 -quality 80 thumb.jpg "th_%~n1.jpg"
:: Get the temp image width and height and set the variables
FOR /F %%x IN ('identify -ping -format "%%w" temp.png') DO SET w=%%x
FOR /F %%x IN ('identify -ping -format "%%h" temp.png') DO SET h=%%x
:: Scale the watermark image - NOT WORKING AS NUMBER MUST BE A WHOLE NUMBER!
::SET/A w1=%w%x0.9
::SET/A h1=%h%x0.2
:: Create the watermark text
convert.exe -size %w%x%h% -background none -font Utopia-bold -fill white -gravity center caption:"Copyright of Anthony" -shade 240x40 "font.png"
:: Composite the watermark onto the image - IF I USE A NAME LIKE embossed.jpg THIS WORKS BUT "norm_%~n1.jpg" FAILS
composite -watermark 15% -gravity center font.png temp.png embossed.jpg
:: Tidy up - Could be left as it will be overwritten by the next image
del font.png
del temp.png
Re: Bat file help
Posted: 2009-09-11T15:14:30-07:00
by fmw42
you will have to convert the unix to Windows Batch, but use convert to do the scale, so in place of
w1=%w%x0.9
use
w1=`convert xc: -format "%[fw:floor(w*0.9)]" info:`
you can use this to replace your identify so you do it all in one step if you want.
Then you would have
w1=`convert temp.png -ping -format "%[fw:floor(w*0.9)]" info:`
as for your output names, you can try enclosing it all in quotes and see if that helps. there was a question recently about names like abcd[8x8].jpg that could not be resolved as 'abcd[8x8].jpg', but you can search the forum and see if you can find that.
Re: Bat file help
Posted: 2009-09-11T19:42:07-07:00
by el_supremo
Code: Select all
:: Scale the watermark image - NOT WORKING AS NUMBER MUST BE A WHOLE NUMBER!
::SET/A w1=%w%x0.9
::SET/A h1=%h%x0.2
You should be able to do this with integer arithmetic:
Code: Select all
SET/A w1=(%w%x9)/10
SET/A h1=%h%/5
It will give you integer results which you need anyway. You could also fiddle it so that it rounds the result.
Pete
Re: Bat file help
Posted: 2009-09-12T01:49:08-07:00
by Bonzo
Thanks for the replys but I give up, the calculations will only work with a round number e.g 1 and not .9
Error = Numeric constants are either decimal(17), hexadecimal (0x11), or octal (021)
My only other choice is to do the calculation with ImageMagick.
The problem with the save name was I forgot to escape the % in 15%
Re: Bat file help
Posted: 2009-09-12T11:51:24-07:00
by Bonzo
Good old Anthony's examples come through again
Code: Select all
:: Resize the photo and add the border to the larger version
:: Save the thumbnail and a temp version to add the watermark to later
convert.exe %1 ( +clone -thumbnail 600x600 -strip -bordercolor Black -border 2x2 -write "temp.png" +delete ) -thumbnail 150x150 -strip -unsharp 1.5x1+0.7+0.02 -quality 80 thumb.jpg "th_%~n1.jpg"
:: Select the temp image width and height and set the variables
FOR /F %%x IN ('identify -ping -format "%%[fx:w*0.9]" temp.png') DO SET w=%%x
FOR /F %%x IN ('identify -ping -format "%%[fx:h*0.2]" temp.png') DO SET h=%%x
:: Create the watermark text - resizing to fit the image
convert.exe -size %w%x%h% -background none -font Utopia-bold -fill white -gravity center caption:"Copyright of Anthony" -shade 240x40 "font.png"
:: Composite the watermark onto the image
composite -watermark 20%% -gravity center font.png temp.png "norm_%~n1.jpg"
::Create a text file to hold the image description
@echo off>%~n1.txt
:: Write default text to file
echo Image details to follow >%~n1.txt
:: Tidy up - Could be left as it will be overwritten by the next image
del font.png
del temp.png
Re: Bat file help
Posted: 2009-09-12T13:30:22-07:00
by fmw42
FOR /F %%x IN ('identify -ping -format "%%[fx:w*0.9]" temp.png') DO SET w=%%x
FOR /F %%x IN ('identify -ping -format "%%[fx:h*0.2]" temp.png') DO SET h=%%x
same as my suggestion above (perhaps you missed it) -- I just used convert in place of identify. They are equivalent, however, I believe that identify may be slightly faster than convert
However, you said you needed integer results and thus you might need to add the floor() to truncate to the lowest integer.
FOR /F %%x IN ('convert temp.png -ping -format "%%[fx:floor(w*0.9)]"') DO SET w=%%x
FOR /F %%x IN ('convert temp.png -ping -format "%%[fx:floor(h*0.2)]"') DO SET h=%%x
0r
FOR /F %%x IN ('identify -ping -format "%%[fx:floor(w*0.9)]" temp.png') DO SET w=%%x
FOR /F %%x IN ('identify -ping -format "%%[fx:floor(h*0.2)]" temp.png') DO SET h=%%x
Re: Bat file help
Posted: 2009-09-13T05:02:27-07:00
by Bonzo
Sorry Fred; I did miss it. I must have been thinking to much about doing the calculations with the batch file.
Anyway I am sorted now and it works for what I want; thanks again.
Re: Bat file help
Posted: 2009-09-13T11:53:58-07:00
by fmw42
How did you avoid the integer requirement if you did not use floor() or something like it?
Re: Bat file help
Posted: 2009-09-13T12:22:00-07:00
by Bonzo
I did not want an integer but the batch file would only accept integers; I managed to confuse both Pete and you
I wanted 0.9 to allow a bit of space between the watermark text and the side of the photos and 0.2 x height to keep the text on the one line.