Open PIL image from zip (Kaggle competition)

Viewed 23

I am trying to read an image from kaggle competition (It is an old competition, but I would like to practice): https://www.kaggle.com/competitions/dogs-vs-cats-redux-kernels-edition

I am trying to read images from the training file zip using this code:

def get_files_names(zip_file_path):
    with ZipFile(zip_file_path) as myzip:
        return myzip.namelist()


def get_image(zip_path, image_name):
    with ZipFile(zip_path) as myzip:
        #         print(myzip.namelist()[:10])
        with myzip.open(image_name) as myfile:
            #             img = Image.open(myfile)
            img = Image.open(myfile)

    return img

names = get_files_names(train_file_path)

img = get_image(train_file_path, names[1])

img.show()

I am getting this error:

Traceback (most recent call last):
  File "/cats_vs_dogs/unrelated_file.py", line 46, in <module>
    img.show()
  File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 2205, in show
    _show(self, title=title, command=command)
  File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 3167, in _show
    _showxv(image, **options)
  File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 3181, in _showxv
    ImageShow.show(image, title, **options)
  File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 56, in show
    if viewer.show(image, title=title, **options):
  File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 81, in show
    return self.show_image(image, **options)
  File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 107, in show_image
    return self.show_file(self.save_image(image), **options)
  File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 103, in save_image
    return image._dump(format=self.get_format(image), **self.options)
  File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 636, in _dump
    self.load()
  File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageFile.py", line 247, in load
    s = read(self.decodermaxblock)
  File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/JpegImagePlugin.py", line 400, in load_read
    s = self.fp.read(read_bytes)
  File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 930, in read
    data = self._read1(n)
  File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 998, in _read1
    data += self._read2(n - len(data))
  File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 1030, in _read2
    data = self._fileobj.read(n)
  File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 753, in read
    self._file.seek(self._pos)
AttributeError: 'NoneType' object has no attribute 'seek'

If I extract the file into finder (using mac), then I see this image: enter image description here

Also, if I try to convert the RGB image into into a numpy array np.array(img), I get this result: enter image description here

What am I doing wrong?

0 Answers
Related