python 中的服务器端 SVG 到 PNG(或其他图像格式)

发布于 2024-09-03 05:39:54 字数 97 浏览 1 评论 0原文

目前,我正在使用 rsvg 加载 svg(从字符串,而不是从文件)并绘制到开罗。有人知道更好的方法吗?我在应用程序的其他地方使用 PIL,但我不知道如何使用 PIL 来执行此操作。

Currently I'm using rsvg to load the svg (from a string, not from a file) and drawing to cairo. Anyone know a better way? I use PIL elsewhere in my application, but I don't know of a way to do this with PIL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

飘逸的'云 2024-09-10 05:39:54

这是我目前拥有的:

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)

Here's what I currently have:

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)
美羊羊 2024-09-10 05:39:54

图像魔法怎么样? - http://www.imagemagick.org/script/magick-vector-graphics .php 它可以从标准输入/标准输出读取/写入,因此即使您不想使用文件,也可以将其与您的应用程序集成

How about imagemagic? - http://www.imagemagick.org/script/magick-vector-graphics.php It can read/write from/to stdin/stdout so You can integrate it with your app even if You don't want to use files

伤感在游骋 2024-09-10 05:39:54

您还可以使用 PhantomJS 来实现此目的(请参阅 http://phantomjs.org/screen-capture.html)

从 shell:

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

或者从使用 selenium 的 python:

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')

You can also use PhantomJS for this (see http://phantomjs.org/screen-capture.html)

From a shell:

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

Or from python using 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')
想你只要分分秒秒 2024-09-10 05:39:54

我安装了 inkscape,所以我只是将进程外包给 inkscape 命令 inkscape -f file.svg -e file.png

使用此代码:

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,并收到 Windows 5 错误 [访问被拒绝](或类似的内容),直到我切换到 inkscape 目录

I have inkscape installed so I am just farming out the process to the inkscape command with inkscape -f file.svg -e file.png

Using this code:

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])

I am on windows 7, and got the Windows 5 Error [Access Denied] (or something like that) until I switched to the inkscape directory

提笔落墨 2024-09-10 05:39:54
import pygame

surface = pygame.image.load("shrubbery.svg")
pygame.image.save(surface, "shrubbery.png")

WebP、AVIF 和 JPEG XL 正在取代 PNG。

import pygame

surface = pygame.image.load("shrubbery.svg")
pygame.image.save(surface, "shrubbery.png")

WebP, AVIF, and JPEG XL are replacing PNG.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文