0

Need to get height and width of image files in shell script.

I have a script that returns the correct image size, however the height and width don't always correlate to the correct orientation (portrait or landscape). Is there an attribute I can check to determine whether to use h x W or W x H ?

$imgloc = 'c:\pics'
$folder = 'pics'
Set-Location -Path $imgloc

$files = Get-ChildItem -Path $imgloc
$icnt = 0
foreach ($file in $files) {
    if ($file.Extension -ne ".jpg") { continue }
    $icnt++
   $hfile = (get-item $file)
    $img = [System.Drawing.Image]::Fromfile($hfile);
    $ih= $img.Size.Height
    $iw= $img.Size.width
    $imgsize = "$iw"+"x"+"$ih"

    $imgsize
}
  • 1
    Does this help? https://stackoverflow.com/questions/27835064/get-image-orientation-and-rotate-as-per-orientation There is a `PropertyTagOrientation`. – lit Sep 20 '19 at 20:03
  • This solution looks like it will work, however I was unable to get it to work in my script. Using the below syntax, I am getting this error : Exception calling "GetPropertyItem" with "1" argument(s): "Property cannot be found. – john berner Sep 26 '19 at 14:47

1 Answers1

1

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

Theo
  • 57,719
  • 8
  • 24
  • 41
  • [`$value = $img.GetPropertyItem( 274).Value[0]`](https://learn.microsoft.com/en-gb/windows/win32/gdiplus/-gdiplus-constant-property-item-descriptions#propertytagorientation) and `$orientation` values are declared in the `[System.Drawing.RotateFlipType]` enum class. – JosefZ Sep 20 '19 at 23:10
  • @JosefZ Please see my edit. IMO the values you retrieve agree more with the `[System.Photo.Orientation]` values which results in clearer descriptive names, hence the lookup array. I did change the way the value is read with your cleaner suggestion `$value = $img.GetPropertyItem(274).Value[0]` though. Thanks for that. – Theo Sep 21 '19 at 10:06
  • I can't absorb _`GDI+`_ versus _`.Net`_ discrepancies… And now I'm totally confused with _Windows Property System_ [grin]… – JosefZ Sep 21 '19 at 21:11