python-lottie  0.6.11+devcecd248
A framework to work with lottie files and telegram animated stickers (tgs)
glaxnimate_helpers.py
Go to the documentation of this file.
1 try:
2  import glaxnimate
3  has_glaxnimate = True
4 except ImportError:
5  has_glaxnimate = False
6 
7 import json
8 from ..nvector import NVector, Point
9 
10 
11 def convert(animation, exporter_slug):
12  with glaxnimate.environment.Headless():
13  document = glaxnimate.model.Document("")
14  glaxnimate.io.registry.from_slug("lottie").load(document, json.dumps(animation.to_dict()).encode("utf8"))
15  return glaxnimate.io.registry.from_slug(exporter_slug).save(document)
16 
17 
18 def serialize(animation, serializer_slug, time=0):
19  with glaxnimate.environment.Headless():
20  document = glaxnimate.model.Document("")
21  glaxnimate.io.registry.from_slug("lottie").load(document, json.dumps(animation.to_dict()).encode("utf8"))
22  document.current_time = time
23  return glaxnimate.io.registry.serializer_from_slug(serializer_slug).serialize([document.main])
24 
25 
27  def __init__(self, animation, serializer_slug, dpi):
28  self.context = glaxnimate.environment.Headless()
29  self.serializer_slug = serializer_slug
30  self.serializer = None
31  self.document = None
32  self.animation = animation
33  # TODO dpi
34 
35  def __enter__(self):
36  self.context.__enter__()
37  self.serializer = glaxnimate.io.registry.serializer_from_slug(self.serializer_slug)
38  self.document = glaxnimate.model.Document("")
39  glaxnimate.io.registry.from_slug("lottie").load(self.document, json.dumps(self.animation.to_dict()).encode("utf8"))
40  return self
41 
42  def __exit__(self, *a, **k):
43  self.context.__exit__(*a, **k)
44 
45  def serialize(self, frame, fp):
46  self.document.current_time = frame
47  data = self.serializer.serialize([self.document.main])
48  if fp:
49  fp.write(data)
50  return data
51 
52 
53 def color_to_glaxnimate(color: NVector):
54  return glaxnimate.utils.Color(*(color * 255).components)
55 
56 
58  return NVector(color.red / 255., color.green / 255., color.blue / 255., color.alpha / 255.)
59 
60 
62  return Point(point.x, point.y)
lottie.parsers.glaxnimate_helpers.GlaxnimateRenderer.document
document
Definition: glaxnimate_helpers.py:31
lottie.parsers.glaxnimate_helpers.serialize
def serialize(animation, serializer_slug, time=0)
Definition: glaxnimate_helpers.py:18
lottie.parsers.glaxnimate_helpers.GlaxnimateRenderer.animation
animation
Definition: glaxnimate_helpers.py:32
lottie.parsers.glaxnimate_helpers.GlaxnimateRenderer.serializer
serializer
Definition: glaxnimate_helpers.py:30
lottie.parsers.glaxnimate_helpers.GlaxnimateRenderer.serialize
def serialize(self, frame, fp)
Definition: glaxnimate_helpers.py:45
lottie.parsers.glaxnimate_helpers.GlaxnimateRenderer.__exit__
def __exit__(self, *a, **k)
Definition: glaxnimate_helpers.py:42
lottie.parsers.glaxnimate_helpers.GlaxnimateRenderer.__init__
def __init__(self, animation, serializer_slug, dpi)
Definition: glaxnimate_helpers.py:27
lottie.parsers.glaxnimate_helpers.GlaxnimateRenderer.context
context
Definition: glaxnimate_helpers.py:28
lottie.parsers.glaxnimate_helpers.convert
def convert(animation, exporter_slug)
Definition: glaxnimate_helpers.py:11
lottie.parsers.glaxnimate_helpers.color_to_glaxnimate
def color_to_glaxnimate(NVector color)
Definition: glaxnimate_helpers.py:53
lottie.parsers.glaxnimate_helpers.GlaxnimateRenderer.serializer_slug
serializer_slug
Definition: glaxnimate_helpers.py:29
lottie.parsers.glaxnimate_helpers.GlaxnimateRenderer
Definition: glaxnimate_helpers.py:26
lottie.parsers.glaxnimate_helpers.color_from_glaxnimate
def color_from_glaxnimate(color)
Definition: glaxnimate_helpers.py:57
lottie.nvector.Point
def Point(x, y)
Definition: nvector.py:135
lottie.parsers.glaxnimate_helpers.point_from_glaxnimate
def point_from_glaxnimate(point)
Definition: glaxnimate_helpers.py:61
lottie.parsers.glaxnimate_helpers.GlaxnimateRenderer.__enter__
def __enter__(self)
Definition: glaxnimate_helpers.py:35
lottie.nvector.NVector
Definition: nvector.py:9