Monday, June 20, 2016

PowerShell v2 Function GetNumberOfPages

This is a quick and dirty function I wrote to do analysis on image files. It simply returns the page count within a Bitmap object, or, -1 ($false) if an error occurs. More work to do on it, but, useful for now.
function GetNumberOfPages
{
     [CmdletBinding()]
     param(
          [Parameter(
              Mandatory = $true,
              ValueFromPipeline = $true,
              ValueFromPipelineByPropertyName = $true
          )]
          [ValidateNotNullOrEmpty()]
          [String]
          $TiffImage
     )
    
     if(([appdomain]::currentdomain.getassemblies() | Where {$_ -match "System.Drawing"}) -eq $null)
     {
          Write-Verbose "Loading System.Drawing assembly.";
          [Void] [System.Reflection.Assembly]::LoadFromPartialName("System.Drawing");
     }
    
     if($file = [System.Drawing.Bitmap]::FromFile($TiffImage))
     {
          return $file.GetFrameCount([System.Drawing.Imaging.FrameDimension]::Page)
     }
     else
     {
          return -1
     }
}
In usage, I go down this road:
 PSC:somedirectory> Get-ChildItem ‘.Images**** | ForEach-Object {"$($_.Name): $(GetNumberOfPages $_.fullname)"}
Which returns as simple list:
test.jpg: 1
1.tif: 14
2.tif: 3
Simple, but, free. I know it needs a lot of expansion, but, for 10 minutes of Googling and coding I am happy with it. Back to work.

Related Post:

0 comments:

Post a Comment

 
Copyright 2009 Information Blog
Powered By Blogger