Warning, /graphics/glaxnimate/src/python/README.md is written in an unsupported language. File is not indexed.

0001 Python Glaxnimate
0002 =================
0003 
0004 Python bindings for [Glaxnimate](https://glaxnimate.mattbas.org/).
0005 
0006 Allows to create and modify vector animations. With support for Lottie, SVG, and other formats.
0007 
0008 See the [documentation page](https://glaxnimate.mattbas.org/contributing/scripting/) for more details.
0009 
0010 ## Examples
0011 
0012 ### Convert animated SVG to Lottie
0013 
0014 ```py
0015 import glaxnimate
0016 
0017 # Set up environment
0018 with glaxnimate.environment.Headless():
0019     # Create a document object
0020     document = glaxnimate.model.Document("")
0021 
0022     # Load an animated SVG
0023     with open("MyFile.svg", "rb") as input_file:
0024         glaxnimate.io.registry.from_extension("svg").load(document, input_file.read())
0025 
0026     # ...
0027 
0028     # Write to Lottie
0029     with open("MyFile.json", "wb") as output_file:
0030         output_file.write(glaxnimate.io.registry.from_extension("json").save(document))
0031 ```
0032 
0033 
0034 ### Render Lottie to gif
0035 
0036 
0037 ```py
0038 from PIL import Image
0039 from PIL import features
0040 import glaxnimate
0041 
0042 
0043 def png_gif_prepare(image):
0044     """
0045     Converts the frame image from RGB to indexed, preserving transparency
0046     """
0047     if image.mode not in ["RGBA", "RGBa"]:
0048         image = image.convert("RGBA")
0049     alpha = image.getchannel("A")
0050     image = image.convert("RGB").convert('P', palette=Image.ADAPTIVE, colors=255)
0051     mask = Image.eval(alpha, lambda a: 255 if a <= 128 else 0)
0052     image.paste(255, mask=mask)
0053     return image
0054 
0055 
0056 def save_gif(document, file, skip_frames=1):
0057     start = int(document.main.animation.first_frame)
0058     end = int(document.main.animation.last_frame)
0059 
0060     # Get all frames as PIL images
0061     frames = []
0062     for i in range(start, end+1, skip_frames):
0063         frames.append(png_gif_prepare(document.render_image(i)))
0064 
0065     # Save as animation
0066     duration = int(round(1000 / document.main.fps * skip_frames / 10)) * 10
0067     frames[0].save(
0068         file,
0069         format='GIF',
0070         append_images=frames[1:],
0071         save_all=True,
0072         duration=duration,
0073         loop=0,
0074         transparency=255,
0075         disposal=2,
0076     )
0077 
0078 # Initialize environment
0079 with glaxnimate.environment.Headless():
0080 
0081     document = glaxnimate.model.Document("")
0082 
0083     # Load the lottie JSON
0084     with open("MyFile.json", "rb") as input_file:
0085         glaxnimate.io.registry.from_extension("json").load(document, input_file.read())
0086 
0087     # Save as GIF
0088     with open("MyFile.gif", "rb") as output_file:
0089         save_gif(document, output_file)
0090 ```
0091 
0092 ### Create animations from code
0093 
0094 ```py
0095 import glaxnimate
0096 
0097 with glaxnimate.environment.Headless():
0098     # Create an empty document
0099     document = glaxnimate.model.Document("")
0100 
0101     # Add a layer
0102     layer = document.main.add_shape("Layer")
0103 
0104     # The fill will be applied to all following shapes in the same group / layer
0105     fill = layer.add_shape("Fill")
0106     fill.color.value = "#ff0000"
0107 
0108     # A simple circle moving left and right
0109     ellipse = layer.add_shape("Ellipse")
0110     radius = 64
0111     ellipse.position.set_keyframe(0, glaxnimate.utils.Point(radius, document.size.height / 2))
0112     ellipse.position.set_keyframe(90, glaxnimate.utils.Point(document.size.width-radius, document.size.height / 2))
0113     ellipse.position.set_keyframe(180, glaxnimate.utils.Point(radius, document.size.height / 2))
0114     ellipse.size.value = glaxnimate.utils.Size(radius, radius)
0115 
0116     # Export it
0117     with open("MyFile.svg", "wb") as output_file:
0118         output_file.write(glaxnimate.io.registry.from_extension("svg").save(document))
0119 ```
0120 
0121 ## Dependencies
0122 
0123 This module depends on the following system libraries
0124 
0125 * Qt5 (widgets, xml)
0126 * potrace
0127 * libav / ffmpeg
0128 * libarchive
0129 
0130 To install them on Ubuntu and similar:
0131 
0132 ```bash
0133 apt install libqt5widgets5 libqt5xml5 potrace ffmpeg libarchive13
0134 ```