You could use something like below to get all the properties you seem to need from the jpg file and act upon the returned value Orientation
$imgloc = 'D:\' #'# dummy comment to restore syntax highlighting in SO
# the various System.Photo.Orientation values
# See: https://learn.microsoft.com/en-us/windows/win32/properties/props-system-photo-orientation
# https://learn.microsoft.com/en-gb/windows/win32/gdiplus/-gdiplus-constant-property-item-descriptions#propertytagorientation
$orientation = 'Unknown', 'Normal', 'FlipHorizontal', 'Rotate180', 'FlipVertical', 'Transpose', 'Rotate270', 'Transverse', 'Rotate90'
Get-ChildItem -Path $imgloc -Filter '*.jpg' | ForEach-Object {
$img = [System.Drawing.Image]::Fromfile($_.FullName)
# return the properties
$value = $img.GetPropertyItem(274).Value[0]
[PsCustomObject]@{
'Path' = $_.FullName
'Width' = $img.Size.width
'Height' = $img.Size.Height
'Orientation' = $orientation[$value]
}
}
This would return as example
Path Width Height Orientation
---- ----- ------ -----------
D:\test.jpg 2592 1456 Normal
D:\test2.jpg 2336 4160 Normal
D:\test3.jpg 2560 1920 Rotate270
and if Orientation
is either Rotate270
or Rotate90
you can swap the values for width and height.
Edit
As JosefZ commented, the orientation values are also declared in the [System.Drawing.RotateFlipType]
enum class like this:
Name Value
---- -----
Rotate180FlipXY 0
RotateNoneFlipNone 0
Rotate90FlipNone 1
Rotate270FlipXY 1
Rotate180FlipNone 2
RotateNoneFlipXY 2
Rotate270FlipNone 3
Rotate90FlipXY 3
RotateNoneFlipX 4
Rotate180FlipY 4
Rotate90FlipX 5
Rotate270FlipY 5
Rotate180FlipX 6
RotateNoneFlipY 6
Rotate270FlipX 7
Rotate90FlipY 7
with values ranging from 0 to 7 and none of them distinct, whereas the values you get using $img.GetPropertyItem(274).Value[0]
range from 1 to 8 and coincide with the values for System.Photo.Orientation