# Buggy code
def load_end(self):
    "internal: finished reading image data"
    while True:
        self.fp.read(4)  # CRC

        try:
            cid, pos, length = self.png.read()
        except (struct.error, SyntaxError):
            break

        if cid == b"IEND":
            break

        try:
            self.png.call(cid, pos, length)
        except UnicodeDecodeError:
            break
    self._text = self.png.im_text
    self.png.close()
    self.png = None


# Traceback (EOFError)
EOFError                                  Traceback (most recent call last)
<ipython-input-11-d946298b95dd> in <module>
----> 1 image.show()

~/py/ocean/ocean_ai/env/lib/python3.6/site-packages/PIL/Image.py in show(self, title, command)
2039         """
2040
-> 2041         _show(self, title=title, command=command)
2042
2043     def split(self):

~/py/ocean/ocean_ai/env/lib/python3.6/site-packages/PIL/Image.py in _show(image, **options)
2904 def _show(image, **options):
2905     # override me, as necessary
-> 2906     _showxv(image, **options)
2907
2908

~/py/ocean/ocean_ai/env/lib/python3.6/site-packages/PIL/Image.py in _showxv(image, title, **options)
2909 def _showxv(image, title=None, **options):
2910     from . import ImageShow
-> 2911     ImageShow.show(image, title, **options)
2912
2913

~/py/ocean/ocean_ai/env/lib/python3.6/site-packages/PIL/ImageShow.py in show(image, title, **options)
51     """
52     for viewer in _viewers:
---> 53         if viewer.show(image, title=title, **options):
54             return 1
55     return 0

~/py/ocean/ocean_ai/env/lib/python3.6/site-packages/PIL/ImageShow.py in show(self, image, **options)
75             image = image.convert(base)
76
---> 77         return self.show_image(image, **options)
78
79     # hook methods

~/py/ocean/ocean_ai/env/lib/python3.6/site-packages/PIL/ImageShow.py in show_image(self, image, **options)
95     def show_image(self, image, **options):
96         """Display given image"""
---> 97         return self.show_file(self.save_image(image), **options)
98
99     def show_file(self, file, **options):

~/py/ocean/ocean_ai/env/lib/python3.6/site-packages/PIL/ImageShow.py in save_image(self, image)
91     def save_image(self, image):
92         """Save to temporary file, and return filename"""
---> 93         return image._dump(format=self.get_format(image), **self.options)
94
95     def show_image(self, image, **options):

~/py/ocean/ocean_ai/env/lib/python3.6/site-packages/PIL/Image.py in _dump(self, file, format, **options)
646                 filename = filename + suffix
647
--> 648         self.load()
649
650         if not format or format == "PPM":

~/py/ocean/ocean_ai/env/lib/python3.6/site-packages/PIL/ImageFile.py in load(self)
248         self.readonly = readonly
249
--> 250         self.load_end()
251
252         if self._exclusive_fp and self._close_exclusive_fp_after_loading:

~/py/ocean/ocean_ai/env/lib/python3.6/site-packages/PIL/PngImagePlugin.py in load_end(self)
675
676             try:
--> 677                 self.png.call(cid, pos, length)
678             except UnicodeDecodeError:
679                 break

~/py/ocean/ocean_ai/env/lib/python3.6/site-packages/PIL/PngImagePlugin.py in call(self, cid, pos, length)
138
139         logger.debug("STREAM %r %s %s", cid, pos, length)
--> 140         return getattr(self, "chunk_" + cid.decode('ascii'))(pos, length)
141
142     def crc(self, cid, data):

~/py/ocean/ocean_ai/env/lib/python3.6/site-packages/PIL/PngImagePlugin.py in chunk_IDAT(self, pos, length)
354         self.im_tile = [("zip", (0, 0)+self.im_size, pos, self.im_rawmode)]
355         self.im_idat = length
--> 356         raise EOFError
357
358     def chunk_IEND(self, pos, length):

EOFError:

# Fixed code
def load_end(self):
    "internal: finished reading image data"
    while True:
        self.fp.read(4)  # CRC

        try:
            cid, pos, length = self.png.read()
        except (struct.error, SyntaxError):
            break

        if cid == b"IEND":
            break

        try:
            self.png.call(cid, pos, length)
        except UnicodeDecodeError:
            break
        except EOFError:
            ImageFile._safe_read(self.fp, length)
    self._text = self.png.im_text
    self.png.close()
    self.png = None