Getting command line to work in windows batch file.

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
michaelmahoney
Posts: 7
Joined: 2014-02-01T07:25:31-07:00
Authentication code: 6789

Getting command line to work in windows batch file.

Post by michaelmahoney »

I'm having trouble getting a convert command line to work in a windows .cmd file. The command line (sort of like -polaroid) works fine:

Code: Select all

FOR %a in (*.jpg) DO convert %%a[800x] -set filename:fname %t -auto-orient -bordercolor grey60 -border 2 -bordercolor white -border 24 -bordercolor grey60 -border 2 -background none -wave 10x1600 -chop 0x10 -background black ( +clone -shadow 60x10+10+10 -rotate 180 ) +swap -background none -flatten -rotate 5 -resize 290x290 -unsharp 2x0.5+0.7+0 %[filename:fname]_thumb.png
But the batch file creates the thumbnails directory, then dies without getting far enough to hit PAUSE:

Code: Select all

SETLOCAL EnableDelayedExpansion
%~d1
CD "%~p1"
MD thumbnails
FOR %%A in (*.jpg) DO (
FOR %a in (*.jpg) DO convert %%a[800x] -set filename:fname %%t -auto-orient -bordercolor grey60 -border 2 -bordercolor white -border 24 -bordercolor grey60 -border 2 -background none -wave 10x1600 -chop 0x10 -background black ( +clone -shadow 60x10+10+10 -rotate 180 ) +swap -background none -flatten -rotate 5 -resize 290x290 -unsharp 2x0.5+0.7+0 %%[filename:fname]_thumb.png
)
PAUSE
I'm new to IM and despite hours of (very interesting) doc-trolling can't for the life of me find it. What silly obvious head-slapping thing am I missing?


PS: I also can't seem to get

Code: Select all

thumbnails/%%[filename:fname]_thumb.png
to drop the created files into the thumbnails subdirectory. Same issue?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Getting command line to work in windows batch file.

Post by snibgo »

You are looping through all the jpegs, %%A. For every one you find, you try to loop through them all again. I don't see why, especially as you never use %%A.

Then you have a bug, writing "FOR %a" instead of "FOR %%a".

For readability, I would chop the convert command into lines.

Code: Select all

SETLOCAL EnableDelayedExpansion
%~d1
CD "%~p1"
MD thumbnails
FOR %%A in (*.jpg) DO convert ^
 %%A[800x] ^
  -set filename:fname %%t -auto-orient ^
  -bordercolor grey60 -border 2 ^
  -bordercolor white -border 24 ^
  -bordercolor grey60 -border 2 ^
  -background none -wave 10x1600 -chop 0x10 ^
  -background black ^
  ( +clone -shadow 60x10+10+10 -rotate 180 ) ^
  +swap ^
  -background none -flatten ^
  -rotate 5 ^
  -resize 290x290 -unsharp 2x0.5+0.7+0 ^
  %%[filename:fname]_thumb.png

PAUSE
snibgo's IM pages: im.snibgo.com
michaelmahoney
Posts: 7
Joined: 2014-02-01T07:25:31-07:00
Authentication code: 6789

Re: Getting command line to work in windows batch file.

Post by michaelmahoney »

snibgo wrote:You are looping through all the jpegs, %%A. For every one you find, you try to loop through them all again. I don't see why, especially as you never use %%A.

Then you have a bug, writing "FOR %a" instead of "FOR %%a".
Ah, hell. That's a typo- more accurately a copy/paste error. You are quite right, there should be only one loop, using %%A. I was looking so hard for some subtle error I didn't see the silly one right in front of my face.

Much thanks. Now that that is working in a batch I can use your other solution to grab the image width and feed -wave as well.

:D
Post Reply