[GRASS-SVN] r71258 - grass/trunk/lib/python/imaging

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jul 11 20:33:58 PDT 2017


Author: annakrat
Date: 2017-07-11 20:33:57 -0700 (Tue, 11 Jul 2017)
New Revision: 71258

Modified:
   grass/trunk/lib/python/imaging/images2gif.py
Log:
pythonlib: add PIL native implementation available since Pillow 3.4.0, see #3367

Modified: grass/trunk/lib/python/imaging/images2gif.py
===================================================================
--- grass/trunk/lib/python/imaging/images2gif.py	2017-07-10 21:47:53 UTC (rev 71257)
+++ grass/trunk/lib/python/imaging/images2gif.py	2017-07-12 03:33:57 UTC (rev 71258)
@@ -477,9 +477,10 @@
         return frames
 
 
-def writeGif(filename, images, duration=0.1, repeat=True, dither=False,
-             nq=0, subRectangles=True, dispose=None):
+def writeGif(filename, images, duration=0.1, repeat=True, **kwargs):
     """Write an animated gif from the specified images.
+    Depending on which PIL library is used, either writeGifVisvis or writeGifPillow
+    is used here.
 
     :param str filename: the name of the file to write the image to.
     :param list images: should be a list consisting of PIL images or numpy
@@ -487,6 +488,49 @@
                         integer types, and between 0 and 1 for float types.
     :param duration: scalar or list of scalars The duration for all frames, or
                      (if a list) for each frame.
+    :param repeat: bool or integer The amount of loops. If True, loops infinitetel
+    :param kwargs: additional parameters for writeGifVisvis
+
+    """
+    if pillow:
+        # Pillow >= 3.4.0 has animated GIF writing
+        version = [int(i) for i in PILLOW_VERSION.split('.')]
+        if version[0] > 3 or (version[0] == 3 and version[1] >= 4):
+            writeGifPillow(filename, images, duration, repeat)
+            return
+    # otherwise use the old one
+    writeGifVisvis(filename, images, duration, repeat, **kwargs)
+
+
+def writeGifPillow(filename, images, duration=0.1, repeat=True):
+    """Write an animated gif from the specified images.
+    Uses native Pillow implementation, which is available since Pillow 3.4.0.
+
+    :param str filename: the name of the file to write the image to.
+    :param list images: should be a list consisting of PIL images or numpy
+                        arrays. The latter should be between 0 and 255 for
+                        integer types, and between 0 and 1 for float types.
+    :param duration: scalar or list of scalars The duration for all frames, or
+                     (if a list) for each frame.
+    :param repeat: bool or integer The amount of loops. If True, loops infinitetel
+
+    """
+    loop = 0 if repeat else 1
+    images[0].save(filename, save_all=True, append_images=images[1:], loop=loop, duration=duration * 1000)
+
+
+def writeGifVisvis(filename, images, duration=0.1, repeat=True, dither=False,
+                   nq=0, subRectangles=True, dispose=None):
+    """Write an animated gif from the specified images.
+    Uses VisVis implementation. Unfortunately it produces corrupted GIF
+    with Pillow >= 3.4.0.
+
+    :param str filename: the name of the file to write the image to.
+    :param list images: should be a list consisting of PIL images or numpy
+                        arrays. The latter should be between 0 and 255 for
+                        integer types, and between 0 and 1 for float types.
+    :param duration: scalar or list of scalars The duration for all frames, or
+                     (if a list) for each frame.
     :param repeat: bool or integer The amount of loops. If True, loops infinitetely.
     :param bool dither: whether to apply dithering
     :param int nq: If nonzero, applies the NeuQuant quantization algorithm to



More information about the grass-commit mailing list