from __future__ import print_function
from __future__ import absolute_import
from __future__ import division

import os

try:
    import imageio
except ImportError:
    pass


__all__ = ['gif_from_images']


def gif_from_images(files,
                    gif_path,
                    fps=10,
                    loop=0,
                    reverse=False,
                    pingpong=False,
                    subrectangles=True,
                    delete_files=False):
    """Create an animated GIF from a series of images.

    Parameters
    ----------
    files : list
        The image series.
    gif_path : str
        The location to save the GIF.
    fps : int, optional
        Frames per second.
    loop : int, optional
        The number of loops.
    reverse : bool, optional
        If True, reverse the image series.
    pingpong : bool, optional
        If True, add a reverse sequence to the end of the base sequence to go back to the beginning.
    subrectangles : bool, optional
        If True, optimize the file size by looking for invariant subrectangles.

    Returns
    -------
    None

    """
    if reverse:
        files.reverse()
    if pingpong:
        files += files[::-1]
    with imageio.get_writer(gif_path,
                            mode='I',
                            fps=fps,
                            loop=loop,
                            subrectangles=subrectangles) as writer:
        for filename in files:
            image = imageio.imread(filename)
            writer.append_data(image)
    if delete_files:
        for filename in files:
            os.remove(filename)

@@ -117,61 +117,60 @@
  os%0A
-%0Atry:%0A    import imageio%0Aexcept ImportError:%0A    pass
+import compas%0A%0Aif not compas.IPY:%0A    import imageio
 %0A%0A%0A_
@@ -179,17 +179,17 @@
 ll__ = %5B
-'
+%22
 gif_from
@@ -195,17 +195,17 @@
 m_images
-'
+%22
 %5D%0A%0A%0Adef 
@@ -224,31 +224,20 @@
 ges(
+%0A    
 files,%0A
-                
     
@@ -254,76 +254,28 @@
     
-                fps=10,%0A                    loop=0,%0A                
+fps=10,%0A    loop=0,%0A
     
@@ -289,32 +289,16 @@
 =False,%0A
-                
     ping
@@ -309,32 +309,16 @@
 =False,%0A
-                
     subr
@@ -333,32 +333,16 @@
 s=True,%0A
-                
     dele
@@ -355,16 +355,18 @@
 es=False
+,%0A
 ):%0A    %22
@@ -1130,159 +1130,56 @@
 ter(
-gif_path,%0A                            mode='I',%0A                            fps=fps,%0A                            loop=loop,%0A                           
+%0A        gif_path, mode=%22I%22, fps=fps, loop=loop,
  sub
@@ -1202,16 +1202,21 @@
 ctangles
+%0A    
 ) as wri

fix import guard