Bat file help

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

Bat file help

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Bat file help

Post 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.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Bat file help

Post 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
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Bat file help

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

Re: Bat file help

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Bat file help

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

Re: Bat file help

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Bat file help

Post by fmw42 »

How did you avoid the integer requirement if you did not use floor() or something like it?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Bat file help

Post 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.
Post Reply