python-lottie  0.6.11+devcecd248
A framework to work with lottie files and telegram animated stickers (tgs)
cairo.py
Go to the documentation of this file.
1 try:
2  import cairosvg
3  has_cairo = True
4 except (ImportError, OSError):
5  has_cairo = False
6 
7 from ..parsers import glaxnimate_helpers
8 
9 import io
10 
11 from .base import exporter
12 from .svg import export_svg
13 
14 
15 if glaxnimate_helpers.has_glaxnimate:
16  @exporter("PNG", ["png"], [], {"frame"})
17  def export_png(animation, fp, frame=0, dpi=96):
18  data = glaxnimate_helpers.serialize(animation, "raster")
19 
20  if isinstance(fp, str):
21  with open(fp, 'wb') as file:
22  file.write(data)
23  else:
24  fp.write(data)
25 
26  def PngRenderer(animation, dpi):
27  return glaxnimate_helpers.GlaxnimateRenderer(animation, "raster", dpi)
28 
29 elif has_cairo:
30  def _export_cairo(func, animation, fp, frame, dpi):
31  intermediate = io.BytesIO()
32  export_svg(animation, intermediate, frame, pretty=False)
33  intermediate.seek(0)
34  func(file_obj=intermediate, write_to=fp, dpi=dpi)
35 
36  @exporter("PNG", ["png"], [], {"frame"})
37  def export_png(animation, fp, frame=0, dpi=96):
38  _export_cairo(cairosvg.svg2png, animation, fp, frame, dpi)
39 
40  class PngRenderer:
41  def __init__(self, animation, dpi):
42  self.animation = animation
43  self.dpi = dpi
44 
45  def __enter__(self):
46  return self
47 
48  def __exit__(self, *a, **k):
49  return
50 
51  def serialize(self, frame, file):
52  export_png(self.animation, file, frame, self.dpi)
53 
54  @exporter("PDF", ["pdf"], [], {"frame"})
55  def export_pdf(animation, fp, frame=0, dpi=96):
56  _export_cairo(cairosvg.svg2pdf, animation, fp, frame, dpi)
57 
58  @exporter("PostScript", ["ps"], [], {"frame"})
59  def export_ps(animation, fp, frame=0, dpi=96):
60  _export_cairo(cairosvg.svg2ps, animation, fp, frame, dpi)
lottie.exporters.cairo.PngRenderer.dpi
dpi
Definition: cairo.py:43
lottie.exporters.cairo.PngRenderer.serialize
def serialize(self, frame, file)
Definition: cairo.py:51
lottie.exporters.cairo.PngRenderer
def PngRenderer(animation, dpi)
Definition: cairo.py:26
lottie.exporters.cairo.PngRenderer.__init__
def __init__(self, animation, dpi)
Definition: cairo.py:41
lottie.exporters.cairo.PngRenderer.__enter__
def __enter__(self)
Definition: cairo.py:45
lottie.exporters.cairo.PngRenderer.animation
animation
Definition: cairo.py:42
lottie.exporters.base.exporter
exporter
Definition: base.py:32
lottie.exporters.cairo.export_pdf
def export_pdf(animation, fp, frame=0, dpi=96)
Definition: cairo.py:55
lottie.exporters.cairo.export_ps
def export_ps(animation, fp, frame=0, dpi=96)
Definition: cairo.py:59
lottie.exporters.svg.export_svg
def export_svg(animation, file, frame=0, pretty=True, animated=False)
Definition: svg.py:23
lottie.exporters.cairo.PngRenderer.__exit__
def __exit__(self, *a, **k)
Definition: cairo.py:48
lottie.exporters.cairo.export_png
def export_png(animation, fp, frame=0, dpi=96)
Definition: cairo.py:17
lottie.exporters.cairo.PngRenderer
Definition: cairo.py:40