Get the detail informations from a .png file in PowerShell

  • You might want to use the PowershellPack Module which contains get-image:

    PS D:\> import-module psimagetools
    PS D:\> get-item .\fig410.png | get-image
    FullName              : D:\fig410.png
    FormatID              : {B96B3CAF-0728-11D3-9D7B-0000F81EF32E}
    FileExtension         : png
    FileData              : System.__ComObject
    ARGBData              : System.__ComObject
    Height                : 450
    Width                 : 700
    HorizontalResolution  : 96,0119934082031
    VerticalResolution    : 96,0119934082031
    PixelDepth            : 32
    IsIndexedPixelFormat  : False
    IsAlphaPixelFormat    : True
    IsExtendedPixelFormat : False
    IsAnimated            : False
    FrameCount            : 1
    ActiveFrame           : 1
    Properties            : System.__ComObject
    
  • or you could use Wia.ImageFile directly (which is how the get-image function does it) this way:

    PS D:\> $image  = New-Object -ComObject Wia.ImageFile
    PS D:\> $image.loadfile("D:\fig410.png")
    PS D:\> $image
    
    FormatID              : {B96B3CAF-0728-11D3-9D7B-0000F81EF32E}
    FileExtension         : png
    FileData              : System.__ComObject
    ARGBData              : System.__ComObject
    Height                : 450
    Width                 : 700
    HorizontalResolution  : 96,0119934082031
    VerticalResolution    : 96,0119934082031
    PixelDepth            : 32
    IsIndexedPixelFormat  : False
    IsAlphaPixelFormat    : True
    IsExtendedPixelFormat : False
    IsAnimated            : False
    FrameCount            : 1
    ActiveFrame           : 1
    Properties            : System.__ComObject
    

You can get most of this information from the files extended properties like this:

$path = 'D:\image.png'
$shell = New-Object -COMObject Shell.Application
$folder = Split-Path $path
$file = Split-Path $path -Leaf
$shellfolder = $shell.Namespace($folder)
$shellfile = $shellfolder.ParseName($file)

$width = 27
$height = 28
$Dimensions = 26
$size = 1

$shellfolder.GetDetailsOf($shellfile, $width)
$shellfolder.GetDetailsOf($shellfile, $height)
$shellfolder.GetDetailsOf($shellfile, $Dimensions)
$shellfolder.GetDetailsOf($shellfile, $size)

You can also get the size in other ways such as (Get-Item D:\image.png).Length / 1KB.

The bit depth property doesn't seem to be listed in the extended properties though even though its available when you right click the file.

Update Another option is to use .NET proper to avoid using COM:

add-type -AssemblyName System.Drawing
$png = New-Object System.Drawing.Bitmap 'D:\image.png'
$png.Height
$png.Width
$png.PhysicalDimension
$png.HorizontalResolution
$png.VerticalResolution

Update 2 The PixelFormat property gives you the bit depth.

$png.PixelFormat

The property is an enumeration of possible formats. You can view the complete list here.

For example, Format32bppArgb is defined as

Specifies that the format is 32 bits per pixel; 8 bits each are used for the alpha, red, green, and blue components.

Tags:

Powershell

Png