PNG批量转换成SVG图

1.创建png2svg.ps1脚本

# 临时添加 Inkscape 的路径到当前会话的 PATH
$env:Path += ";H:\tools\png2svg\ImageMagick-7.1.1;H:\tools\png2svg\potrace-1.16.win64"

# inkscape --action-list
# pause

# 设置输入目录(包含 PNG 文件的目录)
$input_dir = "D:\aoyiduo\woterm\woterm\private\skins\blue"
#$input_dir = "H:\tools\png2svg\skin"

# 设置输出目录(与输入目录平级的 svg 目录)
$output_dir = Join-Path -Path (Split-Path -Path $input_dir -Parent) -ChildPath "svg"

# 如果输出目录不存在,则创建
if (-not (Test-Path $output_dir)) {
    New-Item -ItemType Directory -Path $output_dir | Out-Null
}

# 获取目录中的所有 PNG 文件
$png_files = Get-ChildItem -Path $input_dir -Filter *.png

# 遍历所有 PNG 文件
foreach ($file in $png_files) {
    try {
        # 设置输出文件路径(在输出目录中生成对应文件名的 SVG 文件)
        $temp_file = Join-Path -Path $output_dir -ChildPath ([System.IO.Path]::ChangeExtension($file.Name, ".bmp"))
        $output_file = Join-Path -Path $output_dir -ChildPath ([System.IO.Path]::ChangeExtension($file.Name, ".svg"))
        
        # 如果目标文件已存在,先删除旧文件
        if (Test-Path $output_file) {
            Write-Output "File already exists, deleting: $($output_file)"
            Remove-Item -Path $output_file -Force
        }
        # alpha default on
        # magick $file.FullName -alpha on $temp_file
        magick $file.FullName $temp_file
        # 背景是黑色,图案是透明色。
        # potrace $temp_file -s -o $output_file
        # 图案是黑色,背景是透明色。
        potrace $temp_file --invert -s -o $output_file
        
        # pause
        Remove-Item $temp_file

        # 打印成功信息
        Write-Output "Converted to vector: $($file.Name) -> $($output_file)"
    } catch {
        # 打印错误信息
        Write-Error "Failed to convert: $($file.FullName). Error: $_"
    }
}

2.创建一个调用脚本,用于临时解决脚本权限问题

 powershell -ExecutionPolicy Bypass -File "png2svg.ps1"
 pause