【问题标题】:Server-side SVG to PNG (or some other image format) in pythonpython中的服务器端SVG到PNG(或其他一些图像格式)
【发布时间】:2011-02-25 07:41:29
【问题描述】:

目前我正在使用 rsvg 加载 svg(从字符串,而不是从文件)并绘制到 cairo。有人知道更好的方法吗?我在我的应用程序的其他地方使用了 PIL,但我不知道如何使用 PIL 来做到这一点。

【问题讨论】:

  • PIL 不支持 SVG;快速浏览表明您可能拥有正确的工具链。
  • 我刚刚在此处发布了关于此问题的最新评论 - stackoverflow.com/a/19718153/542550 - 下面的评论指出“ImageMagick 支持似乎很糟糕”,但该评论者没有构建/测试它。现在是 10/2013,我刚刚测试了使用 ImageMagick(通过 Wand-py)导入各种 SVG,效果很好!我还有更多的测试要做,如果我犯了严重的错误,我肯定会撤下这个评论,但在这一点上,它已经在一些已知使用其他方法存在错误的 SVG 上完美运行。

标签: python png svg


【解决方案1】:

这是我目前拥有的:

import cairo
import rsvg

def convert(data, ofile, maxwidth=0, maxheight=0):

    svg = rsvg.Handle(data=data)

    x = width = svg.props.width
    y = height = svg.props.height
    print "actual dims are " + str((width, height))
    print "converting to " + str((maxwidth, maxheight))

    yscale = xscale = 1

    if (maxheight != 0 and width > maxwidth) or (maxheight != 0 and height > maxheight):
        x = maxwidth
        y = float(maxwidth)/float(width) * height
        print "first resize: " + str((x, y))
        if y > maxheight:
            y = maxheight
            x = float(maxheight)/float(height) * width
            print "second resize: " + str((x, y))
        xscale = float(x)/svg.props.width
        yscale = float(y)/svg.props.height

    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, x, y)
    context = cairo.Context(surface)
    context.scale(xscale, yscale)
    svg.render_cairo(context)
    surface.write_to_png(ofile)

【讨论】:

  • 欢迎来到 Stackoverflow!一般的经验法则是,如果您发布的是解决问题的方法,那么它可能是一个答案;如果它的上下文或解决问题的尝试不起作用,或者觉得一起被黑客攻击是一个很好的答案,那么它应该作为编辑进入问题。这段代码似乎是您希望获得 cmets 的“您正在做的事情”(“任何人都知道更好的方法”),而不是问题的解决方案,这一事实表明这确实应该在您的问题中。
  • 里面有一些错误。创建 ImageSurface 时,您需要 Integer surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, round(int(x)), round(int(y)) 并且您应该确保 maxwidth 和 maxheight 始终设置为一个比 0 更好的数字。我有许多高度为 32000 的 SVG,这将导致内存错误。
【解决方案2】:

imagemagic 怎么样? - http://www.imagemagick.org/script/magick-vector-graphics.php 它可以从/向 stdin/stdout 读取/写入,因此即使您不想使用文件,也可以将其与您的应用程序集成

【讨论】:

  • ImageMagick convert 早于 2010 年 5 月的版本在解释 SVG 方面做得很糟糕。鉴于变更日志,他们似乎还没有使 SVG 支持运行良好(尽管我没有构建它来查看)。
【解决方案3】:

我已经安装了 inkscape,所以我只是用 inkscape -f file.svg -e file.png 将进程分配给 inkscape 命令

使用此代码:

import subprocess
inkscape_dir=r"C:\Program Files (x86)\Inkscape"
assert os.path.isdir(inkscape_dir)
os.chdir(inkscape_dir)
subprocess.Popen(['inkscape.exe',"-f",fname,"-e",fname_png])

我在 Windows 7 上,并且在切换到 inkscape 目录之前收到 Windows 5 错误 [拒绝访问](或类似的东西)

【讨论】:

  • 考虑将cwd=inkscape_dir 传递给Popen,而不是更改父进程的目录。
【解决方案4】:

您也可以为此使用 PhantomJS(请参阅http://phantomjs.org/screen-capture.html)

从外壳:

phantomjs rasterize.js http://ariya.github.com/svg/tiger.svg tiger.png

或者从 python 使用 selenium:

from selenium import webdriver  
driver = webdriver.PhantomJS()
driver.set_window_size(1024, 768) 
driver.get('http://ariya.github.com/svg/tiger.svg')
driver.save_screenshot('tiger.png')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 2017-05-11
    相关资源
    最近更新 更多