python-lottie  0.6.11+deva0e77a1
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.contextcontext = glaxnimate.environment.Headless()
29  self.serializer_slugserializer_slug = serializer_slug
30  self.serializerserializer = None
31  self.documentdocument = None
32  self.animationanimation = animation
33  # TODO dpi
34 
35  def __enter__(self):
36  self.contextcontext.__enter__()
37  self.serializerserializer = glaxnimate.io.registry.serializer_from_slug(self.serializer_slugserializer_slug)
38  self.documentdocument = glaxnimate.model.Document("")
39  glaxnimate.io.registry.from_slug("lottie").load(self.documentdocument, json.dumps(self.animationanimation.to_dict()).encode("utf8"))
40  return self
41 
42  def __exit__(self, *a, **k):
43  self.contextcontext.__exit__(*a, **k)
44 
45  def serialize(self, frame, fp):
46  self.documentdocument.current_time = frame
47  data = self.serializerserializer.serialize([self.documentdocument.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)
def __init__(self, animation, serializer_slug, dpi)
def Point(x, y)
Definition: nvector.py:135
def serialize(animation, serializer_slug, time=0)
def convert(animation, exporter_slug)