python中的服务器端SVG到PNG(或其他图像格式) [英] Server-side SVG to PNG (or some other image format) in python
本文介绍了python中的服务器端SVG到PNG(或其他图像格式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当前,我正在使用rsvg加载svg(从字符串而不是从文件)并绘制到cairo.有人知道更好的方法吗?我在应用程序的其他地方使用了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.
推荐答案
这是我目前拥有的:
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)
这篇关于python中的服务器端SVG到PNG(或其他图像格式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文