[GRASS-SVN] r71482 - grass/branches/releasebranch_7_2/lib/python/imaging

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Sep 8 05:54:08 PDT 2017


Author: martinl
Date: 2017-09-08 05:54:08 -0700 (Fri, 08 Sep 2017)
New Revision: 71482

Modified:
   grass/branches/releasebranch_7_2/lib/python/imaging/images2gif.py
Log:
pythonlib: add PIL native implementation available since Pillow 3.4.0, see #3367 (merge r71258 from trunk)


Modified: grass/branches/releasebranch_7_2/lib/python/imaging/images2gif.py
===================================================================
--- grass/branches/releasebranch_7_2/lib/python/imaging/images2gif.py	2017-09-08 12:51:07 UTC (rev 71481)
+++ grass/branches/releasebranch_7_2/lib/python/imaging/images2gif.py	2017-09-08 12:54:08 UTC (rev 71482)
@@ -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