I'm using windows 7 and have a folder containing a couple of hundreds of images. Some of the image has lower values of Width and Height in pixels. I'm looking for images which have a small multiple product of this two fields. There isn't a field that sums this up in the fields list.
I would like to trace down those files by exporting the file list and the width and height attributes to excel spreadsheet.
Is there and straight forword way doing so? If not, is there any workaround?
41 Answer
Use the following PowerShell script and then open sorted.csv with Excel, and perform further manipulation as required.
test.ps1:
$image = New-Object -ComObject Wia.ImageFile
echo ("Name,Width,Height,Area") > test.csv
dir *.png | foreach { $fname =$_.FullName $image.LoadFile($fname) $area=$image.Width*$image.Height echo ('"'+$fname+'",'+$image.Width+","+$image.Height+","+$area)
} >> test.csv
# sort the csv by area (ascending)
Import-Csv test.csv | sort Area | Export-Csv -Path sorted.csv -NoTypeInformationNotes:
- Uses the Wia.ImageFile Com object.
test.csvcontains unsorted outputsorted.csvcontains output sorted (ascending) by "Area" (Width*Height)
Example output:
PS F:\test> dir *.png Directory: F:\test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 01/09/2015 11:45 27156 1.png
-a---- 01/09/2015 11:46 17900 2.png
-a---- 21/05/2015 14:40 114304 3.png
-a---- 15/04/2015 12:56 429394 4.png
PS F:\test> .\test.ps1
PS F:\test> type test.csv
Name,Width,Height,Area
"F:\test\1.png",869,532,462308
"F:\test\2.png",870,344,299280
"F:\test\3.png",328,328,107584
"F:\test\4.png",546,494,269724
PS F:\test> type sorted.csv
"Name","Width","Height","Area"
"F:\test\3.png","328","328","107584"
"F:\test\4.png","546","494","269724"
"F:\test\2.png","870","344","299280"
"F:\test\1.png","869","532","462308"Further Reading
- Windows PowerShell Blog: Image Manipulation in PowerShell