c4dynamics.utils.gen_gif.gif

Contents

c4dynamics.utils.gen_gif.gif#

c4dynamics.utils.gen_gif.gif(dirname, gif_name, duration=None)[source]#

Gif creator.

gif creates a Gif from a directory containing image files.

Parameters:
  • dirname (str) – The path to the directory containing image files.

  • gif_name (str) – The desired name of the output GIF file.

  • duration (float, optional) – The duration (in seconds) for each frame of the GIF. If None, default duration is used.

Example

Let’s generate images of an F16 aircraft in different orientations and create a gif from them.

  1. Prepare trajectory to animate.

Import required packages:

>>> import c4dynamics as c4d
>>> from IPython.display import Image
>>> import numpy as np
>>> import os

Settings and initial conditions:

>>> f16 = c4d.rigidbody()
>>> dt = 0.01

Main loop:

>>> for t in np.arange(0, 9, dt):
...  # in 3 seconds make 180 deg:
...  if t < 3:
...    f16.psi += dt * 180 * c4d.d2r / 3
...  elif t < 6:
...    f16.theta += dt * 180 * c4d.d2r / 3
...  else:
...    f16.phi -= dt * 180 * c4d.d2r / 3
...  f16.store(t)
  1. Animate and save image files.

(Use c4dynamics’ datasets to fetch F16 3D model.)

>>> f16path = c4d.datasets.d3_model('f16')
Fetched successfully
>>> x0 = [90 * c4d.d2r, 0, 180 * c4d.d2r]
>>> outfol = os.path.join('tests', '_out', 'f16b')
>>> f16.animate(f16path, savedir = outfol, angle0 = x0, modelcolor = [0, 0, 0], cbackground = [230 / 255, 230 / 255, 255 / 255])
  1. Export images as gif.

>>> gifname = 'f16_monochrome_gif.gif'
>>> c4d.gif(outfol, gifname, duration = 1)
>>> Image(filename = os.path.join(outfol, gifname))  
../../../_images/f16_monochrome_gif.gif