python-lottie  0.6.10+devbd5499b
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 .shapes import ShapeElement
9 from .composition import Composition
10 
11 
12 ## @ingroup Lottie
14  @classmethod
15  def _load_get_class(cls, lottiedict):
16  if "p" in lottiedict or "u" in lottiedict:
17  return Image
18  if "layers" in lottiedict:
19  return Precomp
20 
21 
22 ## @ingroup Lottie
23 class Image(Asset):
24  """!
25  External image
26 
27  @see http://docs.aenhancers.com/sources/filesource/
28  """
29  _props = [
30  LottieProp("height", "h", float, False),
31  LottieProp("width", "w", float, False),
32  LottieProp("id", "id", str, False),
33  LottieProp("image", "p", str, False),
34  LottieProp("image_path", "u", str, False),
35  LottieProp("is_embedded", "e", PseudoBool, False),
36  ]
37 
38  @staticmethod
39  def guess_mime(file):
40  if isinstance(file, str):
41  filename = file
42  elif hasattr(file, "name"):
43  filename = file.name
44  else:
45  return "application/octet-stream"
46  return mimetypes.guess_type(filename)
47 
48  def __init__(self, id=""):
49  ## Image Height
50  self.height = 0
51  ## Image Width
52  self.width = 0
53  ## Image ID
54  self.id = id
55  ## Image name
56  self.image = ""
57  ## Image path
58  self.image_path = ""
59  ## Image data is stored as a data: url
60  self.is_embedded = False
61 
62  def load(self, file, format=None):
63  """!
64  @param file Filename, file object, or PIL.Image.Image to load
65  @param format Format to store the image data as
66  """
67  from PIL import Image
68 
69  if not isinstance(file, Image.Image):
70  image = Image.open(file)
71  else:
72  image = file
73 
74  self._id_from_file(file)
75 
76  self.image_path = ""
77  if format is None:
78  format = (image.format or "png").lower()
79  self.width, self.height = image.size
80  output = BytesIO()
81  image.save(output, format=format)
82  self.image = "data:image/%s;base64,%s" % (
83  format,
84  base64.b64encode(output.getvalue()).decode("ascii")
85  )
86  self.is_embedded = True
87  return self
88 
89  def _id_from_file(self, file):
90  if not self.id:
91  if isinstance(file, str):
92  self.id = os.path.basename(file)
93  elif hasattr(file, "name"):
94  self.id = os.path.basename(file.name)
95  elif hasattr(file, "filename"):
96  self.id = os.path.basename(file.filename)
97  else:
98  self.id = "image_%s" % id(self)
99 
100  @classmethod
101  def embedded(cls, image, format=None):
102  """!
103  Create an object from an image file
104  """
105  lottie_image = cls()
106  return lottie_image.load(image, format)
107 
108  @classmethod
109  def linked(cls, filename):
110  from PIL import Image
111  image = Image.open(filename)
112  lottie_image = cls()
113  lottie_image._id_from_file(filename)
114  lottie_image.image_path, lottie_image.image = os.path.split(filename)
115  lottie_image.image_path += "/"
116  lottie_image.width = image.width
117  lottie_image.height = image.height
118  return lottie_image
119 
120  def image_data(self):
121  """
122  Returns a tuple (format, data) with the contents of the image
123 
124  `format` is a string like "png", and `data` is just raw binary data.
125 
126  If it's impossible to fetch this info, returns (None, None)
127  """
128  if self.is_embedded:
129  m = re.match("data:[^/]+/([^;,]+);base64,(.*)", self.image)
130  if m:
131  return m.group(1), base64.b64decode(m.group(2))
132  return None, None
133  path = self.image_path + self.image
134  if os.path.isfile(path):
135  with open(path, "rb") as imgfile:
136  return os.path.splitext(path)[1][1:], imgfile.read()
137  return None, None
138 
139 
140 ## @ingroup Lottie
142  """!
143  Character shapes
144  """
145  _props = [
146  LottieProp("shapes", "shapes", ShapeElement, True),
147  ]
148 
149  def __init__(self):
150  self.shapes = []
151 
152 
153 ## @ingroup Lottie
155  """!
156  Defines character shapes to avoid loading system fonts
157  """
158  _props = [
159  LottieProp("character", "ch", str, False),
160  LottieProp("font_family", "fFamily", str, False),
161  LottieProp("font_size", "size", float, False),
162  LottieProp("font_style", "style", str, False),
163  LottieProp("width", "w", float, False),
164  LottieProp("data", "data", CharacterData, False),
165  ]
166 
167  def __init__(self):
168  ## Character Value
169  self.character = ""
170  ## Character Font Family
171  self.font_family = ""
172  ## Character Font Size
173  self.font_size = 0
174  ## Character Font Style
175  self.font_style = "" # Regular
176  ## Character Width
177  self.width = 0
178  ## Character Data
180 
181  @property
182  def shapes(self):
183  return self.data.shapes
184 
185 
186 ## @ingroup Lottie
188  _props = [
189  LottieProp("id", "id", str, False),
190  ]
191 
192  def __init__(self, id="", animation=None):
193  super().__init__()
194  ## Precomp ID
195  self.id = id
196  self.animation = animation
197  if animation:
198  self.animation.assets.append(self)
199 
200  def _on_prepare_layer(self, layer):
201  if self.animation:
202  self.animation.prepare_layer(layer)
203 
204  def set_timing(self, outpoint, inpoint=0, override=True):
205  for layer in self.layers:
206  if override or layer.in_point is None:
207  layer.in_point = inpoint
208  if override or layer.out_point is None:
209  layer.out_point = outpoint
lottie.objects.assets.CharacterData.shapes
shapes
Definition: assets.py:150
lottie.objects.assets.Precomp
Definition: assets.py:187
lottie.objects.assets.Chars.font_size
font_size
Character Font Size.
Definition: assets.py:173
lottie.objects.assets.Chars
Defines character shapes to avoid loading system fonts.
Definition: assets.py:154
lottie.objects.assets.Image.is_embedded
is_embedded
Image data is stored as a data: url.
Definition: assets.py:60
lottie.objects.assets.Chars.font_family
font_family
Character Font Family.
Definition: assets.py:171
lottie.objects.assets.Image.embedded
def embedded(cls, image, format=None)
Create an object from an image file.
Definition: assets.py:101
lottie.objects.assets.Image.width
width
Image Width.
Definition: assets.py:52
lottie.objects.assets.Image.linked
def linked(cls, filename)
Definition: assets.py:109
lottie.objects.assets.Image.image_path
image_path
Image path.
Definition: assets.py:58
lottie.objects.assets.Chars.data
data
Character Data.
Definition: assets.py:179
lottie.objects.base.LottieObject
Base class for mapping Python classes into Lottie JSON objects.
Definition: base.py:224
lottie.objects.assets.Precomp.__init__
def __init__(self, id="", animation=None)
Definition: assets.py:192
lottie.objects.assets.Asset
Definition: assets.py:13
lottie.objects.assets.Chars.shapes
def shapes(self)
Definition: assets.py:182
lottie.objects.assets.Image.image
image
Image name.
Definition: assets.py:56
lottie.objects.assets.Image.height
height
Image Height.
Definition: assets.py:50
lottie.objects.assets.Image
External image.
Definition: assets.py:23
lottie.objects.assets.Image.load
def load(self, file, format=None)
Definition: assets.py:62
lottie.objects.assets.Precomp.id
id
Precomp ID.
Definition: assets.py:195
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:16
lottie.objects.assets.Image.__init__
def __init__(self, id="")
Definition: assets.py:48
lottie.objects.assets.Image.guess_mime
def guess_mime(file)
Definition: assets.py:39
lottie.objects.composition.Composition.prepare_layer
def prepare_layer(self, Layer layer)
Definition: composition.py:52
lottie.objects.assets.Image.id
id
Image ID.
Definition: assets.py:54
lottie.objects.assets.Image._id_from_file
def _id_from_file(self, file)
Definition: assets.py:89
lottie.objects.assets.Chars.font_style
font_style
Character Font Style.
Definition: assets.py:175
lottie.objects.assets.Chars.character
character
Character Value.
Definition: assets.py:169
lottie.objects.assets.Precomp.set_timing
def set_timing(self, outpoint, inpoint=0, override=True)
Definition: assets.py:204
lottie.objects.assets.Chars.__init__
def __init__(self)
Definition: assets.py:167
lottie.objects.assets.Chars.width
width
Character Width.
Definition: assets.py:177
lottie.objects.assets.Precomp.animation
animation
Definition: assets.py:196
lottie.objects.assets.Image.image_data
def image_data(self)
Definition: assets.py:120
lottie.objects.assets.CharacterData
Character shapes.
Definition: assets.py:141
lottie.objects.assets.CharacterData.__init__
def __init__(self)
Definition: assets.py:149