···140140#d.display_image() # Display SVG as an image (will not be interactive)
141141#d.display_iframe() # Display as interactive SVG (alternative)
142142#d.as_gif('orbit.gif', fps=10) # Render as a GIF image, optionally save to file
143143-#d.as_mp4('orbig.mp4', fps=60) # Render as an MP4 video, optionally save to file
143143+#d.as_mp4('orbig.mp4', fps=60, verbose=True) # Render as an MP4 video, optionally save to file
144144+#d.as_spritesheet('orbit-spritesheet.png', row_length=10, fps=3) # Render as a spritesheet
144145d.display_inline() # Display as interactive SVG
145146```
146147
···2525 ) from e
2626 return cairosvg
27272828+def delay_import_imageio():
2929+ try:
3030+ import imageio
3131+ except ImportError as e:
3232+ raise ImportError(
3333+ 'Optional dependencies not installed. '
3434+ 'Install with `python3 -m pip install "drawsvg[all]"` '
3535+ 'or `python3 -m pip install "drawsvg[raster]"`. '
3636+ 'See https://github.com/cduck/drawsvg#full-feature-install '
3737+ 'for more details.'
3838+ ) from e
3939+ return imageio
4040+28412942class Raster:
3043 def __init__(self, png_data=None, png_file=None):
···4356 cairosvg = delay_import_cairo()
4457 cairosvg.svg2png(bytestring=svg_data, write_to=out_file)
4558 return Raster(None, png_file=out_file)
5959+ @staticmethod
6060+ def from_arr(arr, out_file=None):
6161+ imageio = delay_import_imageio()
6262+ if out_file is None:
6363+ with io.BytesIO() as f:
6464+ imageio.imwrite(f, arr, format='png')
6565+ f.seek(0)
6666+ return Raster(f.read())
6767+ else:
6868+ imageio.imwrite(out_file, arr, format='png')
6969+ return Raster(None, png_file=out_file)
4670 def _repr_png_(self):
4771 if self.png_data:
4872 return self.png_data
+31-5
drawsvg/video.py
···149149 print(f'Converting to video')
150150 imageio.mimsave(file, frames, **kwargs)
151151152152-def save_spritesheet(frames, file, row_length=None, verbose=False, **kwargs):
152152+def render_spritesheet(frames, row_length=None, verbose=False, **kwargs):
153153 '''
154154 Save a series of drawings as a bitmap spritesheet
155155156156 Arguments:
157157 frames: A list of `Drawing`s or a list of `numpy.array`s.
158158- file: File name or file like object to write the spritesheet to. The
159159- extension determines the output format.
160158 row_length: The length (in frames) of one row in the spritesheet.
161159 If not provided, all frames go on one row.
162160 align_bottom: If frames are different sizes, align the bottoms of each
···168166 **kwargs: Other arguments to imageio.imsave().
169167170168 '''
171171- np, imageio = delay_import_np_imageio()
169169+ np, _ = delay_import_np_imageio()
172170 if not isinstance(frames[0], np.ndarray):
173171 frames = render_svg_frames(frames, verbose=verbose, **kwargs)
174172 kwargs.pop('align_bottom', None)
175173 kwargs.pop('align_right', None)
176176- kwargs.pop('bg', None)
174174+ bg = kwargs.pop('bg', (255, 255, 255, 255))
177175178176 cols = row_length if row_length is not None else len(frames)
179177 rows = (len(frames) - 1) // cols + 1
180178181179 if rows * cols > len(frames): # Unfilled final row
182180 empty_frame = np.zeros(frames[0].shape, dtype=frames[0].dtype)
181181+ empty_frame[..., :] = bg[:empty_frame.shape[-1]]
183182 frames.extend([empty_frame] * (rows * cols - len(frames)))
184183185184 block_arrangement = []
···190189 ])
191190192191 spritesheet = np.block(block_arrangement)
192192+ return spritesheet
193193+194194+def save_spritesheet(frames, file, row_length=None, verbose=False, **kwargs):
195195+ '''
196196+ Save a series of drawings as a bitmap spritesheet
197197+198198+ Arguments:
199199+ frames: A list of `Drawing`s or a list of `numpy.array`s.
200200+ file: File name or file like object to write the spritesheet to. The
201201+ extension determines the output format.
202202+ row_length: The length (in frames) of one row in the spritesheet.
203203+ If not provided, all frames go on one row.
204204+ align_bottom: If frames are different sizes, align the bottoms of each
205205+ frame in the video.
206206+ align_right: If frames are different sizes, align the right edge of each
207207+ frame in the video.
208208+ bg: If frames are different sizes, fill the background with this color.
209209+ (default is white: (255, 255, 255, 255))
210210+ **kwargs: Other arguments to imageio.imsave().
211211+212212+ '''
213213+ _, imageio = delay_import_np_imageio()
214214+ spritesheet = render_spritesheet(
215215+ frames, row_length=row_length, verbose=verbose, **kwargs)
216216+ kwargs.pop('align_bottom', None)
217217+ kwargs.pop('align_right', None)
218218+ kwargs.pop('bg', None)
193219 imageio.imsave(file, spritesheet, **kwargs)