python-lottie  0.6.11+devc144cca
A framework to work with lottie files and telegram animated stickers (tgs)
assets.py
Go to the documentation of this file.
1 import os
2 import re
3 import base64
4 import mimetypes
5 from io import BytesIO
6 from .base import LottieObject, LottieProp, PseudoBool, Index
7 from .layers import Layer
8 from .composition import Composition
9 
10 
11 ## @ingroup Lottie
13  _props = [
14  LottieProp("id", "id", str, False),
15  ]
16 
17  @classmethod
18  def _load_get_class(cls, lottiedict):
19  if "p" in lottiedict or "u" in lottiedict:
20  return Image
21  if "layers" in lottiedict:
22  return Precomp
23 
24 
25 ## @ingroup Lottie
26 class Image(Asset):
27  """!
28  External image
29 
30  @see http://docs.aenhancers.com/sources/filesource/
31  """
32  _props = [
33  LottieProp("height", "h", float, False),
34  LottieProp("width", "w", float, False),
35  LottieProp("image", "p", str, False),
36  LottieProp("image_path", "u", str, False),
37  LottieProp("is_embedded", "e", PseudoBool, False),
38  ]
39 
40  @staticmethod
41  def guess_mime(file):
42  if isinstance(file, str):
43  filename = file
44  elif hasattr(file, "name"):
45  filename = file.name
46  else:
47  return "application/octet-stream"
48  return mimetypes.guess_type(filename)
49 
50  def __init__(self, id=""):
51  ## Image Height
52  self.height = 0
53  ## Image Width
54  self.width = 0
55  ## Image ID
56  self.id = id
57  ## Image name
58  self.image = ""
59  ## Image path
60  self.image_path = ""
61  ## Image data is stored as a data: url
62  self.is_embedded = False
63 
64  def load(self, file, format=None):
65  """!
66  @param file Filename, file object, or PIL.Image.Image to load
67  @param format Format to store the image data as
68  """
69  from PIL import Image
70 
71  if not isinstance(file, Image.Image):
72  image = Image.open(file)
73  else:
74  image = file
75 
76  self._id_from_file(file)
77 
78  self.image_path = ""
79  if format is None:
80  format = (image.format or "png").lower()
81  self.width, self.height = image.size
82  output = BytesIO()
83  image.save(output, format=format)
84  self.image = "data:image/%s;base64,%s" % (
85  format,
86  base64.b64encode(output.getvalue()).decode("ascii")
87  )
88  self.is_embedded = True
89  return self
90 
91  def _id_from_file(self, file):
92  if not self.id:
93  if isinstance(file, str):
94  self.id = os.path.basename(file)
95  elif hasattr(file, "name"):
96  self.id = os.path.basename(file.name)
97  elif hasattr(file, "filename"):
98  self.id = os.path.basename(file.filename)
99  else:
100  self.id = "image_%s" % id(self)
101 
102  @classmethod
103  def embedded(cls, image, format=None):
104  """!
105  Create an object from an image file
106  """
107  lottie_image = cls()
108  return lottie_image.load(image, format)
109 
110  @classmethod
111  def linked(cls, filename):
112  from PIL import Image
113  image = Image.open(filename)
114  lottie_image = cls()
115  lottie_image._id_from_file(filename)
116  lottie_image.image_path, lottie_image.image = os.path.split(filename)
117  lottie_image.image_path += "/"
118  lottie_image.width = image.width
119  lottie_image.height = image.height
120  return lottie_image
121 
122  def image_data(self):
123  """
124  Returns a tuple (format, data) with the contents of the image
125 
126  `format` is a string like "png", and `data` is just raw binary data.
127 
128  If it's impossible to fetch this info, returns (None, None)
129  """
130  if self.is_embedded:
131  m = re.match("data:[^/]+/([^;,]+);base64,(.*)", self.image)
132  if m:
133  return m.group(1), base64.b64decode(m.group(2))
134  return None, None
135  path = self.image_path + self.image
136  if os.path.isfile(path):
137  with open(path, "rb") as imgfile:
138  return os.path.splitext(path)[1][1:], imgfile.read()
139  return None, None
140 
141 
142 ## @ingroup Lottie
144  _props = [
145  LottieProp("name", "nm", str, False),
146  LottieProp("frame_rate", "fr", float, False),
147  ]
148 
149  def __init__(self, id="", animation=None):
150  super().__init__()
151  ## Precomp ID
152  self.id = id
153  self.animation = animation
154  if animation:
155  self.animation.assets.append(self)
156  self.name = None
157  self.frame_rate = None
158 
159  def _on_prepare_layer(self, layer):
160  if self.animation:
161  self.animation.prepare_layer(layer)
162 
163  def set_timing(self, outpoint, inpoint=0, override=True):
164  for layer in self.layers:
165  if override or layer.in_point is None:
166  layer.in_point = inpoint
167  if override or layer.out_point is None:
168  layer.out_point = outpoint
lottie.objects.assets.Precomp
Definition: assets.py:143
lottie.objects.assets.Precomp.frame_rate
frame_rate
Definition: assets.py:157
lottie.objects.assets.Image.is_embedded
is_embedded
Image data is stored as a data: url.
Definition: assets.py:62
lottie.objects.assets.Image.embedded
def embedded(cls, image, format=None)
Create an object from an image file.
Definition: assets.py:103
lottie.objects.assets.Image.width
width
Image Width.
Definition: assets.py:54
lottie.objects.assets.Image.linked
def linked(cls, filename)
Definition: assets.py:111
lottie.objects.assets.Image.image_path
image_path
Image path.
Definition: assets.py:60
lottie.objects.base.LottieObject
Base class for mapping Python classes into Lottie JSON objects.
Definition: base.py:225
lottie.objects.assets.Precomp.__init__
def __init__(self, id="", animation=None)
Definition: assets.py:149
lottie.objects.assets.Asset
Definition: assets.py:12
lottie.objects.assets.Image.image
image
Image name.
Definition: assets.py:58
lottie.objects.assets.Image.height
height
Image Height.
Definition: assets.py:52
lottie.objects.assets.Image
External image.
Definition: assets.py:26
lottie.objects.assets.Image.load
def load(self, file, format=None)
Definition: assets.py:64
lottie.objects.assets.Precomp.id
id
Precomp ID.
Definition: assets.py:152
lottie.objects.base.LottieProp
Lottie <-> Python property mapper.
Definition: base.py:88
lottie.objects.composition.Composition
Base class for layer holders.
Definition: composition.py:6
lottie.objects.composition.Composition.layers
layers
List of Composition Layers.
Definition: composition.py:17
lottie.objects.assets.Image.__init__
def __init__(self, id="")
Definition: assets.py:50
lottie.objects.assets.Image.guess_mime
def guess_mime(file)
Definition: assets.py:41
lottie.objects.assets.Precomp.name
name
Definition: assets.py:156
lottie.objects.composition.Composition.prepare_layer
def prepare_layer(self, Layer layer)
Definition: composition.py:53
lottie.objects.assets.Image.id
id
Image ID.
Definition: assets.py:56
lottie.objects.assets.Image._id_from_file
def _id_from_file(self, file)
Definition: assets.py:91
lottie.objects.assets.Precomp.set_timing
def set_timing(self, outpoint, inpoint=0, override=True)
Definition: assets.py:163
lottie.objects.assets.Precomp.animation
animation
Definition: assets.py:153
lottie.objects.assets.Image.image_data
def image_data(self)
Definition: assets.py:122