A Python framework to work with Lottie files and Telegram animated stickers.
(Repo forked from https://gitlab.com/mattbas/python-lottie.git)
This section describes some common things you might want to do without having to read the whole README
pip install lottie
This package provide the script lottie_convert.py
, it's precise location
depends on how you installed python-lottie.
For PNG, GIF, and Webp you have to install cairosvg
and pillow
.
To render a still image:
lottie_convert.py input_file.json output_file.png --frame 30
To render an animated image (GIF or WebP):
lottie_convert.py input_file.json output_file.webp
A list of supported formats is described in the section "Supported Formats" below.
The lottie format is for vector graphics, this means converting raster images usually doesn't work too well.
That said, python-lottie does support a few different algorithms to import raster images, the process is a bit slow for larger images but use it with caution.
To use the potrace vectorization library, install the extras tagged as "trace".
Once set up, just invoke lottie_convert.py
using the vectorization algorithm
lottie_convert.py input_file.gif output_file.json --bmp-mode trace
For pixel art, you can use the pizel
algorithm, which doesn't require potrace
lottie_convert.py input_file.gif output_file.json --bmp-mode pixel
If you are ok with keeping raster images as such, you can use the default mode
lottie_convert.py input_file.gif output_file.json
This format is natively supported by python lottie, but telegram doesn't support all of the features supported by lottie (see the section labeled "Supported After Effects Features" for details).
When converting from tgs, nothing special is needed as it's handled as a lottie animation.
lottie_convert.py AnimatedSticker.tgs output_file.webp
But when converting into animated stickers, you might end up with a file that
Telegram doesn't recognize. To help with this, by default lottie_convert.py
will scale the animation to be the right size and framerate.
It will also print out any warnings related to unsupported features.
Everything else works like any other conversion:
lottie_convert.py input_file.json output_file.tgs
If you want to see the same warnings for an existing tgs file use tgs_check.py
tgs_check.py AnimatedSticker.tgs
See the examples at https://mattbas.gitlab.io/python-lottie/examples.html and read the available lottie objects at https://mattbas.gitlab.io/python-lottie/group__Lottie.html#details
Here is a list of features of the lottie python framework:
- Loading compressed TGS and uncompressed lottie JSON
- Manipulation of lottie objects
- Simple animation presets (eg: shake, linear bounce)
- Bezier path animations (eg: follow path, making paths appear and disappear)
- Wave distortion animation (eg: for flags)
- Pseudo-3D rotations
- Animation easing functions
- Inverse Kinematic solver
- Pretty printing and comparison of lottie files
- Rendering text as shapes
Format | Import | Import Animated | Export | Export Animated |
---|---|---|---|---|
lottie | π | π | π | π |
tgs | π | π | π | π |
SVG | π | π | π | βοΈ |
SVGz | π | π | π | βοΈ |
PNG | π | π1 | π | βοΈ |
Synfig | π | π | π | π |
WebP | π | π | π | π |
dotLottie | π | π | π | π |
PostScript | βοΈ | βοΈ | π | βοΈ |
βοΈ | βοΈ | π | βοΈ | |
BMP | π | π1 | βοΈ | βοΈ |
GIF | π | π | π | π |
TIFF | π | π | π | π |
MP4 | βοΈ | βοΈ | π | π |
AVI | βοΈ | βοΈ | π | π |
WebM | βοΈ | βοΈ | π | π |
HTML | βοΈ | βοΈ | π | π |
Blender | π2 | π2 | βοΈ | βοΈ |
Krita | π | βοΈ | βοΈ | βοΈ |
python-lottie provides several scripts to convert or manage lottie animations. For full documentation see https://mattbas.gitlab.io/python-lottie/scripts.html
The main one is lottie_convert.py
, which can be used to convert between the supported formats.
There is also the script lottie_gui.py
which provides a graphical interface for lottie playback.
The packages for the various applications listed below can be downloaded from here:
There's a Synfig studio plugin to export telegram stickers. To install, just copy (or symlink) ./addons/synfig/pylot-exporter into the synfig plugin directory. You might have to copy ./lib/lottie in there as well.
There are some import/export extensions for inkscape.
Just copy (or symlink) the files under ./addons/inkscape to the inkscape extension directory. On my system that's ~/.config/inkscape/extensions/ but you can double check from Inkscape: Edit > Preferences... > System > User extensions
Note that the extensions require Python 3.
If they are run with a python 2 interpreter, they will try to run themselves using python3
.
They also need the lottie framework to be in the python path, otherwise you can manually set the path on the import/export dialogues.
See also https://inkscape.org/~mattia.basaglia/%E2%98%85tgslottie-importexport
There are some export addons for blender.
Copy (or symlink) the files under ./addons/blender to the Blender extension directory.
On my system that's ~/.config/blender/2.80/scripts/addons/ you can check available paths through the Blender Python console:
import addon_utils; print(addon_utils.paths())
You can also install the addon from Blender using the zipfile created by make
.
You can install from pypi:
pip install lottie
from git:
pip install git+https://gitlab.com/mattbas/python-lottie.git@master
for the source directory:
pip install /path/to/the/sources # this is the path where setup.py is located
Python 3.
In order to provide lean installations, this framework doesn't have dependencies for its core functionality.
To add support for extra formats or advanced functionality, you can install additional packages.
These requirements are declared as extra in the Pypi package, follows a table listing dependencies and features
Packages | Extra | Feature |
---|---|---|
pillow |
images | To load image assets |
cairosvg |
PNG | To export PNG / PDF / PS |
cairosvg , pillow |
GIF | To export GIF and animated WebP |
fonttools |
text | To render text as shapes |
grapheme |
emoji | Adding emoji support to text rendering |
cairosvg , numpy , Python OpenCV 2 |
video | To export video |
pillow , pypotrace>=0.2 , numpy , scipy |
trace | To convert raster images into vectors |
QScintilla |
GUI | Grafical user interface utilities |
coverage |
To show unit test coverage, used optionally by test.sh |
If intalling from pip, you can install optional requirements like so:
pip install lottie[GIF]
The above example will ensure cairosvg
and pillow
are installed.
For more details see https://pip.pypa.io/en/latest/reference/pip_install/#examples.
For convenience, an additional extra requirements is defined, so you can install all dependencies at once:
pip install lottie[all]
If you are using python-lottie from source you can run
pip install -r requirements.txt
Which will install all the requirements (except for pypotrace, as that package has some issues)
I had to reverse engineer the format because Telegram couldn't be bothered providing the specs.
A TGS file is a gzip compressed JSON, the JSON data is described here: https://mattbas.gitlab.io/python-lottie/group__Lottie.html#lottie_json
If you can get the source image into lottie format, that's 90% of the work done.
I've created Python classes based the format schema and after effects documentation, which output the correct json. Eg:
foo = lottie.Animation()
# ...
json.dump(foo.to_dict(), output_file)
I'm also creating a proper documentation for the format, see: https://mattbas.gitlab.io/tgs/group__Lottie.html#details
Nothing major, just ensure the root JSON object has tgs: 1
The tgs file is the JSON described above compressed into a gzip, and renamed to .tgs
AGPLv3+ https://www.gnu.org/licenses/agpl-3.0.en.html
Copyright 2019 (C) Mattia Basaglia
https://mattbas.gitlab.io/python-lottie/index.html
https://gitlab.com/mattbas/python-lottie/
https://mattbas.gitlab.io/python-lottie/downloads.html
Here you can download packages for pip, blender, and inkscape before they are released. These packages always have the latest features but they might be unstable.
You can report any issue in the tracker on gitlab:
https://gitlab.com/mattbas/python-lottie/-/issues
Compare with http://airbnb.io/lottie/#/supported-features
- π Supported
- β Unknown / untested
- βοΈ Not supported
- python-lottie refers to this framework in general
- Telegram refers to features supported by telegram animated stickers
- SVG refers to the exported SVG images from this framework, features supported here will also reflect on other formats (such as video, png, and similar)
Telegram doesn't support everything in the Lottie format. https://core.telegram.org/animated_stickers lists some things that are unsupported but what is listed there isn't correct.
There are several things marked as unsupported in telegram animated stickers that are actually supported.
Shapes | python-lottie | Telegram | SVG |
---|---|---|---|
Shape | π | π | π |
Ellipse | π | π | π |
Rectangle | π | π | π |
Rounded Rectangle | π | π | π |
Polystar | π | π3 | π |
Group | π | π | π |
Trim Path (individually) | π | π | π |
Trim Path (simultaneously) | π | π | π |
Fills | python-lottie | Telegram | SVG |
Color | π | π | π |
Opacity | π | π | π |
Radial Gradient | π | π | π |
Linear Gradient | π | π | π |
Fill Rule | π | π | π |
Strokes | python-lottie | Telegram | SVG |
Color | π | π | π |
Opacity | π | π | π |
Width | π | π | π |
Line Cap | π | π | π |
Line Join | π | π | π |
Miter Limit | π | π | π |
Dashes | π | π | π |
Gradient | π | π3 | π |
Transforms | python-lottie | Telegram | SVG |
Position | π | π | π |
Position (separated X/Y) | π | π | π |
Scale | π | π | π |
Rotation | π | π | π |
Anchor Point | π | π | π |
Opacity | π | π | π |
Parenting | π | π | π |
Skew | π | βοΈ4 | π |
Auto Orient | π | π3 | π |
Interpolation | python-lottie | Telegram | SVG |
Linear Interpolation | π | π | π |
Bezier Interpolation | π | π | π |
Hold Interpolation | π | π | π |
Spatial Bezier Interpolation | π | π | π |
Rove Across Time | βοΈ | βοΈ5 | βοΈ |
Masks | python-lottie | Telegram | SVG |
Mask Path | π | π3 | π |
Mask Opacity | π | π3 | π |
Add | π | π3 | βοΈ |
Subtract | π | π3 | βοΈ |
Intersect | π | π3 | π |
Lighten | π | π3 | βοΈ |
Darken | π | π3 | βοΈ |
Difference | π | π3 | βοΈ |
Expansion | π | π3 | βοΈ |
Feather | π | π3 | βοΈ |
Mattes | python-lottie | Telegram | SVG |
Alpha Matte | π | βοΈ6 | π |
Alpha Inverted Matte | π | βοΈ6 | βοΈ |
Luma Matte | π | βοΈ6 | π |
Luma Inverted Matte | π | βοΈ6 | βοΈ |
Merge Paths | python-lottie | Telegram | SVG |
Merge | βοΈ | βοΈ5 | βοΈ |
Add | βοΈ | βοΈ5 | βοΈ |
Subtract | βοΈ | βοΈ5 | βοΈ |
Intersect | βοΈ | βοΈ5 | βοΈ |
Exclude Intersection | βοΈ | βοΈ5 | βοΈ |
Layer Effects | python-lottie | Telegram | SVG |
Fill | π | βοΈ | βοΈ |
Stroke | π | βοΈ | βοΈ |
Tint | π | βοΈ | βοΈ |
Tritone | π | βοΈ | βοΈ |
Levels Individual Controls | π | βοΈ | βοΈ |
Text 7 | python-lottie | Telegram | SVG |
Glyphs | π | βοΈ | βοΈ |
Fonts | π | βοΈ | βοΈ |
Transform | π | βοΈ | βοΈ |
Fill | π | βοΈ | π |
Stroke | π | βοΈ | βοΈ |
Tracking | βοΈ | βοΈ | βοΈ |
Anchor point grouping | βοΈ | βοΈ | βοΈ |
Text Path | βοΈ | βοΈ | βοΈ |
Per-character 3D | βοΈ | βοΈ | βοΈ |
Range selector (Units) | βοΈ | βοΈ | βοΈ |
Range selector (Based on) | βοΈ | βοΈ | βοΈ |
Range selector (Amount) | βοΈ | βοΈ | βοΈ |
Range selector (Shape) | βοΈ | βοΈ | βοΈ |
Range selector (Ease High) | βοΈ | βοΈ | βοΈ |
Range selector (Ease Low) | βοΈ | βοΈ | βοΈ |
Range selector (Randomize order) | βοΈ | βοΈ | βοΈ |
expression selector | βοΈ | βοΈ | βοΈ |
Other | python-lottie | Telegram | SVG |
Expressions | βοΈ | βοΈ5 | βοΈ |
Images | π | βοΈ | π |
Precomps | π | π | π |
Time Stretch | π | βοΈ | βοΈ |
Time remap | π | βοΈ6 | π |
Markers | βοΈ | βοΈ5 | βοΈ |
3D Layers | π | βοΈ5 | βοΈ |
Repeaters | π | π3 | π |
Solids | π | π3 | π |
Footnotes
-
Marked as unsupported β© β©2 β©3 β©4 β©5 β©6 β©7 β©8 β©9 β©10 β©11 β©12 β©13 β©14 β©15
-
Not listed as unsupported, maybe a bug? β©
-
Marked as unsuported but I haven't tested it β© β©2 β©3 β©4 β©5 β©6 β©7 β©8 β©9
-
Note that python-lottie offers an alternative to lottie text layers, and can render text as shapes, so that is supported everywhere β©