Move images based on dimension

A while ago I created a script to copy the Windows 10 lock screen images, this puts all the files in 1 directory. I wanted to sort those images based on their dimensions, so I move them to two different folders, one called “landscape” and one called “portrait”.

The script is pretty easy, first I need to load system.drawing, which is done by this line:

Add-Type -AssemblyName System.Drawing

After that I set the parameters for the script (change those lines to match your needs):

$ImageList = Get-ChildItem "C:\SourceFiles"
$LandscapePath = "C:\Pictures\Landscape"
$PortraitPath = "C:\Pictures\Portrait"

Then I loop through the list of images. Open the image to get its attributes. Once the attributes (Height and Width) are obtained, the image needs to be closed, so the file isn’t open when I want to move it. If the width is greater than the height, it is a landscape oriented image, otherwise it’s portrait oriented and it needs to be moved to the corresponding folder.

#Get the image information
$image = New-Object System.Drawing.Bitmap $ImageFile.Fullname
#Get the image attributes
$ImageHeight = $image.Height
$ImageWidth = $image.Width
#Close the image
$image.Dispose()
If($ImageWidth -gt $ImageHeight)
{
    Move-Item -LiteralPath $ImageFile.FullName -Destination $LandscapePath -Force
}
Else
{
    Move-Item -LiteralPath $ImageFile.FullName -Destination $PortraitPath -Force
}

You can download the entire script here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s