这是我正在尝试做的事情:
我需要对文件夹系统中具有相同名称的所有图像执行 Photoshop 操作。然后,我需要将图像保存为 .png 文件,并在与原始图像相同的位置使用新名称。以下代码在大多数情况下都可以正常工作,除了不保存 .png 文件,而是保存 .psd 文件。
我不允许使用任何第三方模块,例如 PowerShell-Photoshop-Api-Tools。
这是我到目前为止的代码:
# Specify the root folder where your images are located
$rootFolderPath = "C:\PhotoshopTest"
# Specify the common image name (excluding file extension)
$imageName = "TestImage"
# Specify the name of the action you want to apply
$actionSetName = "AutomationTest"
$actionName = "Test1"
# Connect to Photoshop
$app = New-Object -ComObject "Photoshop.Application"
# Get all image files with the same name (excluding file extensions) in the folder and subfolders
$imageFiles = Get-ChildItem -Path $rootFolderPath -Recurse -File | Where-Object { $_.BaseName -eq $imageName }
# Process each image
foreach ($imageFile in $imageFiles) {
# Open the image
$doc = $app.Open($imageFile.FullName)
# Execute any necessary edits or adjustments
$app.DoAction($actionName, $actionSetName)
# Save the modified image with a new name
$newFileName = "Test1.png"
$newImagePath = Join-Path $imageFile.DirectoryName $newFileName
$pngSaveOptions = [System.Type]::GetType("Adobe.Photoshop.PNGSaveOptions")
$doc.SaveAs($newImagePath, $pngSaveOptions)
# Close the image
$doc.Close()
}
# Quit Photoshop
$app.Quit()
Write-Host "Images processed and saved with new names."
我尝试了 SaveAs 函数的以下变体:
保存了 .psd 文件:
$doc.SaveAs($newImagePath)
$pngSaveOptions = [System.Type]::GetType("Adobe.Photoshop.PsSaveOptions")
$doc.SaveAs($newImagePath, $pngSaveOptions)
$pngSaveOptions = [System.Type]::GetType("Adobe.Photoshop.TiffSaveOptions")
$doc.SaveAs($newImagePath, $pngSaveOptions)
$app.ActiveDocument.SaveAs($newImagePath, $pngSaveOptions)
破坏了并且没有保存任何东西:
$doc.SaveAs($newImagePath, 16)
$doc.SaveAs($newImagePath, [System.Drawing.Imaging.ImageFormat]::Png)
@fatalrrer 和 @mclayton 找到的解决方案:
$pngSaveOptions = new-object -ComObject "Photoshop.PngSaveOptions