[GRASS-dev] [GRASS GIS] #3367: g.gui.animation: animated GIF export generates corrupted GIF files

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
-----------------------------+-------------------------
Reporter: neteler | Owner: grass-dev@…
     Type: defect | Status: new
Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: unspecified
Keywords: g.gui.animation | CPU: Unspecified
Platform: Unspecified |
-----------------------------+-------------------------
When exporting an animated GIF, the generated file is corrupted:

{{{
# using GRASS 7.2.2svn (nc_climate_spm_2000_2012): >
g.region raster=2002_10_precip -p

g.gui.animation `g.list raster pattern="2*_10_precip" sep=comma`
Default TGIS driver / database set to:
driver: sqlite
database: $GISDBASE/$LOCATION_NAME/$MAPSET/tgis/sqlite.db
WARNING: Temporal database connection defined as:
/home/mneteler/grassdata/nc_climate_spm_2000_2012/climate_1970_2012/tgis/sqlite.db
          But database file does not exist.
Creating temporal database:
/home/mneteler/grassdata/nc_climate_spm_2000_2012/climate_1970_2012/tgis/sqlite.db
/home/mneteler/software/grass72/dist.x86_64-pc-linux-
gnu/etc/python/grass/imaging/images2gif.py:344: VisibleDeprecationWarning:
converting an array with ndim > 0 to an index will result in an error in
the future
   im2 = im[y0:y1, x0:x1]
}}}

(not sure if the warning matters)

{{{
ls -la bla.gif
-rw-r--r-- 1 mneteler mneteler 1561325 Jul 6 14:55 bla.gif

display bla.gif
display: corrupt image `bla.gif' @ error/gif.c/ReadGIFImage/1368.
}}}

System:
{{{
System Info
GRASS version: 7.2.2svn
GRASS SVN revision: r71233M
Build date: 2017-07-03
Build platform: x86_64-pc-linux-gnu
GDAL: 2.1.2
PROJ.4: 4.9.2
GEOS: 3.5.0
SQLite: 3.14.2
Python: 2.7.13
wxPython: 3.0.2.0
Platform: Linux-4.11.3-202.fc25.x86_64-x86_64-with-fedora-25-Twenty_Five
}}}

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------
Changes (by neteler):

* platform: Unspecified => Linux
* version: unspecified => svn-releasebranch72
* cpu: Unspecified => x86-64

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:1&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: closed
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: fixed | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------
Changes (by annakrat):

* status: new => closed
* resolution: => fixed

Comment:

In [changeset:"71243" 71243]:
{{{
#!CommitTicketReference repository="" revision="71243"
pythonlib: fix #3367 - creating gif files
}}}

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:2&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------
Changes (by annakrat):

* status: closed => reopened
* resolution: fixed =>

Comment:

It automatically closed it, please test and I will backport it then.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:3&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by veroandreo):

Replying to [comment:3 annakrat]:
> It automatically closed it, please test and I will backport it then.

After updating (make distclean ; svn up && sh config_grass7.sh && make), I
still get a corrupted gif from g.gui.animation.

{{{
display prueba.gif
display: corrupt image `prueba.gif' @ error/gif.c/ReadGIFImage/1368.
}}}

System Info

{{{
GRASS version: 7.3.svn
GRASS SVN revision: r71243M
Build date: 2017-07-08
Build platform: x86_64-pc-linux-gnu
GDAL: 2.1.2
PROJ.4: 4.9.2
GEOS: 3.5.0
SQLite: 3.14.2
Python: 2.7.13
wxPython: 3.0.2.0
Platform: Linux-4.11.8-200.fc25.x86_64-x86_64-with-fedora-25-Twenty_Five
}}}

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:4&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by hellik):

Replying to [comment:3 annakrat]:
> It automatically closed it, please test and I will backport it then.

tested here with

{{{
GRASS version: 7.3.svn
GRASS SVN revision: r71243
Build date: 2017-07-08
Build platform: x86_64-pc-linux-gnu
GDAL: 2.2.0
PROJ.4: 4.9.3
GEOS: 3.5.1
SQLite: 3.16.2
Python: 2.7.13
wxPython: 3.0.2.0
Platform: Linux-4.9.0-3-amd64-x86_64-with-debian-9.0
}}}

{{{
display gguitest2.gif
display-im6.q16: corrupt image `gguitest2.gif' @
error/gif.c/ReadGIFImage/1386.
}}}

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:5&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by annakrat):

My gifs are ok... Could you test this code snippet to see if the problem
is in the images2gif (likely)? Also do you have Pillow or PIL?
{{{
import os
from PIL import Image
from grass.imaging import writeGif

color_path = os.path.join(os.environ['GISBASE'], "docs", "html",
"colortables")
pil_images = [Image.open(os.path.join(color_path, c)) for c in
os.listdir(color_path)]
writeGif('/tmp/test.gif', pil_images, duration=0.2, repeat=True)

}}}

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:6&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by hellik):

Replying to [comment:6 annakrat]:
> My gifs are ok... Could you test this code snippet to see if the problem
is in the images2gif (likely)? Also do you have Pillow or PIL?
> {{{
> import os
> from PIL import Image
> from grass.imaging import writeGif
>
> color_path = os.path.join(os.environ['GISBASE'], "docs", "html",
"colortables")
> pil_images = [Image.open(os.path.join(color_path, c)) for c in
os.listdir(color_path)]
> writeGif('/tmp/test.gif', pil_images, duration=0.2, repeat=True)
>
> }}}

lines above tested

{{{
display test.gif
display-im6.q16: corrupt image `test.gif' @ error/gif.c/ReadGIFImage/1386.
}}}

the same error

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:7&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by hellik):

Replying to [comment:6 annakrat]:
> Also do you have Pillow or PIL?

{{{
python-pil 4.0.0-4
}}}

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:8&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by veroandreo):

I tested the code in the python console of GRASS GUI. The output gif gives
the same error as before.

I have python2-pillow-3.4.2-1

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:9&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by annakrat):

Could you test any of the available versions of images2gif.py files from
different repositories? I found couple for example:

https://bitbucket.org/bench/images2gif.py/raw/a618bd8fc352f278a3b0c777cb6427ec39236f0a/images2gif.py

https://github.com/isaacgerg/images2gif/blob/master/images2gif.py

https://gist.github.com/jonschoning/7216290

https://github.com/rec/echomesh/blob/master/code/python/external/images2gif.py

If you replace the import with

{{{ from images2gif import writeGif}}}

then the test code snippet should work.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:10&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by hellik):

Replying to [comment:10 annakrat]:
> Could you test any of the available versions of images2gif.py files from
different repositories? I found couple for example:
>
>
https://bitbucket.org/bench/images2gif.py/raw/a618bd8fc352f278a3b0c777cb6427ec39236f0a/images2gif.py
>
>
> https://github.com/isaacgerg/images2gif/blob/master/images2gif.py
>
> https://gist.github.com/jonschoning/7216290
>
>
https://github.com/rec/echomesh/blob/master/code/python/external/images2gif.py
>
> If you replace the import with
>
> {{{ from images2gif import writeGif}}}
>
> then the test code snippet should work.

tested here with

{{{
System Info
GRASS version: 7.3.svn
GRASS SVN revision: r71251
Build date: 2017-07-09
Build platform: x86_64-w64-mingw32
GDAL: 2.2.1
PROJ.4: 4.9.3
GEOS: 3.5.0
SQLite: 3.17.0
Python: 2.7.5
wxPython: 2.8.12.1
Platform: Windows-8-6.2.9200 (OSGeo4W)
}}}

and no change in images2gif.

the gif is exported, but it's not displayed correctly. only the first
frame is shown and the next frames are white

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:11&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by hellik):

Replying to [comment:10 annakrat]:
> Could you test any of the available versions of images2gif.py files from
different repositories? I found couple for example:
>
>
https://bitbucket.org/bench/images2gif.py/raw/a618bd8fc352f278a3b0c777cb6427ec39236f0a/images2gif.py
>
>
> https://github.com/isaacgerg/images2gif/blob/master/images2gif.py
>
> https://gist.github.com/jonschoning/7216290
>
>
https://github.com/rec/echomesh/blob/master/code/python/external/images2gif.py
>

tested here on windows all these images2gif.py, none of them worked.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:12&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by annakrat):

I tested it on standalone winGRASS and OSGEO4W and it works in the
standalone one, but not in OSGEO4W. It seems they use different PIL
library, so that must be the difference. I will look into it later.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:13&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by annakrat):

In [changeset:"71258" 71258]:
{{{
#!CommitTicketReference repository="" revision="71258"
pythonlib: add PIL native implementation available since Pillow 3.4.0, see
#3367
}}}

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:14&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by annakrat):

So, after some digging around, it seems to stop working since Pillow
version 3.4.0 and I haven't found anywhere a working version of
images2gif.py and I haven't been able to find the problem. But since 3.4.0
Pillow itself allows to write GIFs, so I added it there. Now depending on
your PIL/Pillow and Pillow version, either the old or the native Pillow
way is used. The problem is the GIF created by Pillow looks worse, but I
don't know what could be done here.
Also we could use imagemagick as a backend if it's installed and
available.
Anyway, please test.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:15&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by veroandreo):

Tested here having python2-pillow-3.4.2-1. Now, it works, but as you say
the GIF doesn't look so good in terms of definition of the images. They
look noisy, as when you increase ISO in the camera.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:16&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by martinl):

Replying to [comment:16 veroandreo]:
> Tested here having python2-pillow-3.4.2-1. Now, it works, but as you say
the GIF doesn't look so good in terms of definition of the images. They
look noisy, as when you increase ISO in the camera.

I can confirm it. I have `python-pil 4.2.1-1`. See attachment:noisy.gif
created by

{{{
import os
from PIL import Image
from grass.imaging import writeGif

color_path = os.path.join(os.environ['GISBASE'], "docs", "html",
"colortables")
pil_images = [Image.open(os.path.join(color_path, c)) for c in
os.listdir(color_path)]
writeGif('/tmp/test.gif', pil_images, duration=0.2, repeat=True)
}}}

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:17&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------
Changes (by martinl):

* Attachment "noisy.gif" added.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367&gt;
GRASS GIS <https://grass.osgeo.org>

#3367: g.gui.animation: animated GIF export generates corrupted GIF files
----------------------+---------------------------------
  Reporter: neteler | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.2
Component: wxGUI | Version: svn-releasebranch72
Resolution: | Keywords: g.gui.animation
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by martinl):

In [changeset:"71481" 71481]:
{{{
#!CommitTicketReference repository="" revision="71481"
pythonlib: see #3367 - creating gif files (merge r71243 from trunk)
}}}

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3367#comment:18&gt;
GRASS GIS <https://grass.osgeo.org>