Search code examples
imagepowershellexport-to-excelbmpfromfile

How save png as jpg without saving the file in dir


I'm using FromFile to get the image out of files, and it has the following error for the png's on the FromFile line:

Exception calling "FromFile" with "1" argument(s): "The given path's format is not supported."

So, I'm trying to convert the bmp's to jpg, (see convert line above FromFile below) but all the examples I see (that seem usable) are saving the file. I don't want to save the file in the dir. All I need is the image format, so FromFile can use it like this example. I saw ConvertTo-Jpeg, but I don't think this is a standard powershell module, or don't see how to install it.

I saw this link, but I don't think that would leave the image in the format needed by FromFile.

This is my code:

               $imageFile2 = Get-ChildItem -Recurse -Path $ImageFullBasePath -Include @("*.bmp","*.jpg","*.png") | Where-Object {$_.Name -match "$($pictureName)"}  #$imageFile | Select-String -Pattern '$($pictureName)' -AllMatches
                

                Write-Host $imageFile2
                if($imageFile2.Exists)
                {
                    if($imageFile2 -Match "png")
                    {
                        $imageFile2 | .\ConvertTo-Jpeg #I don't think this will work with FromFile below
                    }
                    $image = [System.Drawing.Image]::FromFile($imageFile2)  step
                }
                else {
                    Write-Host "$($imageFile2) does not exist"
                }

And then I put it in excel:

  $xlsx = $result | Export-Excel -Path $outFilePath -WorksheetName $errCode -Autosize -AutoFilter -FreezeTopRow -BoldTopRow  -PassThru # -ClearSheet can't ClearSheet every time or it clears previous data  ###left off
  $ws = $xlsx.Workbook.Worksheets[$errCode]
  $ws.Dimension.Columns  #number of columns
  $tempRowCount = $ws.Dimension.Rows     #number of rows

  #only change width of 3rd column
  $ws.Column(3).Width
  $ws.Column(3).Width = 100

  #Change all row heights
  for ($row = 2 ;( $row -le $tempRowCount ); $row++)
  {
     #Write-Host $($ws.Dimension.Rows)
     #Write-Host $($row)
     $ws.Row($row).Height
     $ws.Row($row).Height = 150
     #place the image in spreadsheet
     #https://github.com/dfinke/ImportExcel/issues/1041  https://github.com/dfinke/ImportExcel/issues/993
     $drawingName = "$($row.PictureID)_Col3_$($row)" #Name_ColumnIndex_RowIndex   
     Write-Host $image
     $picture = $ws.Drawings.AddPicture("$drawingName",$image)
     $picture.SetPosition($row - 1, 0, 3 - 1, 0)
     if($ws.Row($row).Height -lt $image.Height * (375/500)) {
         $ws.Row($row).Height = $image.Height * (375/500)
     }
     if($ws.Column(3).Width -lt $image.Width * (17/120)){
        $ws.Column(3).Width = $image.Width * (17/120)
     }
  }

Update:

I just wanted to reiterate that FromFile can't be used for a png image. So where Hey Scripting Guy saves the image like this doesn't work:

$image = [drawing.image]::FromFile($imageFile2)

Solution

  • I figured out that the $imageFile2 path has 2 filenames in it. It must be that two met the Get-ChildItem/Where-Object/match criteria. The images look identical, but have similar names, so will be easy to process. After I split the names, it does FromFile ok.