python-lottie  0.7.0+dev418bd09
A framework to work with lottie files and telegram animated stickers (tgs)
dot_lottie.py
Go to the documentation of this file.
1 import json
2 import string
3 import zipfile
4 
5 from .base import exporter
6 from ..parsers.baseporter import ExtraOption
7 from ..parsers.tgs import parse_tgs
8 from lottie import __version__
9 from ..objects import assets
10 
11 
12 @exporter("dotLottie Archive", ["lottie"], [ ExtraOption("id", help="ID of the animation", default=None),
13  ExtraOption("append", help="Append animation to existing archive", action="store_true"),
14  ExtraOption("revision", help="File revision", type=int, default=None),
15  ExtraOption("author", help="File author", default=None),
16  ExtraOption("speed", help="Playback speed", type=float, default=1),
17  ExtraOption("theme_color", help="Theme color", type=str, default="#ffffff"),
18  ExtraOption("no_loop", help="Disable Looping", action="store_false", dest="loop"),
19  ExtraOption("no_pack", help="Don't auto-pack images", action="store_false", dest="pack_images"),
20 ], slug="dotlottie")
21 def export_dotlottie(animation, file, id=None, append=False, revision=None, author=None,
22  speed=1.0, theme_color="#ffffff", loop=True, pack_images=True):
23 
24  files = {}
25 
26  if append:
27  with zipfile.ZipFile(file, "r") as zf:
28  with zf.open("manifest.json") as manifest:
29  meta = json.load(manifest)
30 
31  for name in zf.namelist():
32  if name != "manifest.json":
33  files[name] = zf.read(name)
34  else:
35  meta = {
36  "generator": "Python Lottie " + __version__,
37  "version": 1.0,
38  "revision": 1,
39  "author": "",
40  "animations": [],
41  "custom": {}
42  }
43 
44  if revision is not None:
45  meta["revision"] = revision
46 
47  if author is not None:
48  meta["author"] = author
49 
50  if id is None:
51  if animation.name:
52  idok = string.ascii_letters + string.digits + "_-"
53  id = "".join(filter(lambda x: x in idok, animation.name.replace(" ", "_")))
54  if not id:
55  id = "animation_%s" % len(meta["animations"])
56 
57  meta["animations"].append({
58  "id": id,
59  "speed": speed,
60  "themeColor": theme_color,
61  "loop": loop,
62  })
63 
64  if pack_images and animation.assets:
65  animation = animation.clone()
66  image_no = 0
67  for asset in animation.assets:
68  if isinstance(asset, assets.FileAsset):
69  ext, data = asset.data()
70  if not ext:
71  continue
72  pathname = "images/"
73  while True:
74  basename = "image_%s.%s" % (image_no, ext)
75  image_no += 1
76  if pathname+basename not in files:
77  break
78  files[pathname+basename] = data
79  asset.path = pathname
80  asset.file_name = basename
81  asset.is_embedded = False
82 
83  files["manifest.json"] = json.dumps(meta)
84  files["animations/%s.json" % id] = json.dumps(animation.to_dict())
85 
86  with zipfile.ZipFile(file, "w") as zf:
87  for name, data in files.items():
88  zf.writestr(name, data)
89 
def export_dotlottie(animation, file, id=None, append=False, revision=None, author=None, speed=1.0, theme_color="#ffffff", loop=True, pack_images=True)
Definition: dot_lottie.py:22