Repository: python-pillow/Pillow

Issue:
TypeError: object of type 'NoneType' has no len()
### TypeError: object of type 'NoneType' has no len()

This image has a value "comment" is None. It raises that error.  

This is my PR 
https://github.com/python-pillow/Pillow/pull/3314

https://github.com/python-pillow/Pillow/blob/master/src/PIL/GifImagePlugin.py#L527
```python
>    if "comment" in im.encoderinfo and 1 <= len(im.encoderinfo["comment"]) <= 255:
E                  TypeError: object of type 'NoneType' has no len()

```


https://github.com/python-pillow/Pillow/blob/master/src/PIL/GifImagePlugin.py#L697

```python
    def _get_global_header(im, info):
        """Return a list of strings representing a GIF header"""
    
        # Header Block
        # http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
    
        version = b"87a"
        for extensionKey in ["transparency", "duration", "loop", "comment"]:
            if info and extensionKey in info:
                if ((extensionKey == "duration" and info[extensionKey] == 0) or
>                  (extensionKey == "comment" and not (1 <= len(info[extensionKey]) <= 255))):
E                  TypeError: object of type 'NoneType' has no len()
```


```python
{'background': 160,
 'comment': None,
 'duration': 500,
 'optimize': True,
 'version': b'GIF89a'}
```

This code is reproducing that issue
```python
from PIL import Image
from io import BytesIO

filename = 'test.gif'


with open(filename, "rb") as fp:

    img = Image.open(fp)
    file_obj = BytesIO()

    print(img.info)

    getattr(img, "is_animated", False)

    print(img.info)

    img.save(file_obj, format="GIF")
```
![test2](https://user-images.githubusercontent.com/9821654/44784628-f9408780-ab96-11e8-9e9b-038467b6058a.gif)




Discussion:
The code you have quoted is from GifImagePlugin, but the image you have attached is a PNG. Are you able to attach the problematic GIF file?

Resolving patch:
diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py
--- a/src/PIL/GifImagePlugin.py
+++ b/src/PIL/GifImagePlugin.py
@@ -166,6 +166,7 @@ def _seek(self, frame):
         from copy import copy
         self.palette = copy(self.global_palette)
 
+        info = {}
         while True:
 
             s = self.fp.read(1)
@@ -184,8 +185,8 @@ def _seek(self, frame):
                     #
                     flags = i8(block[0])
                     if flags & 1:
-                        self.info["transparency"] = i8(block[3])
-                    self.info["duration"] = i16(block[1:3]) * 10
+                        info["transparency"] = i8(block[3])
+                    info["duration"] = i16(block[1:3]) * 10
 
                     # disposal method - find the value of bits 4 - 6
                     dispose_bits = 0b00011100 & flags
@@ -200,16 +201,16 @@ def _seek(self, frame):
                     #
                     # comment extension
                     #
-                    self.info["comment"] = block
+                    info["comment"] = block
                 elif i8(s) == 255:
                     #
                     # application extension
                     #
-                    self.info["extension"] = block, self.fp.tell()
+                    info["extension"] = block, self.fp.tell()
                     if block[:11] == b"NETSCAPE2.0":
                         block = self.data()
                         if len(block) >= 3 and i8(block[0]) == 1:
-                            self.info["loop"] = i16(block[1:3])
+                            info["loop"] = i16(block[1:3])
                 while self.data():
                     pass
 
@@ -268,6 +269,12 @@ def _seek(self, frame):
             # self.__fp = None
             raise EOFError
 
+        for k in ["transparency", "duration", "comment", "extension", "loop"]:
+            if k in info:
+                self.info[k] = info[k]
+            elif k in self.info:
+                del self.info[k]
+
         self.mode = "L"
         if self.palette:
             self.mode = "P"


Test patch:
diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py
--- a/Tests/test_file_gif.py
+++ b/Tests/test_file_gif.py
@@ -134,13 +134,15 @@ def test_headers_saving_for_animated_gifs(self):
         # Multiframe image
         im = Image.open("Tests/images/dispose_bgnd.gif")
 
+        info = im.info.copy()
+
         out = self.tempfile('temp.gif')
         im.save(out, save_all=True)
         reread = Image.open(out)
 
         for header in important_headers:
             self.assertEqual(
-                im.info[header],
+                info[header],
                 reread.info[header]
             )
 
@@ -207,6 +209,15 @@ def test_seek(self):
         except EOFError:
             self.assertEqual(framecount, 5)
 
+    def test_seek_info(self):
+        im = Image.open("Tests/images/iss634.gif")
+        info = im.info.copy()
+
+        im.seek(1)
+        im.seek(0)
+
+        self.assertEqual(im.info, info)
+
     def test_n_frames(self):
         for path, n_frames in [
             [TEST_GIF, 1],