Python:在没有抗锯齿的情况下将 svg 转换为 png

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

我正在尝试在 python 中将 svg 转换为 png,但我不想要任何压缩和抗锯齿。 我试图将 inkscape 与子进程一起使用,但我找不到告诉 inkscape 我不想要任何压缩和抗锯齿的参数。

inkscape_path = "D:/Programmes/bin/inkscape.exe"
dpi_value = 300

result = subprocess.run([inkscape_path, "cleaned.svg", "--export-id=layer1", "--export-ignore-filters", "--export-type=png", "--export-filename=" + "cleaned.png", "--export-dpi=" + str(dpi_value)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

if result.returncode != 0:
    print("Error: " + result.stderr.decode("utf-8"))
else:
    print("PNG file exported successfully.")

我也试过用魔杖来做,但是尽管 img.antialisaing = False 仍然存在抗锯齿:

# Load SVG file as an image object
with Image(filename='cleaned.svg') as img:
    # Disable antialiasing
    library.MagickSetOption(img.wand, b'antialias', b'0')
    img.antialias = False
    
    # Convert image format to PNG
    img.format = 'png'
    
    # Save without compression
    img.compression_quality = 100

    # Save to 300 dpi
    img.resolution = (300, 300)
    # Save output file
    img.save(filename='cleaned.png')

在 inkscape 的 UI 中,我们可以在导出过程中指定它,你知道我们是否可以在 python 中使用子进程或库来做同样的事情吗?

想要的结果:

不需要的结果:

python svg inkscape
© www.soinside.com 2019 - 2024. All rights reserved.