python-lottie  0.6.11+devcecd248
A framework to work with lottie files and telegram animated stickers (tgs)
raster.py
Go to the documentation of this file.
1 from .base import importer
2 from ..parsers.baseporter import ExtraOption
3 from ..parsers.pixel import (
4  pixel_to_animation_paths, pixel_to_animation,
5  raster_to_embedded_assets, raster_to_linked_assets
6 )
7 from ..parsers.svg.importer import parse_color
8 
9 try:
10  from ..parsers.raster import raster_to_animation, TraceOptions
11  raster = True
12 except ImportError:
13  raster = False
14 
15 
16 @importer("Raster image", ["bmp", "png", "gif", "webp", "tiff"], [
17  ExtraOption("n_colors", type=int, default=1, help="Number of colors to quantize"),
18  ExtraOption("palette", type=parse_color, default=[], nargs="+", help="Custom palette"),
20  "mode",
21  default="trace" if raster else "embed",
22  choices=["external", "embed", "pixel", "polygon"] + (["trace"] if raster else []),
23  help="Vectorization mode:\n" +
24  " * external : load images as linked assets\n" +
25  " * embed : load images as embedded assets\n" +
26  " * pixel : Vectorize the image into rectangles\n" +
27  " * polygon : Vectorize the image into polygonal shapes\n" +
28  " Looks the same as pixel, but a single shape per color\n" +
29  " * trace : (if available) Use potrace to vectorize\n"
30  ),
31  ExtraOption("frame_delay", type=int, default=4, help="Number of frames to skip between images"),
32  ExtraOption("framerate", type=int, default=60, help="Frames per second"),
33  ExtraOption("frame_files", nargs="+", default=[], help="Additional frames to import"),
35  "color_mode",
36  default="nearest",
37  choices=["nearest", "exact"],
38  help="How to quantize colors.\n" +
39  " * nearest will map each color to the most similar in the palette\n" +
40  " * exact will only match exact colors"
41  ),
43  "embed_format",
44  default=None,
45  help="Format to store images internally when using `embed` mode"
46  ),
48  "stroke",
49  default=1,
50  type=int,
51  help="Stroke width for output shapes"
52  ),
53 ])
54 def import_raster(filenames, n_colors, palette, mode, frame_delay=1,
55  framerate=60, frame_files=[], color_mode="nearest",
56  embed_format=None, stroke=1):
57  if not isinstance(filenames, list):
58  filenames = [filenames]
59  filenames = filenames + frame_files
60 
61  if mode == "embed":
62  return raster_to_embedded_assets(filenames, frame_delay, framerate, embed_format)
63  elif mode == "external":
64  return raster_to_linked_assets(filenames, frame_delay, framerate)
65  elif mode == "trace":
66  from ..parsers.raster import QuanzationMode
67  options = TraceOptions()
68  options.color_mode = QuanzationMode.Nearest if color_mode == "nearest" else QuanzationMode.Exact
69  options.stroke_width = stroke
70  return raster_to_animation(
71  filenames, n_colors, frame_delay,
72  framerate=framerate,
73  palette=palette,
74  trace_options=options
75  )
76  elif mode == "polygon":
77  return pixel_to_animation_paths(filenames, frame_delay, framerate)
78  else:
79  return pixel_to_animation(filenames, frame_delay, framerate)
lottie.parsers.pixel.pixel_to_animation
def pixel_to_animation(filenames, frame_delay=1, framerate=60)
Converts pixel art to vector.
Definition: pixel.py:285
lottie.parsers.raster.raster_to_animation
def raster_to_animation(filenames, n_colors=1, frame_delay=1, framerate=60, palette=[], trace_options=TraceOptions())
Definition: raster.py:147
lottie.parsers.baseporter.ExtraOption
Definition: baseporter.py:32
lottie.parsers.pixel.pixel_to_animation_paths
def pixel_to_animation_paths(filenames, frame_delay=1, framerate=60)
Converts pixel art to vector paths.
Definition: pixel.py:297
lottie.parsers.pixel.raster_to_linked_assets
def raster_to_linked_assets(filenames, frame_delay=1, framerate=60)
Loads external assets.
Definition: pixel.py:269
lottie.importers.raster.import_raster
def import_raster(filenames, n_colors, palette, mode, frame_delay=1, framerate=60, frame_files=[], color_mode="nearest", embed_format=None, stroke=1)
Definition: raster.py:54
lottie.parsers.raster.TraceOptions
Definition: raster.py:39
lottie.parsers.pixel.raster_to_embedded_assets
def raster_to_embedded_assets(filenames, frame_delay=1, framerate=60, embed_format=None)
Loads external assets.
Definition: pixel.py:255
lottie.importers.base.importer
importer
Definition: base.py:21