Rotate or flip multiple image files using PowerShell
Use a PowerShell script in Microsoft Windows to rotate or flip large numbers of images automatically.
Having taken a large number of digital photos for a particular project, I needed to rotate them all 90-degrees for further processing.
Whilst it’s possible to open Adobe Photoshop or other image manipulation software and rotate images, this quickly becomes unfeasible when the numbers get into the dozens.
Thankfully it’s possible to employ Microsoft PowerShell to do the work for you. PowerShell can batch rotate and/or flip images in the BMP, GIF, JPEG, PNG, TIFF and WMF formats. Bear in mind that there may be a loss of quality going from a format such as TIFF to a format such as JPEG.
The following script will rotate PNG and JPG images 90-degrees in the target directory and any sub-directories:
$path = "c:/path/to/files" #target directory
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
Get-ChildItem -recurse ($path) -include @("*.png", "*.jpg") |
ForEach-Object {
$image = [System.Drawing.image]::FromFile( $_ )
$image.rotateflip("Rotate90FlipNone")
$image.save($_)
}
Images of a specific type can be targeted by specifying these within -include
. This script will recurse any subdirectories, but this can be removed by deleting the -recurse
command.
Rotation Options
$image.rotateflip("Rotate90FlipNone")
tells PowerShell to rotate the image 90-degrees but not to flip the image.
According to The Scripting Guy, the following rotation and flipping options are available:
Name | Description |
---|---|
RotateNoneFlipNone | Uses no rotation and no flipping. |
Rotate90FlipNone | Uses a 90-degree rotation without flipping. |
Rotate180FlipNone | Uses a 180-degree rotation without flipping. |
Rotate270FlipNone | Uses a 270-degree rotation without flipping. |
RotateNoneFlipX | Uses no rotation followed by a horizontal flip. |
Rotate90FlipX | Uses a 90-degree rotation followed by a horizontal flip. |
Rotate180FlipX | Uses a 180-degree rotation followed by a horizontal flip. |
Rotate270FlipX | Uses a 270-degree rotation followed by a horizontal flip. |
RotateNoneFlipY | Uses no rotation followed by a vertical flip. |
Rotate90FlipY | Uses a 90-degree rotation followed by a vertical flip. |
Rotate180FlipY | Uses a 180-degree rotation followed by a vertical flip. |
Rotate270FlipY | Uses a 270-degree rotation followed by a vertical flip. |
RotateNoneFlipXY | Uses no rotation followed by a horizontal and vertical flip. |
Rotate90FlipXY | Uses a 90-degree rotation followed by a horizontal and vertical flip. |
Rotate180FlipXY | Uses a 180-degree rotation followed by a horizontal and vertical flip. |
Rotate270FlipXY | Uses a 270-degree rotation followed by a horizontal and vertical flip. |
Simply substitute the relevant command in the code prior to execution.
Comments
4 responses to “Rotate or flip multiple image files using PowerShell”
When using this method, my JPG changes from 3MB to 16MB…
Not acceptable !
Any suggestions about how to avoid ?
$image.save($_, “jpeg”) or your file gets a losless PNG-conversion.
Thanks that was exactly what I needed.
With Windows 10 which has Powershell readily installed it ran with a lightning speed – definitely recommend this post to other users of this as ultimate solution.
Hi, I’m trying to use powershell to rotate EMF file, is there a way to do it?