本文整理汇总了Python中cairocffi.SVGSurface方法的典型用法代码示例。如果您正苦于以下问题:Python cairocffi.SVGSurface方法的具体用法?Python cairocffi.SVGSurface怎么用?Python cairocffi.SVGSurface使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cairocffi
的用法示例。
在下文中一共展示了cairocffi.SVGSurface方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setupCairo
# 需要导入模块: import cairocffi [as 别名]
# 或者: from cairocffi import SVGSurface [as 别名]
def setupCairo(self, outputFormat='png'):
self.outputFormat = outputFormat
if outputFormat == 'png':
self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
self.width, self.height)
elif outputFormat == 'svg':
self.surfaceData = BytesIO()
self.surface = cairo.SVGSurface(self.surfaceData,
self.width, self.height)
elif outputFormat == 'pdf':
self.surfaceData = BytesIO()
self.surface = cairo.PDFSurface(self.surfaceData,
self.width, self.height)
res_x, res_y = self.surface.get_fallback_resolution()
self.width = float(self.width / res_x) * 72
self.height = float(self.height / res_y) * 72
self.surface.set_size(self.width, self.height)
self.ctx = cairo.Context(self.surface)
示例2: _save
# 需要导入模块: import cairocffi [as 别名]
# 或者: from cairocffi import SVGSurface [as 别名]
def _save(self, fo, fmt, **kwargs):
# save PDF/PS/SVG
orientation = kwargs.get('orientation', 'portrait')
dpi = 72
self.figure.dpi = dpi
w_in, h_in = self.figure.get_size_inches()
width_in_points, height_in_points = w_in * dpi, h_in * dpi
if orientation == 'landscape':
width_in_points, height_in_points = (
height_in_points, width_in_points)
if fmt == 'ps':
if not hasattr(cairo, 'PSSurface'):
raise RuntimeError('cairo has not been compiled with PS '
'support enabled')
surface = cairo.PSSurface(fo, width_in_points, height_in_points)
elif fmt == 'pdf':
if not hasattr(cairo, 'PDFSurface'):
raise RuntimeError('cairo has not been compiled with PDF '
'support enabled')
surface = cairo.PDFSurface(fo, width_in_points, height_in_points)
elif fmt in ('svg', 'svgz'):
if not hasattr(cairo, 'SVGSurface'):
raise RuntimeError('cairo has not been compiled with SVG '
'support enabled')
if fmt == 'svgz':
if isinstance(fo, str):
fo = gzip.GzipFile(fo, 'wb')
else:
fo = gzip.GzipFile(None, 'wb', fileobj=fo)
surface = cairo.SVGSurface(fo, width_in_points, height_in_points)
else:
raise ValueError("Unknown format: {!r}".format(fmt))
# surface.set_dpi() can be used
renderer = RendererCairo(self.figure.dpi)
renderer.set_width_height(width_in_points, height_in_points)
renderer.set_ctx_from_surface(surface)
ctx = renderer.gc.ctx
if orientation == 'landscape':
ctx.rotate(np.pi / 2)
ctx.translate(0, -height_in_points)
# Perhaps add an '%%Orientation: Landscape' comment?
self.figure.draw(renderer)
ctx.show_page()
surface.finish()
if fmt == 'svgz':
fo.close()
示例3: _save
# 需要导入模块: import cairocffi [as 别名]
# 或者: from cairocffi import SVGSurface [as 别名]
def _save(self, fo, fmt, **kwargs):
# save PDF/PS/SVG
orientation = kwargs.get('orientation', 'portrait')
dpi = 72
self.figure.dpi = dpi
w_in, h_in = self.figure.get_size_inches()
width_in_points, height_in_points = w_in * dpi, h_in * dpi
if orientation == 'landscape':
width_in_points, height_in_points = (
height_in_points, width_in_points)
if fmt == 'ps':
if not hasattr(cairo, 'PSSurface'):
raise RuntimeError('cairo has not been compiled with PS '
'support enabled')
surface = cairo.PSSurface(fo, width_in_points, height_in_points)
elif fmt == 'pdf':
if not hasattr(cairo, 'PDFSurface'):
raise RuntimeError('cairo has not been compiled with PDF '
'support enabled')
surface = cairo.PDFSurface(fo, width_in_points, height_in_points)
elif fmt in ('svg', 'svgz'):
if not hasattr(cairo, 'SVGSurface'):
raise RuntimeError('cairo has not been compiled with SVG '
'support enabled')
if fmt == 'svgz':
if isinstance(fo, str):
fo = gzip.GzipFile(fo, 'wb')
else:
fo = gzip.GzipFile(None, 'wb', fileobj=fo)
surface = cairo.SVGSurface(fo, width_in_points, height_in_points)
else:
warnings.warn("unknown format: %s" % fmt, stacklevel=2)
return
# surface.set_dpi() can be used
renderer = RendererCairo(self.figure.dpi)
renderer.set_width_height(width_in_points, height_in_points)
renderer.set_ctx_from_surface(surface)
ctx = renderer.gc.ctx
if orientation == 'landscape':
ctx.rotate(np.pi / 2)
ctx.translate(0, -height_in_points)
# Perhaps add an '%%Orientation: Landscape' comment?
self.figure.draw(renderer)
ctx.show_page()
surface.finish()
if fmt == 'svgz':
fo.close()
示例4: render
# 需要导入模块: import cairocffi [as 别名]
# 或者: from cairocffi import SVGSurface [as 别名]
def render(self, filename=None):
# render the image to png. returns bytes if f is None, otherwise it calls f.write()
if self.width == 256 and self.height == 256 and not self.g:
# create empty tile png with minimal size, indexed color palette with only one entry
plte = b'PLTE' + bytearray(tuple(int(i*255) for i in self.background_rgb))
return (b'\x89PNG\r\n\x1a\n' +
b'\x00\x00\x00\rIHDR\x00\x00\x01\x00\x00\x00\x01\x00\x01\x03\x00\x00\x00f\xbc:%\x00\x00\x00\x03' +
plte + zlib.crc32(plte).to_bytes(4, byteorder='big') +
b'\x00\x00\x00\x1fIDATh\xde\xed\xc1\x01\r\x00\x00\x00\xc2\xa0\xf7Om\x0e7\xa0\x00\x00\x00\x00\x00' +
b'\x00\x00\x00\xbe\r!\x00\x00\x01\x7f\x19\x9c\xa7\x00\x00\x00\x00IEND\xaeB`\x82')
if settings.SVG_RENDERER == 'rsvg':
# create buffered surfaces
buffered_surface = cairocffi.SVGSurface(None, self.buffered_width, self.buffered_height)
buffered_context = cairocffi.Context(buffered_surface)
# draw svg with rsvg
handle = Rsvg.Handle()
svg = handle.new_from_data(self.get_xml(buffer=True).encode())
svg.render_cairo(buffered_context)
# create cropped image
surface = buffered_surface.create_similar(cairocffi.CONTENT_COLOR, self.width, self.height)
context = cairocffi.Context(surface)
# set background color
context.set_source(cairocffi.SolidPattern(*self.background_rgb))
context.paint()
# paste buffered immage with offset
context.set_source_surface(buffered_surface, -self.buffer, -self.buffer)
context.paint()
return surface.write_to_png()
elif settings.SVG_RENDERER == 'rsvg-convert':
p = subprocess.run(('rsvg-convert', '-b', self.background, '--format', 'png'),
input=self.get_xml(buffer=True).encode(), stdout=subprocess.PIPE, check=True)
png = io.BytesIO(p.stdout)
img = Image.open(png)
img = img.crop((self.buffer, self.buffer,
self.buffer + self.width,
self.buffer + self.height))
f = io.BytesIO()
img.save(f, 'PNG')
f.seek(0)
return f.read()
elif settings.SVG_RENDERER == 'inkscape':
p = subprocess.run(('inkscape', '-z', '-b', self.background, '-e', '/dev/stderr', '/dev/stdin'),
input=self.get_xml().encode(), stdout=subprocess.PIPE, stderr=subprocess.PIPE,
check=True)
png = p.stderr[p.stderr.index(b'\x89PNG'):]
return png
示例5: __init__
# 需要导入模块: import cairocffi [as 别名]
# 或者: from cairocffi import SVGSurface [as 别名]
def __init__(self,
img_format="png",
output="page",
width=0.15,
height=0.15,
dots_per_inch=dots_per_inch):
"""
A thin wrapper to produce vector graphics using cairo. This class represents a page / image file we are going
to draw onto.
:param img_format:
The image format we are to produce.
:type img_format:
str
:param output:
The filename of the image file we are to produce, without a file type suffix
:type output:
str
:param width:
The width of the page, metres
:type width:
float
:param height:
The height of the page, metres
:type height:
float
:param dots_per_inch:
The dots per inch resolution to render this page
:type dots_per_inch:
float
"""
# PDF surfaces are always measured in points
if img_format in ("pdf", "svg"):
dots_per_inch = 72.
self.format = img_format
self.output = "{}.{}".format(output, img_format)
self.dots_per_metre = dots_per_inch * 39.370079
self.width = int(width * self.dots_per_metre)
self.height = int(height * self.dots_per_metre)
if self.format == "pdf":
self.surface = cairo.PDFSurface(self.output, self.width, self.height)
elif self.format == "png":
self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, self.width, self.height)
elif self.format == "svg":
self.surface = cairo.SVGSurface(self.output, self.width, self.height)
else:
assert False, "Unknown image output format {}".format(self.format)
示例6: _save
# 需要导入模块: import cairocffi [as 别名]
# 或者: from cairocffi import SVGSurface [as 别名]
def _save(self, fo, fmt, **kwargs):
# save PDF/PS/SVG
orientation = kwargs.get('orientation', 'portrait')
dpi = 72
self.figure.dpi = dpi
w_in, h_in = self.figure.get_size_inches()
width_in_points, height_in_points = w_in * dpi, h_in * dpi
if orientation == 'landscape':
width_in_points, height_in_points = (
height_in_points, width_in_points)
if fmt == 'ps':
if not hasattr(cairo, 'PSSurface'):
raise RuntimeError('cairo has not been compiled with PS '
'support enabled')
surface = cairo.PSSurface(fo, width_in_points, height_in_points)
elif fmt == 'pdf':
if not hasattr(cairo, 'PDFSurface'):
raise RuntimeError('cairo has not been compiled with PDF '
'support enabled')
surface = cairo.PDFSurface(fo, width_in_points, height_in_points)
elif fmt in ('svg', 'svgz'):
if not hasattr(cairo, 'SVGSurface'):
raise RuntimeError('cairo has not been compiled with SVG '
'support enabled')
if fmt == 'svgz':
if isinstance(fo, six.string_types):
fo = gzip.GzipFile(fo, 'wb')
else:
fo = gzip.GzipFile(None, 'wb', fileobj=fo)
surface = cairo.SVGSurface(fo, width_in_points, height_in_points)
else:
warnings.warn("unknown format: %s" % fmt)
return
# surface.set_dpi() can be used
renderer = RendererCairo(self.figure.dpi)
renderer.set_width_height(width_in_points, height_in_points)
renderer.set_ctx_from_surface(surface)
ctx = renderer.gc.ctx
if orientation == 'landscape':
ctx.rotate(np.pi / 2)
ctx.translate(0, -height_in_points)
# Perhaps add an '%%Orientation: Landscape' comment?
self.figure.draw(renderer)
ctx.show_page()
surface.finish()
if fmt == 'svgz':
fo.close()