当前位置:首页 > 技术分享 > 正文内容

gif 转 lottie json

admin1个月前 (05-30)技术分享162


(1)下载 lottie,解压后找到 /bin/lottie_convert.py,将其复制出来和gif图片放一起。

ps:使用 pip3 install lottie 安装的话是找不到这个文件的。

(2)安装 pillow

pip3 install pillow

(3)安装 demjson

pip3 install demjson

可能会报错:error in demjson setup command: use_2to3 is invalid。

解决方法:执行

pip3 install --upgrade setuptools==57.5.0

(4)打开 https://gitee.com/chockqiu/tiny-lottie 下载下来将生成好的json文件放到tiny-lottie目录中执行压缩命令


 gif 转 json

python ./lottie_convert.py tutu.gif tutu.json

压缩 json

tiny-lottie.exe -q 75 -p


#!/usr/bin/env python3

import re
import sys
import os
import argparse
sys.path.insert(0, os.path.join(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
    "lib"
))
from lottie.exporters import exporters
from lottie.importers import importers
from lottie.utils.stripper import float_strip, heavy_strip
from lottie import __version__


desc = """Converts between multiple formats

Supported formats:

- Input:
%s

- Output:
%s
""" % (
    "\n".join("%s- %s" % (" "*2, e.name) for e in importers),
    "\n".join("%s- %s" % (" "*2, e.name) for e in exporters),
)

parser = argparse.ArgumentParser(
    description=desc,
    formatter_class=argparse.RawTextHelpFormatter,
    conflict_handler='resolve'
)
parser.add_argument("--version", "-v", action="version", version="%(prog)s - python-lottie " + __version__)

group = importers.set_options(parser)
group.add_argument(
    "infile",
    default="-",
    nargs="?",
    help="Input file"
)
group.add_argument(
    "--input-format", "-if",
    default=None,
    choices=[importer.slug for importer in importers],
    help="Explicit output format (if missing implied by the input filename)",
)


group = exporters.set_options(parser)

group.add_argument(
    "outfile",
    default="-",
    nargs="?",
    help="Output file"
)
group.add_argument(
    "--output-format", "-of",
    default=None,
    choices=[exporter.slug for exporter in exporters],
    help="Explicit output format (if missing implied by the output filename)",
)
group.add_argument(
    "--sanitize",
    default=False,
    action="store_true",
    help="Ensure the animation is 512x512 and 30 or 60 fps",
)
group.add_argument(
    "--optimize",
    "-O",
    default=1,
    type=int,
    choices=[0, 1, 2],
    help="Optimize the animation parameter:\n" +
         " * 0 no optimization\n" +
         " * 1 truncate floats\n" +
         " * 2 truncate floats and names"
)
group.add_argument(
    "--fps",
    default=None,
    type=int,
    help="If present, changes the output fps to match this value"
)
group.add_argument(
    "--width",
    default=None,
    type=int,
    help="Override output width",
)
group.add_argument(
    "--height",
    default=None,
    type=int,
    help="Override output height",
)


def print_dep_message(loader):
    if not loader.failed_modules:
        return

    sys.stderr.write("Make sure you have the correct dependencies installed\n")

    for failed, dep in loader.failed_modules.items():
        sys.stderr.write("For %s install %s\n" % (failed, dep))


if __name__ == "__main__":
    ns = parser.parse_args()
    if ns.infile == "-" and not ns.input_format:
        parser.print_help()

    infile = ns.infile
    importer = None
    if infile == "-":
        infile = sys.stdin
    else:
        suf = os.path.splitext(infile)[1][1:]
        for p in importers:
            if suf in p.extensions:
                importer = p
                break
    if ns.input_format:
        importer = None
        for p in importers:
            if p.slug == ns.input_format:
                importer = p
                break
    if not importer:
        sys.stderr.write("Unknown importer\n")
        print_dep_message(importers)
        sys.exit(1)

    outfile = ns.outfile
    exporter = None
    if outfile == "-":
        outfile = sys.stdout
    else:
        exporter = exporters.get_from_filename(outfile)
    if ns.output_format:
        exporter = exporters.get(ns.output_format)

    if not exporter:
        sys.stderr.write("Unknown exporter\n")
        print_dep_message(exporters)
        sys.exit(1)

    i_options = {}
    for opt in importer.extra_options:
        i_options[opt.name] = getattr(ns, opt.nsvar(importer.slug))

    o_options = exporter.argparse_options(ns)

    an = importer.process(infile, **i_options)

    if ns.fps:
        an.frame_rate = ns.fps

    if ns.width or ns.height:
        width = ns.width
        height = ns.height
        if not width:
            width = an.width * height / an.height
        if not height:
            height = an.height * width / an.width
        an.scale(width, height)

    if ns.optimize == 1:
        float_strip(an)
    elif ns.optimize >= 2:
        heavy_strip(an)

    exporter.process(an, outfile, **o_options)


扫描二维码推送至手机访问。

版权声明:本文由小刚刚技术博客发布,如需转载请注明出处。

本文链接:https://bitefu.net/post/670.html

分享给朋友:

相关文章

百度云,天翼云盘解析网页地址 收集分享

百度云解析http://p.106666.xyz/https://pan.kdbaidu.com/https://pan.kdpd.me/https://yun.kdbaidu.com/http://blog.xxatf.top/https...

[教程] WTG备份新方法——FFU镜像格式

[教程] WTG备份新方法——FFU镜像格式

FFU(Full Flash Update) 格式是一种基于扇区的磁盘镜像文件格式,默认使用快速哈夫曼压缩(Xpress-Huffman)算法压缩,在捕获和部署时会生成哈希表进行校验,并可以使用DISM修改捕获的镜像。FFU格式很适合WTG...

PIP 更换国内安装源linux/windows

pip国内的一些镜像  阿里云 http://mirrors.aliyun.com/pypi/simple/   中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/   豆瓣(...

七牛html js上传带进度条源码

七牛html js上传带进度条源码注册链接https://s.qiniu.com/uM7RJv完整代码下载:https://n802.com/f/349707-489018989-c141f6(访问密码:5036)http://www.yi...

阿里云盘 分享脚本 抢先实现文件分享功能

Win端基本所有浏览器均可进行安装安装地址:https://bbs.tampermonkey.net.cn/thread-427-1-1.html如何安装:Tampermoneky安装:谷歌浏览器参考:https://bbs.tamperm...

Linux/centos inode 占用100%的解决办法

当你的 Linux 系统无法创建新文件时,有可能是你的磁盘满了,还有可能是你的磁盘的 inode 用光了,我们今天要说的就是后一种情况,要解决这个问题,只能是删除一些文件,但是一般情况下,其实是你的系统中的某个地方产生了大量的你并不需要的文...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。