Skip to content

Dear Internet Explorer user: Your browser is no longer supported

Please switch to a modern browser such as Microsoft Edge, Mozilla Firefox or Google Chrome to view this website's content.

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:

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”

On 4 November 2016, Deepak Shukla wrote: Hyperlink chain icon

we need script to export list only file name and files resolutions.
we have 50000 .tif files.
can help me anyone.

Reply

    On 8 November 2016, Adam Dimech wrote: Hyperlink chain icon

    So why not use this script? It should work for you, although it may take some time.

    Reply

On 19 May 2018, Aditya Patel wrote: Hyperlink chain icon

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

Reply

On 21 May 2018, Kamran wrote: Hyperlink chain icon

Cannot find path ‘C:\Windows\System32\WindowsPowerShell\v1.0\System.Drawing.Bitmap’ because it does not exist

Reply

On 5 May 2019, Joe Peni wrote: Hyperlink chain icon

Thanks for the useful Powershell script

Reply

On 8 October 2019, Venkat wrote: Hyperlink chain icon

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.

Reply

On 4 March 2020, Alexandru P wrote: Hyperlink chain icon

Thank you! Very usefull the script.

Reply

On 14 May 2020, Michael Rhodes wrote: Hyperlink chain icon

How do you control the column order of the image properties in the CSV?

Reply

On 21 October 2020, aj AU wrote: Hyperlink chain icon

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 !

Reply

Have Your Say

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.