Use OpenCV with Powershell

Question:

I currently have a Python script that is using OpenCV and PIL to auto crop head shots. My work has asked me to convert to powershell for various reasons of working in a windows world.

Looking around I am not seeing anything specific to powershell but I have seen some VB/.net versions floating around. Is it possible to leverage them with powershell?

Also, does powershell have anything like PIL (pillow now)?

Asked By: Grady D

||

Answers:

There is a simple image manipulation module included with the PowerShellPack called PSImageTools that does basic operations such as rotate, crop, and convert formats.

You can also use the built-in .NET libraries for image manipulation.

Example:

$maxX = 256
$maxY = 256
$b = New-Object -TypeName Drawing.Bitmap($maxX, $maxY)

foreach($x in 0..($maxX-1)) {
    foreach($y in 0..($maxY-1)) {
        [Drawing.Color]$c = '{0},{1},{2}' -f ($x, $y, (255-$x))
        $b.SetPixel($x, $y, $c)
    }
}
$path = 'C:Tempgradient.bmp'
$b.Save($path)
Invoke-Item $path 

If you have a VB/.NET library that does what you want, you can import it with Add-Type -Path C:PathTo.dll, and use it in a similar way as in the above example.

Answered By: Rynant
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.