You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Lottie抛出ImportError:无法从lottie.exporters.cairo导入PngRenderer?

解决ImportError: cannot import name 'PngRenderer' from 'lottie.exporters.cairo'

这个报错的核心原因是lottie库版本不兼容或者cairo依赖未正确安装,以下是几种可行的解决方法:

1. 回退到兼容的lottie版本

新版本的lottie可能重构了导出模块,移除了PngRenderer类或者变更了它的位置。你可以尝试安装一个已知兼容的旧版本:

pip uninstall lottie -y
pip install lottie==0.6.6

安装完成后重新运行你的转换代码,大概率能解决导入问题。

2. 确保cairo依赖完整安装

lottie的GIF导出功能依赖pycairo库,Windows环境下如果pycairo安装不完整,可能导致模块内部类无法正常加载。尝试重新安装pycairo:

pip uninstall pycairo -y
pip install pycairo

如果pip安装失败,可以尝试用conda安装(需要先配置conda环境):

conda install -c conda-forge pycairo

3. 适配新版本lottie的导出API(如果不想回退版本)

如果你想使用最新版lottie,需要调整代码适配新的导出接口。新版本中export_gif可能已被整合到通用导出方法中,修改后的代码如下:

import lottie
import logging
from lottie.parsers.tgs import parse_tgs
from lottie.exporters import export

def convert_tgs_to_gif(tgs_file, gif_file):
    try:
        animation = parse_tgs(tgs_file)
        # 使用通用导出方法指定格式为gif
        export(animation, gif_file, format="gif", skip_frames=5, dpi=48)
        return True
    except Exception:
        logging.exception("Error occurred while converting TGS to GIF.")
        return False

内容的提问来源于stack exchange,提问作者Bubunduc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 12:50:19