0

I have the script:

convert a.jpg ( -clone 0 -fill white -colorize 100 -fill black -draw "polygon 500,300 500,1500 1300,1500 1300,300" -alpha off -write mpr:mask +delete ) -mask mpr:mask +repage -threshold 50% -morphology open square:4 +mask c.jpg

which happily takes my image, makes a mask, and does what I need it to do, on a per image basis, using an original filename for the input, and a new filename for the output.

However, I'm trying to get this to run on every image in a folder, and I'm having zero luck...

I have tried many .bat files, such as:

@echo on
setlocal enabledelayedexpansion
set img_folder=C:\me\pics\
set output_folder=C:\me\pics\cropped
for /f "delims=" %%i in ('dir /b "%img_folder%\*.jpg"') do (
  set input_file=%img_folder%\%%i
  set output_file=%output_folder%\%%i
  convert %input_file% ( -clone 0 -fill white -colorize 100 -fill black -draw "polygon 500,300 500,1500 1300,1500 1300,300" -alpha off -write mpr:mask +delete ) -mask mpr:mask +repage -threshold 50% -morphology open square:4 +mask %output_file%
)
pause

However, something about the brackets seems to be messing with everything else, as the bracket after +delete is pairing up in sublimetext with the bracket after "do" in the for loop.

I'm really stumped, I've tried everything I can think of, and could really use some help, if anyone can offer a simple solution, I'd be very much appreciative!

Compo
  • 36,585
  • 5
  • 27
  • 39
TomB3D
  • 1
  • 2
  • Have you tried escaping your parentheses? Also in a .bat script, you need to double your % to %% for example on 50%% – fmw42 Jan 18 '23 at 16:32
  • I have tried changing the crop to a region, but will work one by one, however when going through a batch script it only gives a white square in the region: set "input_folder=C:\Users\tcsha\Pictures\test scans\PhotosForCyphemeTest\Cypheme Test Labels" set "output_folder=C:\Users\tcsha\Pictures\test scans\PhotosForCyphemeTest\Cypheme Test Labels\ImageMagickd" if not exist "%output_folder%" mkdir "%output_folder%" for %%f in ("%input_folder%\*") do ( magick "%%f" -region 650x650+600+325 +repage -threshold 50% -morphology open square:4 "%output_folder%\%%~nf.jpg" ) – TomB3D Jan 18 '23 at 16:39
  • Beware of the [delayed expansion trap](https://stackoverflow.com/a/30284028/2128947) Try `convert "!input_file!" ( -clone .... "!output_file!"`, or preferably `convert "%img_folder%\%%i" ( -clone .... "%output_folder%\%%i"` I'm assuming that `convert` is a `.exe` – Magoo Jan 18 '23 at 17:23
  • Rule number one on working with a __FOR__ loop in a batch file: Do not assign a string to an environment variable which is already assigned to the loop variable. The solution is the single command line `@for /F "eol=| delims=" %%I in ('dir "C:\me\pics\*.jpg" /A-D-L /B 2^>nul') do @convert.exe "C:\me\pics\%%I" ( -clone 0 -fill white -colorize 100 -fill black -draw "polygon 500,300 500,1500 1300,1500 1300,300" -alpha off -write mpr:mask +delete ) -mask mpr:mask +repage -threshold 50%% -morphology open square:4 +mask "C:\me\pics\cropped\%%I"` replacing the entire batch file. – Mofi Jan 18 '23 at 18:15
  • Please note further that a percent sign must be escaped with one more `%` in a batch file to be interpreted by `cmd.exe` as literal character which is important to know here because of the the option `-threshold 50%` which must be written in a batch file as `-threshold 50%%` to get passed to `convert.exe` the two arguments `-threshold` and `50%`. It would be additionally better to specify `convert.exe` with its fully qualified file name. Then `cmd.exe` must not search for `convert` on each JPEG file to process in file system using the environment variables `PATH` and `PATHEXT`. – Mofi Jan 18 '23 at 18:19

2 Answers2

1

Instead of having a batch file looping on convert calls, you can have convert loop on the files. This requires a special syntax to create the output file name from the input file.

(untested, I don't run Windows)

convert -verbose C:\me\pics\*.jpg -set filename:out "c:\me\pics\cropped\%%[basename]" {your_filter_goes_here} "%%[filename:out].jpg"
Compo
  • 36,585
  • 5
  • 27
  • 39
xenoid
  • 8,396
  • 3
  • 23
  • 49
0

What about this simpler version, which makes sure your command is not nested within parentheses?

@SetLocal EnableExtensions DisableDelayedExpansion
@Set "img_folder=C:\me\pics"
@Set "output_folder=C:\me\pics\cropped"
@For /F "EOL=? Delims=" %%G In ('Dir "%img_folder%\*.jpg" /A:-D /B 2^>NUL'
) Do @convert "%img_folder%\%%G" ( -clone 0 -fill white -colorize 100 -fill black -draw "polygon 500,300 500,1500 1300,1500 1300,300" -alpha off -write mpr:mask +delete ) -mask mpr:mask +repage -threshold 50%% -morphology open square:4 +mask "%output_folder%\%%G"
@Pause
Compo
  • 36,585
  • 5
  • 27
  • 39