我正在寻找将 PNG 转换为 SVG。
目标系统处于离线状态,因此无法在脚本中调用在线服务。 我也希望避免安装第三方服务/软件。 如果需要,便携式版本也可以使用。
Windows系统请使用CMD或Powershell。
我尝试使用 InkSpace Portanle 1.2.2 版本,但便携式版本不接受脚本(或者至少我无法使其工作)。
当前脚本:
param(
[string]$path = 'C:\Users\<user>\Downloads\Convert',
[string]$exec = "C:\Users\<user>\Downloads\InkscapePortable\InkscapePortable.exe")
foreach ($filename in Get-ChildItem $path) {
if ($filename.toString().EndsWith('.png')) {
$targetName = $filename.BaseName + ".svg";
#Write-Output -"Converting $filename ..."
$command = "& `"$exec`" --export-type='svg' --export-filename='$targetName' $fileName";
Invoke-Expression $command;
}
}
我尝试将上面的脚本与 InkSpace 的便携版一起使用,但它只加载了 exe,而没有导出 svg。
经过挖掘,我学到了很多关于 SVG 的知识。
我发现您可以将照片作为 base64 嵌入到 SVG 中,而不是跟踪图像并对其进行矢量化。
这可能处理速度较慢(如某些地方所读),但不会像某些工具那样降低图像分辨率。
就我而言,嵌入是一个可以接受的解决方案。
以下是我的工作脚本:
$File ='' #Image file path here such as D:\old.png
$Export='' ##Export Path such as D:\new.svg
$CompanyLogo=Get-Content -Path $file -AsByteStream
$CompanyLogo=[System.Convert]::ToBase64String($CompanyLogo)
Add-Type -AssemblyName System.Drawing
$image = New-Object System.Drawing.Bitmap $File
$Width=$image.Width
$Height=$image.Height
$XML = "<?xml version=""1.0"" standalone=""no""?>
<!DOCTYPE svg PUBLIC ""-//W3C//DTD SVG 20010904//EN""
""http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"">
<svg version=""1.0"" xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink""
width=""$Width"" height=""$Height"" >
<image
width=""$Width"" height=""$Height""
href=""data:image/png;base64,$CompanyLogo""
/>
</svg>"
$XML | Out-File -FilePath $Export -Force