Home > database >  Get an imagemagick script (with brackets for a mask) to run as a script for every file in a folder?
Get an imagemagick script (with brackets for a mask) to run as a script for every file in a folder?

Time:01-19

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!

CodePudding user response:

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"

CodePudding user response:

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
  • Related