Recompressing Large Images with ImageMagick and PowerShell

ImageMagick, it seems, is picky about the order of it's parameters.  For example, when calling mogrify to compress files, you have to put the -quality paramter before the filename, not after.

In one of my previous scripts I batch-converted some images to JPGs but inadvertently set the JPEG quality to 100% after getting caught up in parameter order issues.  This left me with a lot of very large (15MB+) files that are taking up valuable space on my laptop.

This is a quick and dirty script that finds the large files and calls mogrify to recompress with the JPEG compression level set to 85.  For my images (mostly scanned 35mm film) this gives me files closer to 5MB.

$Quality=85     #desired JPEG compression level
$SizeFilter=10  #size in megabytes of files you want to target

if (Test-Path -path $Path)
    Write-Host "Info: Found Path $Path"

    $Items = Get-ChildItem -path $Path -recurse | where  { ($_.Length /1MB) -gt $SizeFilter }

    #Loop through and act on each file individually. We're only going to touch the big files so we'll mogrify them individually, efficiency be damned

    ForEach ($Item in $Items)

    #$ItemFullName = $Item.Fullname

    $MagickParams = @("-quality", $Quality, $Item.Fullname)
    $MagickCmd = "mogrify"

    Write-Host "Calling $MagickCmd $MagickParams"

    & $MagickCmd $MagickParams


    write-Host "Path $Path Not Found. Abending."

No comments:

Post a Comment