PowerShell Photoshop 自动另存为 .png 另存为 .psd

问题描述 投票:0回答:1

这就是我正在尝试做的事情:

我需要对文件夹系统中具有相同名称的所有图像执行 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."

我尝试了以下另存为功能的变体:

已保存 .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)
powershell automation photoshop
1个回答
0
投票

希望比我更有知识的人能够提供更好的答案,但在那之前:

在您提到的每种情况下,听起来应用程序都只是使用默认选项保存的。基于此,您显然没有提取任何设置,或者至少没有采用可以使用的格式。

离开本文档: https://github.com/Adobe-CEP/CEP-Resources/blob/master/Documentation/Product%20special%20Documentation/Photoshop%20Scripting/photoshop-cc-scripting-guide-2019.pdf (第32页)

我意识到它不是 PowerShell,但基于一般格式,可能值得尝试镜像一些示例。例如,尝试将 Adobe 拖放到“Adobe.Photoshop.PNGSaveOptions”中,因为文档中的示例代码仅使用“Photoshop.JPEGSaveOptions”。

还值得注意的是,在示例代码中,他们为文件格式设置创建一个对象,而不是使用 GET 命令。因此,通过尝试使用 New-Object 作为保存选项,您可能会发现更好的结果。我还注意到 VBS 和 JS 示例在保存命令中都包含 True 值,这可能很重要。

抱歉,我无法提供更多信息,但希望这至少可以为您提供更多探索的选择。

© www.soinside.com 2019 - 2024. All rights reserved.