Skip to main content

A ctypes API for rlottie, with additional functions for getting Pillow Image.

Project description

rlottie-python

A ctypes API for rlottie, with additional functions for getting Pillow Image and animated sequences, as well as telegram animated stickers (tgs).

See example/example.py for example usage.

The functions mostly follow rlottie/inc/rlottie.h

Documentations: https://rlottie-python.readthedocs.io/en/latest/

Table of contents

Installing

Note that rlottie is included in the wheel package, you need not install librlottie.

To install, run the following:

pip3 install rlottie-python

Pillow is optional dependency. It is required for render_pillow_frame(), save_frame() and save_animation(). To also install Pillow, run:

pip3 install rlottie-python[full]

Examples

Getting information about an lottie animation

from rlottie_python import LottieAnimation

anim = LottieAnimation.from_file("samples/sample.json")
frames = anim.lottie_animation_get_totalframe()
print(f"{frames = }")

width, height = anim.lottie_animation_get_size()
print(f"{width, height = }")

duration = anim.lottie_animation_get_duration()
print(f"{duration = }")

totalframe = anim.lottie_animation_get_totalframe()
print(f"{totalframe = }")

framerate = anim.lottie_animation_get_framerate()
print(f"{framerate = }")

render_tree = anim.lottie_animation_render_tree(0)
print(f"{render_tree.mMaskList.size = }")

mapped_frame = anim.lottie_animation_get_frame_at_pos(0)
print(f"{mapped_frame = }")

Rendering and saving frame

from rlottie_python import LottieAnimation
from PIL import Image

anim = LottieAnimation.from_file("samples/sample.json")

# Method 1: Saving the frame to file directly
anim.save_frame("frame30.png", frame_num=30)

# Method 2: Getting Pillow Image
im = anim.render_pillow_frame(frame_num=40)
im.save("frame40.png")

# Method 3: Getting buffer
buffer = anim.lottie_animation_render(frame_num=50)
width, height = anim.lottie_animation_get_size()
im = Image.frombuffer("RGBA", (width, height), buffer, "raw", "BGRA")
im.save("frame50.png")

Loading from JSON file, string of JSON, tgs; and rendering animation

from rlottie_python import LottieAnimation

# Loading from file
anim = LottieAnimation.from_file("samples/sample.json")
anim.save_animation("animation1.apng")

anim = LottieAnimation.from_tgs("samples/sample.tgs")
anim.save_animation("animation2.gif")

with open("samples/sample.json", encoding="utf-8") as f:
    data = f.read()

anim = LottieAnimation.from_data(data=data)
anim.save_animation("animation3.webp")

You may also load animation using with statement

from rlottie_python import LottieAnimation

with LottieAnimation.from_file("samples/sample.json") as anim:
    anim.save_animation("animation4.apng")

Comparing to other library

The lottie (https://pypi.org/project/lottie/) python package is also capable of working with lottie files and telegram animated stickers (tgs). It is also able to support many input/output formats and vector graphics, without any dependency on extenral libraries such as librlottie. However some images it creates is broken (Example1 Example2). It seems librlottie is more stable in terms of rendering frames.

The pyrlottie (https://pypi.org/project/pyrlottie/) python package is also able to convert lottie and tgs files to webp/gif. However, it works by calling executables gif2webp and lottie2gif with subprocess, and it does not support macOS.

Building from source

To build wheel, run the following:

git clone --recursive https://github.com/laggykiller/rlottie-python.git
cd rlottie-python

# To build wheel
python3 -m build .

# To install directly
pip3 install .

Development

To run tests:

pip install pytest
pytest

To lint:

pip install ruff mypy isort
mypy
isort .
ruff check
ruff format

Credits

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rlottie_python-1.3.4.tar.gz (4.2 MB view hashes)

Uploaded Source

Built Distributions

rlottie_python-1.3.4-pp310-pypy310_pp73-win_amd64.whl (354.7 kB view hashes)

Uploaded PyPy Windows x86-64

rlottie_python-1.3.4-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (878.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

rlottie_python-1.3.4-pp310-pypy310_pp73-manylinux_2_17_i686.manylinux2014_i686.whl (904.4 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686

rlottie_python-1.3.4-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (815.7 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

rlottie_python-1.3.4-pp310-pypy310_pp73-macosx_11_0_arm64.whl (643.6 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

rlottie_python-1.3.4-pp310-pypy310_pp73-macosx_10_9_x86_64.whl (727.3 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

rlottie_python-1.3.4-pp39-pypy39_pp73-win_amd64.whl (354.7 kB view hashes)

Uploaded PyPy Windows x86-64

rlottie_python-1.3.4-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (878.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

rlottie_python-1.3.4-pp39-pypy39_pp73-manylinux_2_17_i686.manylinux2014_i686.whl (904.3 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686

rlottie_python-1.3.4-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (815.7 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

rlottie_python-1.3.4-pp39-pypy39_pp73-macosx_11_0_arm64.whl (643.6 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

rlottie_python-1.3.4-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (727.3 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

rlottie_python-1.3.4-pp38-pypy38_pp73-win_amd64.whl (354.7 kB view hashes)

Uploaded PyPy Windows x86-64

rlottie_python-1.3.4-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (878.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

rlottie_python-1.3.4-pp38-pypy38_pp73-manylinux_2_17_i686.manylinux2014_i686.whl (904.3 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686

rlottie_python-1.3.4-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (815.7 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

rlottie_python-1.3.4-pp38-pypy38_pp73-macosx_11_0_arm64.whl (643.6 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

rlottie_python-1.3.4-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (727.3 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

rlottie_python-1.3.4-pp37-pypy37_pp73-win_amd64.whl (354.7 kB view hashes)

Uploaded PyPy Windows x86-64

rlottie_python-1.3.4-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (878.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

rlottie_python-1.3.4-pp37-pypy37_pp73-manylinux_2_17_i686.manylinux2014_i686.whl (904.3 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686

rlottie_python-1.3.4-pp37-pypy37_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (815.7 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

rlottie_python-1.3.4-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (727.3 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

rlottie_python-1.3.4-cp39-abi3-win_arm64.whl (295.2 kB view hashes)

Uploaded CPython 3.9+ Windows ARM64

rlottie_python-1.3.4-cp38-abi3-macosx_11_0_arm64.whl (643.6 kB view hashes)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

rlottie_python-1.3.4-cp38-abi3-macosx_10_9_universal2.whl (1.3 MB view hashes)

Uploaded CPython 3.8+ macOS 10.9+ universal2 (ARM64, x86-64)

rlottie_python-1.3.4-cp37-abi3-win_amd64.whl (354.6 kB view hashes)

Uploaded CPython 3.7+ Windows x86-64

rlottie_python-1.3.4-cp37-abi3-win32.whl (299.4 kB view hashes)

Uploaded CPython 3.7+ Windows x86

rlottie_python-1.3.4-cp37-abi3-musllinux_1_1_x86_64.whl (1.4 MB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.1+ x86-64

rlottie_python-1.3.4-cp37-abi3-musllinux_1_1_s390x.whl (1.4 MB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.1+ s390x

rlottie_python-1.3.4-cp37-abi3-musllinux_1_1_ppc64le.whl (1.5 MB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.1+ ppc64le

rlottie_python-1.3.4-cp37-abi3-musllinux_1_1_i686.whl (1.4 MB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.1+ i686

rlottie_python-1.3.4-cp37-abi3-musllinux_1_1_aarch64.whl (1.3 MB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.1+ ARM64

rlottie_python-1.3.4-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (878.1 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

rlottie_python-1.3.4-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (822.8 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ s390x

rlottie_python-1.3.4-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (1.0 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ppc64le

rlottie_python-1.3.4-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (904.3 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ i686

rlottie_python-1.3.4-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (815.7 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARM64

rlottie_python-1.3.4-cp37-abi3-macosx_10_9_x86_64.whl (727.3 kB view hashes)

Uploaded CPython 3.7+ macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page