Home > Software design >  howto use imagemagick in powershell
howto use imagemagick in powershell

Time:12-27

I'm trying to convert a bat file into a powershell script. But I cannot solve the imagemagick command to determine if the photo is blackwhite.

Can you assist me ? w.

batfile :

for %%f in (*.jpg) do (
%%f
for /f %%i in ('magick "%%f" -colorspace HSL -channel g -separate  channel -format "%%[fx:mean]" info:') do set VAR=%%i
if !VAR! LEQ 0.05 copy "%%f" .\bw)

powershell:

param ([string]$Path = "\\nas\photo\")

Add-Type -assembly "system.io.compression.filesystem"

$PathArray = @()
$magickExePath = "C:\Program Files\ImageMagick-7.1.0-Q16-HDRI\magick.exe"


Get-ChildItem $Path  -directory -exclude "#recycle"  -re | 



      ForEach-Object { 
            #$_.FullName
            #$_.Name
         If (($_.Name -match "landschap"))
         {      
                $source = $_.FullName 
                $_.FullName
                $_.name
                $MagickArguments = "$_.name -colorspace HSL -channel g -separate  channel -format "$_.name[fx:mean]" info:' "
                $ColorLevel = $magickExePath $MagickArguments

         }    
         }

I'm expecting $colorlevel is a number between 0 and 1.

CodePudding user response:

  • If you want PowerShell to execute an executable whose path is stored in a variable (or is quoted), you must use &, the call operator.

  • To store arguments in a variable, create an array, each element of which represents a separate argument to pass.

$MagickArguments = @(
  $_.Name
  '-colorspace'
  'HSL'
  '-channel'
  'g'
  '-separate'
  ' channel'
  '-format'
  '%[fx:mean]'
  'info'  
)

[double] $ColorLevel = & $magickExePath $MagickArguments

It would be simpler to pass the arguments directly however:

[double] $ColorLevel = & $magickExePath $_.Name -colorspace HSL -channel g -separate  channel -format %[fx:mean] info:
  • Related