Mogrify not working on batch

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
chakiss
Posts: 1
Joined: 2017-09-13T11:27:06-07:00
Authentication code: 1151

Mogrify not working on batch

Post by chakiss »

Hey all!
I am trying to create a batch file that process all jpgs in multiple subdirectories of certain folder.
It looks like this:

Code: Select all

cd C:\codigo2\
for /r /d %%a in (*) do  mogrify -fuzz 30% -trim "%%~a\*.jpg"
pause
for /r /d %%a in (*) do mogrify -resize 1000x5000 "%%~a\*.jpg"
for /r /d %%a in (*) do mogrify -gravity center -extent 1115x5000 "%%~a\*.jpg"
pause
if I use mogrify -trim, it works perfectly. But as soon as I try to add the -fuzz 30%, it throughts the following error:

Image
The following usage of the path operator in batch-parameter substitution is invalid: %~a*.jpg"
Do you know how to tell windows I am trying to pass multiple parameters with hyphens?

This code works perfectly, is the added -fuzz 30% that breaks it.

Code: Select all

cd C:\codigo2\
for /r /d %%a in (*) do  mogrify -trim "%%~a\*.jpg"
pause
for /r /d %%a in (*) do mogrify -resize 1000x5000 "%%~a\*.jpg"
for /r /d %%a in (*) do mogrify -gravity center -extent 1115x5000 "%%~a\*.jpg"
pause
I also tried this on cmd and work perfectly:

Code: Select all

 mogrify -fuzz 30% -trim "image.jpg"

Best regards!
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Mogrify not working on batch

Post by GeeMack »

chakiss wrote: 2017-09-13T11:40:12-07:00This code works perfectly, is the added -fuzz 30% that breaks it.
When using percent signs in a BAT script you must almost always double them. Your "-fuzz 30%" that works from the command prompt needs to be "-fuzz 30%%" in the script.
jpeni
Posts: 9
Joined: 2015-01-12T14:56:33-07:00
Authentication code: 6789
Location: Uniondale NY, USA

Re: Mogrify not working on batch

Post by jpeni »

Here is a useful Powershell script the converts recursively. you can modify it to run any conversions or file processing.

If desired, you can run Mogrify instead of Convert.

Code: Select all


# Powershell script to recursively convert image formats
# Configuration
$srcfolder = "C:\test\Animals"
$destfolder = "C:\test\Animals"
#This ps1 file will add copy files to designated folder
#Do NOT use Mogrify or the original images will be deleted
$im_convert_exe = "convert.exe -density 300"
# with VECTOR files the density setting should come BEFORE the vector format
# is specified or the image will be blurry.
# for example - for vector files place -density option immediately after the convert.exe
# command in the im_convert_exe definition.  This way it will be set before any 
# vector format is specified.
# change src_filter to the format of the source files
$src_filter = "*.eps"

# change dest_ext to the format of the destination files
$dest_ext = "png"
$options = "-depth 8 -alpha off"
$logfile = "C:\temp\convert.log"
$fp = New-Item -ItemType file $logfile -force
$count=0
foreach ($srcitem in $(Get-ChildItem $srcfolder -include $src_filter -recurse))
{
    $srcname = $srcitem.fullname

    # Construct the filename and filepath for the output
    $partial = $srcitem.FullName.Substring( $srcfolder.Length )
    $destname = $destfolder + $partial
    $destname= [System.IO.Path]::ChangeExtension( $destname , $dest_ext )
    $destpath = [System.IO.Path]::GetDirectoryName( $destname )

    # Create the destination path if it does not exist
    if (-not (test-path $destpath))
    {
        New-Item $destpath -type directory | Out-Null
    }

    # Perform the conversion by calling an external tool
    $cmdline =  $im_convert_exe + " `"" + $srcname  + "`"" + $options + " `"" + $destname + "`" " 
    #echo $cmdline
    invoke-expression -command $cmdline

    # Get information about the output file    
    $destitem = Get-item $destname

    # Show and record information comparing the input and output files
    $info = [string]::Format( "{0} `t {1} `t {2} `t {3} `t {4} `t {5}", $count, 
	$partial, $srcname, $destname, $srcitem.Length ,  $destitem.Length)
    echo $info
    Add-Content $fp $info

    $count=$count+1
} 

Post Reply