Script to list image files by size and resolution, export to Excel
Instructions for using PowerShell to export a list of images and their properties to Excel as a tab-delimited CSV.
Windows PowerShell is a DOS-like task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. It looks a lot like Windows DOS and can work similarly. As one of its many functions, PowerShell can be used to collect information about the files on a computer and export that information to Excel as a tab-delimited comma-separated values file (CSV).
Previously, I used PowerShell to list all of the files in a directory and export their name, date of last modification and size (in bytes) to Excel.
Now, I want to create a list of images which includes their height and width in pixels, their date of creation, their size in megabytes as well as their resolution in megapixels.
Listing images of one type
Let’s assume that our images are located in c:\images. We can use the following script to locate all images in c:\images with a width and height greater than 500 pixels. The output will be placed in a file located at c:\images\img.csv.
Get-ChildItem -Recurse c:\images -Filter *.jpg | % {
$image = [System.Drawing.Image]::FromFile($_.FullName)
if ($image.width -gt 500 -and $image.height -gt 500) {
New-Object PSObject -Property @{
height_pixels = $image.Height
width_pixels = $image.Width
megapixels = ($image.Height * $image.Width)/1000/1000
megabytes = (($_.Length)/1024)/1024
name = $_.Name
fullname = $_.Fullname
date = $_.LastWriteTime
}
}
} | Export-Csv 'c:\images\img.csv' -NoTypeInformation
Some features of the code:
-Filter *.jpg
= Find files with a .jpg extension.$image.width -gt 500
= Choose images with a width greater than 500 pixels. (This could be changed to-lt
for “less than” or another filter could be used if that’s what’s required). Another option is to use-or
instead of-and
when defining the full filter.(($_.Length)/1024)/1024
= Convert the file size from bytes to megabytes (a more meaningful measure for most people).($image.Height * $image.Width)/1000/1000
= Calculate the resolution in megapixels from the height and width parameters.$_.Name
= The file’s name.$_.Fullname
= The file’s name and path.
Some of these parameters can be omitted, depending on specific requirements.
Listing multiple image types
Unfortunately the -Filter
operation can’t be used for multiple file types, so the code needs to be modified if multiple image file types are to be listed:
Get-ChildItem -Recurse c:\images $originalPath -Include @("*.png", "*.jpg") | % {
$image = [System.Drawing.Image]::FromFile($_.FullName)
if ($image.width -gt 500 -and $image.height -gt 500) {
New-Object PSObject -Property @{
height_pixels = $image.Height
width_pixels = $image.Width
megapixels = ($image.Height * $image.Width)/1000/1000
megabytes = (($_.Length)/1024)/1024
name = $_.Name
fullname = $_.Fullname
date = $_.LastWriteTime
}
}
} | Export-Csv 'c:\images\img.csv' -NoTypeInformation
The list of file types can be extended as much as is required.
Don’t forget that -Include
operations consume a lot more resources than -Filter
, so the script will run more slowly. This could come at quite some cost if many thousands of images are to be searched.
Comments
9 responses to “Script to list image files by size and resolution, export to Excel”
we need script to export list only file name and files resolutions.
we have 50000 .tif files.
can help me anyone.
So why not use this script? It should work for you, although it may take some time.
Just in case if someone gets error that unable to find System.Drawing.Image assembly is missing, use this on the top line of your script
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
If you get error that ExceptionPolicy is restricted, run powershell as admin user and type this:
Set-ExecutionPolicy RemoteSigned
Cannot find path ‘C:\Windows\System32\WindowsPowerShell\v1.0\System.Drawing.Bitmap’ because it does not exist
Thanks for the useful Powershell script
Before posting this comment i have been searching to get the images dimensions(approximately 450 images), finally i achieved with your script after couple of hours.
Thanks for the great help.
Thanks,
Venkat.
Thank you! Very usefull the script.
How do you control the column order of the image properties in the CSV?
Thanks for this script. 🙌
I was looking for a way to get dimensions (eg 1024 x 950 px )of multiple images in a folder. Was able to successfully do it 👊.
Total newbie, this was my first time actually opening a PowerShell and running a script !!!
I had to do 3 things to get the output:
1. Got an error :
“Unable to find type [System.Drawing.Image].”
SO recommended :
“Make sure you load the following assemblies at the top of your script:
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”) ”
Source: https://stackoverflow.com/a/27792262/2369164
Using that assembly fixed it !!
So here is the final script :
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
Get-ChildItem -Recurse c:\images $originalPath -Include @(“*.png”, “*.jpg”) | % {
$image = [System.Drawing.Image]::FromFile($_.FullName)
if ($image.width -gt 500 -and $image.height -gt 500) {
New-Object PSObject -Property @{
height_pixels = $image.Height
width_pixels = $image.Width
megapixels = ($image.Height * $image.Width)/1000/1000
megabytes = (($_.Length)/1024)/1024
name = $_.Name
fullname = $_.Fullname
date = $_.LastWriteTime
}
}
} | Export-Csv ‘c:\images\img.csv’ -NoTypeInformation
2. Had to create a folder name “images” in c:\
3. Had to create a file named img.csv in “c:\images” folder.
To do so I opened a blank notepad and saved it as img.csv and in file “Save as type” field selected “All files”.
Cheers from down under !