如何将png保存为jpg而不将文件保存在目录中
How save png as jpg without saving the file in dir
我正在使用 FromFile 从文件中获取图像,它在 FromFile 行上出现以下 png 错误:
Exception calling "FromFile" with "1" argument(s): "The given path's
format is not supported."
所以,我正在尝试将 bmp 转换为 jpg,(请参阅下面 FromFile 上方的转换行)但我看到的所有示例(似乎可用)都是 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,但我不认为这是一个标准的powershell模块,不然看不到怎么安装。
我看到了 this link,但我认为这不会使图像保留 FromFile 所需的格式。
这是我的代码:
$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"
}
然后我把它放在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)
}
}
更新:
我只是想重申 FromFile 不能用于 png 图像。所以 Hey Scripting Guy 像这样保存图像的地方不起作用:
$image = [drawing.image]::FromFile($imageFile2)
我发现 $imageFile2 路径中有 2 个文件名。肯定是有两个满足了Get-ChildItem/Where-Object/match的条件。这些图像看起来相同,但名称相似,因此很容易处理。拆分名称后,FromFile 就可以了。
我正在使用 FromFile 从文件中获取图像,它在 FromFile 行上出现以下 png 错误:
Exception calling "FromFile" with "1" argument(s): "The given path's format is not supported."
所以,我正在尝试将 bmp 转换为 jpg,(请参阅下面 FromFile 上方的转换行)但我看到的所有示例(似乎可用)都是 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,但我不认为这是一个标准的powershell模块,不然看不到怎么安装。
我看到了 this link,但我认为这不会使图像保留 FromFile 所需的格式。
这是我的代码:
$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"
}
然后我把它放在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)
}
}
更新:
我只是想重申 FromFile 不能用于 png 图像。所以 Hey Scripting Guy 像这样保存图像的地方不起作用:
$image = [drawing.image]::FromFile($imageFile2)
我发现 $imageFile2 路径中有 2 个文件名。肯定是有两个满足了Get-ChildItem/Where-Object/match的条件。这些图像看起来相同,但名称相似,因此很容易处理。拆分名称后,FromFile 就可以了。