Repository: python-pillow/Pillow

Issue:
UnboundLocalError: local variable 'err_code' referenced before assignment
### What did you do?
Load a GIF and attempted to save each frame to a file
### What did you expect to happen?
Each frame of the gif is saved to a different image file in a directory
### What actually happened?
The loop errored before completion
```python
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 50, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "/root/discord/mods/Fun.py", line 313, in gmagik
    for image in glob.glob(gif_dir+"*_{0}.png".format(rand)):
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/root/discord/mods/Fun.py", line 232, in do_gmagik
    try:
  File "/usr/local/lib/python3.6/site-packages/PIL/Image.py", line 1698, in save
    self.load()
  File "/usr/local/lib/python3.6/site-packages/PIL/ImageFile.py", line 242, in load
    if not self.map and not LOAD_TRUNCATED_IMAGES and err_code < 0:
UnboundLocalError: local variable 'err_code' referenced before assignment
```
### What versions of Pillow and Python are you using?
Latest PIL from PyPy, Python 3.6

Replicated just using PIL from repl.
```python
try:
	frame = PIL.Image.open(gif)
except:
	return '\N{WARNING SIGN} Invalid Gif.'
if frame.size >= (3000, 3000):
	os.remove(gif)
	return '\N{WARNING SIGN} `GIF resolution exceeds maximum >= (3000, 3000).`'
count = 0
while frame:
	frame.save('{0}{1}_{2}.png'.format(gif_dir, count, rand), 'GIF')
	count += 1
	try:
		frame.seek(count)
	except EOFError:
		break
```
GIF In question: https://cdn.discordapp.com/attachments/272432272949510144/275311320017010709/gmagik.gif (can give more samples if needed)


Discussion:
Thanks, a couple of people have reported this, but your report is the first to include a triggering image.
No problem, I might wanna also include that the gif was processed before (loaded into PIL, frames extracted, Wand/Imagemagick effects applied and then stitched together with ffmpeg) using the same function.
> GIF In question: https://cdn.discordapp.com/attachments/272432272949510144/275311320017010709/gmagik.gif 

This URL gives me:
```xml
<?xml version='1.0' encoding='UTF-8'?><Error><Code>NoSuchKey</Code><Message>The specified key does not exist.</Message></Error>
```
Please could you attach the image to this issue?
That's weird, still loading for me after cache reset but Discord CDN tends to be weird, here: 
![gmagik](https://cloud.githubusercontent.com/assets/4484641/22406500/37cad028-e622-11e6-93dc-fd118ced87c1.gif)

The basic problem is that the second frame has an extent of 256x249, while the first frame is 256x248. 
```
-> decoder.setimage(self.im, extents)
(Pdb) extents
(0, 0, 256, 249)
(Pdb) self.im.size
(256, 248)
(Pdb) n
ValueError: 'tile cannot extend outside image'
> /Users/erics/tests/build/bdist.macosx-10.11-x86_64/egg/PIL/ImageFile.py(197)load()
-> decoder.setimage(self.im, extents)
```

This is in a try/except, which simply swallows the error.

```python
            for decoder_name, extents, offset, args in self.tile:
                decoder = Image._getdecoder(self.mode, decoder_name,
                                      args, self.decoderconfig)
                seek(offset)
                try:
                    decoder.setimage(self.im, extents)
                except ValueError:
                    continue
```

Which bounces out, and then we hit the `err_code` not defined.  

I know that in other image types (e.g., TIFF) when we have multiple frames, we allow for the new image to be a different size than the old one. TIFFs don't have disposal options that GIFs have, so there's a little more complication in the GIF case. 

I'd propose that if the extents are outside the existing image, we should increase the canvas size to cover the union of the existing and next frame. (as long as the extents aren't negative. I think that we should bounce that error).

Additionally, we should not bury errors of this sort. They should probably be raised and allowed to stop the processing. 
Ah, well that makes more sense now.

Increasing canvas size does sound like a good way to go unless there are other complications, thanks again!

Resolving patch:
diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py
--- a/src/PIL/GifImagePlugin.py
+++ b/src/PIL/GifImagePlugin.py
@@ -233,6 +233,8 @@ def _seek(self, frame):
                 # extent
                 x0, y0 = i16(s[0:]), i16(s[2:])
                 x1, y1 = x0 + i16(s[4:]), y0 + i16(s[6:])
+                if x1 > self.size[0] or y1 > self.size[1]:
+                    self._size = max(x1, self.size[0]), max(y1, self.size[1])
                 self.dispose_extent = x0, y0, x1, y1
                 flags = i8(s[8])
 


Test patch:
diff --git a/Tests/images/test_extents.gif b/Tests/images/test_extents.gif
new file mode 100644
Binary files /dev/null and b/Tests/images/test_extents.gif differ
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
@@ -663,3 +663,9 @@ def test_lzw_bits(self):
         self.assertEqual(im.tile[0][3][0], 11)  # LZW bits
         # codec error prepatch
         im.load()
+
+    def test_extents(self):
+        im = Image.open('Tests/images/test_extents.gif')
+        self.assertEqual(im.size, (100, 100))
+        im.seek(1)
+        self.assertEqual(im.size, (150, 150))