Extracting "color representation" property in of an image

Hi Guys,

I have two codes with me. I now want to merge this two codes together.

Code:1 - This will give you the image metadata info in a excel sheet:

Write-Host "Starting the script...Please wait untill the extraction is complete"

Function Get-FileMetaData

{

Param([string[]]$folder)

foreach($sFolder in $folder)

{

$a = 0

$objShell = New-Object -ComObject Shell.Application

$objFolder = $objShell.namespace($sFolder)

foreach ($File in $objFolder.items())

{

$FileMetaData = New-Object PSOBJECT

for ($a ; $a -le 266; $a++)

{

if($objFolder.getDetailsOf($File, $a))

{
$hash += @{$($objFolder.getDetailsOf($objFolder.items, $a)) = $($objFolder.getDetailsOf($File, $a) -replace([char]8206,"") -replace([char]8207,"") ) }

$FileMetaData | Add-Member $hash

$hash.clear()

} #end if

} #end for

$a=0

$FileMetaData

} #end foreach $file

} #end foreach $sfolder

} #end Get-FileMetaData

$picdata = Get-FileMetaData $Env:userprofile\Desktop\image |select name,‘Item type’,size, height,width,‘horizontal resolution’,‘vertical resolution’,‘color depth’, ‘Bit depth’,‘jpeg_colorspace’

$picdata | Export-csv $Env:userprofile\Desktop\image\ImageData.csv


Code 2 - This will give the “colorspace” info of an image through a form. The user has to select each image individually to find this property:

########################################################### Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing

Main Form

$mainForm = New-Object System.Windows.Forms.Form
$mainForm.Font = “Comic Sans MS,8.25”
$mainForm.Text = “ColorSpace Extraction”
$mainForm.ForeColor = “White”
$mainForm.BackColor = “DarkBlue”
$mainForm.width = 400
$mainForm.height = 480

Title Label

$titleLabel = New-Object System.Windows.Forms.Label
$titleLabel.Font = “Comic Sans MS,12”
$titleLabel.ForeColor = “Yellow”
$titleLabel.Location = “95,10”
$titleLabel.Size = “300,30”
$titleLabel.Text = “ColorSpace Extraction”
$mainForm.Controls.Add($titleLabel)

Form Labels

$colorSpaceLabel = New-Object System.Windows.Forms.Label
$colorSpaceLabel.Font = “Comic Sans MS,8”
$colorSpaceLabel.Location = “20,100”
$colorSpaceLabel.Size = “120,15”
$colorSpaceLabel.Text = “Color Space”
$mainForm.Controls.Add($colorSpaceLabel)

Form TextBoxes

$colorSpaceTextBox = New-Object System.Windows.Forms.TextBox
$colorSpaceTextBox.Font = “Comic Sans MS,8”
$colorSpaceTextBox.Location = “140,100”
$colorSpaceTextBox.Size = “200,15”
$mainForm.Controls.Add($colorSpaceTextBox)

SelectFile Button

$imageFileNameButton = New-Object System.Windows.Forms.Button
$imageFileNameButton.Font = “Comic Sans MS,8”
$imageFileNameButton.Location = “30,212”
$imageFileNameButton.Size = “75,23”
$imageFileNameButton.ForeColor = “DarkBlue”
$imageFileNameButton.BackColor = “White”
$imageFileNameButton.Text = “Select File”
$imageFileNameButton.add_Click({selectFile})
$mainForm.Controls.Add($imageFileNameButton)

Exit Button

$exitButton = New-Object System.Windows.Forms.Button
$exitButton.Location = “270,212”
$exitButton.Size = “75,23”
$exitButton.ForeColor = “Red”
$exitButton.BackColor = “White”
$exitButton.Text = “Exit”
$exitButton.add_Click({$mainForm.close()})
$mainForm.Controls.Add($exitButton)

function selectFile {
$selectForm = New-Object System.Windows.Forms.OpenFileDialog
$selectForm.Filter = “All Files (.)|.
$selectForm.InitialDirectory = “.”
$selectForm.Title = “Select a Image File to Process”
$getKey = $selectForm.ShowDialog()
If ($getKey -eq “OK”) {
$filename = $selectForm.FileName
$photo = [System.Drawing.Image]::FromFile($filename)

    # Color Space
    $colorProperty = $photo.GetPropertyItem(40961)
    if ($colorProperty -ne $null){
        $colorSpace = [System.BitConverter]::ToUInt16($colorProperty.Value, 0)}
    if ($colorSpace -eq "1") {$colorSpace = "sRGB"}
    if ($colorSpace -eq "2") {$colorSpace = "Adobe RGB"}
    if ($colorSpace -eq "65535") {$colorSpace = "Uncalibrated"}
    $colorSpaceTextBox.Text = $colorSpace   
         
   

} # End $getkey If

} # End SelectFile Function

function MakeNumber {
$first =$args[0].value[0] + 256 * $args[0].value[1] + 65536 * $args[0].value[2] + 16777216 * $args[0].value[3] ;
$second=$args[0].value[4] + 256 * $args[0].value[5] + 65536 * $args[0].value[6] + 16777216 * $args[0].value[7] ;
if ($first -gt 2147483648) {$first = $first - 4294967296} ;
if ($second -gt 2147483648) {$second= $second - 4294967296} ;
if ($second -eq 0) {$second= 1} ;
if (($first –eq 1) -and ($second -ne 1)) {
write-output (“1/” + $second)}
else {
write-output ($first / $second)}
} # End Function MakeNumber

[void] $mainForm.ShowDialog()


This is what I wanna do:

I want to merge the colorspace code into code 1 so that the result will be added into the excel. (without the form interface) Can some one help me on where I should I add the colorspace code in Code 1 to accomplish this?

Any help guys?

I think I’ve got it working. In Code 1, after the line

$a=0

Add the following:

$fileName = "$($objFolder.Self.Path)\$($objFolder.getDetailsOf($File, 0))"

$photo = [System.Drawing.Image]::FromFile($fileName)

# Color Space
 try { 
    $colorProperty = $photo.GetPropertyItem(40961)
    if ($colorProperty -ne $null) {
        $colorSpace = [System.BitConverter]::ToUInt16($colorProperty.Value, 0)}
    if ($colorSpace -eq "1") {$colorSpace = "sRGB"}
    if ($colorSpace -eq "2") {$colorSpace = "Adobe RGB"}
    if ($colorSpace -eq "65535") {$colorSpace = "Uncalibrated"}
    $cs = @{'ColorSpace' = $colorSpace}
 } #end try
 
 catch {
    $cs = @{'ColorSpace' = 'Property Not Set'}
 } #end catch
  
$FileMetadata | Add-Member $cs

I had to add the try/catch because an exception was generated if the file did not have that property set. I would recommend additional error handling to skip files that are not images, these generated an out of memory exception when I tested the script.

HI There !

This works perfectly fine when I run it through the PowerShell ISE…But when I convert this into exe i’m getting the error message as “Unable to find type [System.Drawing.Image]. Make sure that the assembly that contains this type is loaded”

And the data in the excel sheet is “Property Not Set” for the colorspace.

how to fix this?

Should this be added to the code?
Add-Type -AssemblyName System.Drawing

Any help ?

What are you using to turn the PowerShell code into an exe?

ps1 to exe tool

I just tested it with PS2EXE from the TechNet Gallery and you’re right, you will need to add

Add-Type -AssemblyName System.Drawing 

Place it at the top of the function declaration after

Param([string[]]$folder)

Also, a minor thing, in the please wait message ‘untill’ should only have one ‘l’ :slight_smile:

yeah this works fine…but i’m getting “exception calling…out of memory” message in the cmd when i run the exe…is there any way to make this disappear?

From my testing that error was generated when the script attempts to process a file that is not an image. You will need to add code to skip files that are not supported. A list of supported images types can be found here:
https://msdn.microsoft.com/en-us/library/at62haz6(v=vs.110).aspx