python-lottie  0.6.11+deved4e6c7
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"], [ ExtraOption("n_colors", type=int, default=1, help="Number of colors to quantize"),
17  ExtraOption("palette", type=parse_color, default=[], nargs="+", help="Custom palette"),
19  "mode",
20  default="trace" if raster else "embed",
21  choices=["external", "embed", "pixel", "polygon"] + (["trace"] if raster else []),
22  help="Vectorization mode:\n" +
23  " * external : load images as linked assets\n" +
24  " * embed : load images as embedded assets\n" +
25  " * pixel : Vectorize the image into rectangles\n" +
26  " * polygon : Vectorize the image into polygonal shapes\n" +
27  " Looks the same as pixel, but a single shape per color\n" +
28  " * trace : (if available) Use potrace to vectorize\n"
29  ),
30  ExtraOption("frame_delay", type=int, default=4, help="Number of frames to skip between images"),
31  ExtraOption("framerate", type=int, default=60, help="Frames per second"),
32  ExtraOption("frame_files", nargs="+", default=[], help="Additional frames to import"),
34  "color_mode",
35  default="nearest",
36  choices=["nearest", "exact"],
37  help="How to quantize colors.\n" +
38  " * nearest will map each color to the most similar in the palette\n" +
39  " * exact will only match exact colors"
40  ),
42  "embed_format",
43  default=None,
44  help="Format to store images internally when using `embed` mode"
45  ),
47  "stroke",
48  default=1,
49  type=int,
50  help="Stroke width for output shapes"
51  ),
52 ])
53 def import_raster(filenames, n_colors, palette, mode, frame_delay=1,
54  framerate=60, frame_files=[], color_mode="nearest",
55  embed_format=None, stroke=1):
56  if not isinstance(filenames, list):
57  filenames = [filenames]
58  filenames = filenames + frame_files
59 
60  if mode == "embed":
61  return raster_to_embedded_assets(filenames, frame_delay, framerate, embed_format)
62  elif mode == "external":
63  return raster_to_linked_assets(filenames, frame_delay, framerate)
64  elif mode == "trace":
65  from ..parsers.raster import QuanzationMode
66  options = TraceOptions()
67  options.color_mode = QuanzationMode.Nearest if color_mode == "nearest" else QuanzationMode.Exact
68  options.stroke_width = stroke
69  return raster_to_animation(
70  filenames, n_colors, frame_delay,
71  framerate=framerate,
72  palette=palette,
73  trace_options=options
74  )
75  elif mode == "polygon":
76  return pixel_to_animation_paths(filenames, frame_delay, framerate)
77  else:
78  return pixel_to_animation(filenames, frame_delay, framerate)
79 
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:55
def pixel_to_animation_paths(filenames, frame_delay=1, framerate=60)
Converts pixel art to vector paths.
Definition: pixel.py:297
def pixel_to_animation(filenames, frame_delay=1, framerate=60)
Converts pixel art to vector.
Definition: pixel.py:285
def raster_to_linked_assets(filenames, frame_delay=1, framerate=60)
Loads external assets.
Definition: pixel.py:269
def raster_to_embedded_assets(filenames, frame_delay=1, framerate=60, embed_format=None)
Loads external assets.
Definition: pixel.py:255
def raster_to_animation(filenames, n_colors=1, frame_delay=1, framerate=60, palette=[], trace_options=TraceOptions())
Definition: raster.py:172