File indexing completed on 2025-01-19 03:59:52
0001 from .base import importer 0002 from ..parsers.baseporter import ExtraOption 0003 from ..parsers.pixel import ( 0004 pixel_to_animation_paths, pixel_to_animation, 0005 raster_to_embedded_assets, raster_to_linked_assets 0006 ) 0007 from ..parsers.svg.importer import parse_color 0008 0009 try: 0010 from ..parsers.raster import raster_to_animation 0011 raster = True 0012 except ImportError: 0013 raster = False 0014 0015 0016 @importer("Raster image", ["bmp", "png", "gif", "webp", "tiff"], [ 0017 ExtraOption("n_colors", type=int, default=1, help="Number of colors to quantize"), 0018 ExtraOption("palette", type=parse_color, default=[], nargs="+", help="Custom palette"), 0019 ExtraOption( 0020 "mode", 0021 default="embed", 0022 choices=["external", "embed", "pixel", "polygon"] + (["trace"] if raster else []), 0023 help="Vectorization mode:\n" + 0024 " * external : load images as linked assets\n" + 0025 " * embed : load images as embedded assets\n" + 0026 " * pixel : Vectorize the image into rectangles\n" + 0027 " * polygon : Vectorize the image into polygonal shapes\n" + 0028 " Looks the same as pixel, but a single shape per color\n" + 0029 " * trace : (if available) Use potrace to vectorize\n" 0030 ), 0031 ExtraOption("frame_delay", type=int, default=4, help="Number of frames to skip between images"), 0032 ExtraOption("framerate", type=int, default=60, help="Frames per second"), 0033 ExtraOption("frame_files", nargs="+", default=[], help="Additional frames to import"), 0034 ExtraOption( 0035 "color_mode", 0036 default="nearest", 0037 choices=["nearest", "exact"], 0038 help="How to quantize colors.\n" + 0039 " * nearest will map each color to the most similar in the palette\n" + 0040 " * exact will only match exact colors" 0041 ), 0042 ExtraOption( 0043 "embed_format", 0044 default=None, 0045 help="Format to store images internally when using `embed` mode" 0046 ), 0047 ]) 0048 def import_raster(filenames, n_colors, palette, mode, frame_delay=1, 0049 framerate=60, frame_files=[], color_mode="nearest", embed_format=None): 0050 if not isinstance(filenames, list): 0051 filenames = [filenames] 0052 filenames = filenames + frame_files 0053 0054 if mode == "embed": 0055 return raster_to_embedded_assets(filenames, frame_delay, framerate, embed_format) 0056 elif mode == "external": 0057 return raster_to_linked_assets(filenames, frame_delay, framerate) 0058 elif mode == "trace": 0059 from ..parsers.raster import QuanzationMode 0060 # TODO QuanzationMode for raster 0061 cm = QuanzationMode.Nearest if color_mode == "nearest" else QuanzationMode.Exact 0062 0063 return raster_to_animation( 0064 filenames, n_colors, frame_delay, 0065 framerate=framerate, 0066 palette=palette, 0067 mode=cm 0068 ) 0069 elif mode == "polygon": 0070 return pixel_to_animation_paths(filenames, frame_delay, framerate) 0071 else: 0072 return pixel_to_animation(filenames, frame_delay, framerate)