Several resources online demonstrate resizing or otherwise filtering an image using built-in Windows COM components, such as this MSDN article and this TechNet article. I wanted to do this in Powershell without relying on other scripts or modules. However, following the guidance using WIA.ImageFile and WIA.ImageProcess were giving me an error "The parameter is incorrect" when attempting to apply a resize.
The solution turned out to be simple. If you don't set both the width and height, that's what you'll get. Below is my version of the resizing script, which takes all the files in your current directory and saves them all out scaled down to 720p resolution (for an old TV I'd like to use as a big digital photo frame). I was unable to find out how to control the output quality with originals around 4MB and 6000x4000 in size weighing in at at a svelte 200KB post-conversion. Without further ado,
$jpgImg = New-Object -ComObject WIA.ImageFile;
$filter = New-Object -ComObject WIA.ImageProcess;
$filter.Filters.Add($filter.FilterInfos("Scale").FilterID);
$filter.Filters[1].Properties("MaximumWidth").Value = 1280;
$filter.Filters[1].Properties("MaximumHeight").Value = 720;
dir | % {$file = $_; $jpgImg.LoadFile($file.FullName); $jpgImg = $filter.Apply($jpgImg); $jpgImg.SaveFile('C:\Users\example_user\Desktop\example_folder\' + $file.Name);}
No comments:
Post a Comment