Pillow: AttributeError: 'NoneType' object has no attribute 'read'

Created on 3 Apr 2020  路  10Comments  路  Source: python-pillow/Pillow

What did you do?

Following on from #4509 after the 7.1.0 release, we have a new issue that cropped up opening a png from scikit-image.

What did you expect to happen?

Not error.

What actually happened?

It errored.

What are your OS, Python and Pillow versions?

  • OS: Linux
  • Python: 3.8.2
  • Pillow: 7.1.0


The code should just be loading a scikit-image PNG data:

from skimage import data
original = data.camera().astype('float')

The error message is:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/miniconda/envs/sunpy/lib/python3.8/site-packages/skimage/data/__init__.py", line 109, in camera
    return _load("camera.png")
  File "/opt/miniconda/envs/sunpy/lib/python3.8/site-packages/skimage/data/__init__.py", line 96, in _load
    return imread(_os.path.join(data_dir, f), plugin='pil', as_gray=as_gray)
  File "/opt/miniconda/envs/sunpy/lib/python3.8/site-packages/skimage/io/_io.py", line 48, in imread
    img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
  File "/opt/miniconda/envs/sunpy/lib/python3.8/site-packages/skimage/io/manage_plugins.py", line 210, in call_plugin
    return func(*args, **kwargs)
  File "/opt/miniconda/envs/sunpy/lib/python3.8/site-packages/skimage/io/_plugins/pil_plugin.py", line 36, in imread
    return pil_to_ndarray(im, dtype=dtype, img_num=img_num)
  File "/opt/miniconda/envs/sunpy/lib/python3.8/site-packages/skimage/io/_plugins/pil_plugin.py", line 66, in pil_to_ndarray
    image.seek(i)
  File "/opt/miniconda/envs/sunpy/lib/python3.8/site-packages/PIL/PngImagePlugin.py", line 748, in seek
    self._seek(f)
  File "/opt/miniconda/envs/sunpy/lib/python3.8/site-packages/PIL/PngImagePlugin.py", line 791, in _seek
    cid, pos, length = self.png.read()
AttributeError: 'NoneType' object has no attribute 'read'

Since this is scikit image loading one of their PNGs, I wonder if it's actually a bug there.

Bug

Most helpful comment

Pillow 7.1.2 has now been released with the fix for this.

All 10 comments

I'll look into this, I think the issue is seek(1) (or anything > 0) not raising EOFError for regular png's

We've merged a fix for this (https://github.com/python-pillow/Pillow/pull/4528). Before we release, it would be helpful if people could run their test suites with the master branch to check if there are other regressions. Thanks!

I just pip installed master and run the given example (and my local test suite), and everything is working. Thanks!

I have the same issue, could someone tell me how I can get it fixed please? I bumped into this problem when using scikit-image (in a line with data.coins() in it, which is supposed to read an image of some coins as a numpy object) and reading the error messages it seems to be an issue with pillow. I'm using the pillow-7.1.1, is there a newer one I should install using pip?

$HOME/.local/lib/python3.6/site-packages/PIL/PngImagePlugin.py in _seek(self, frame, rewind)
    789 
    790             try:
--> 791                 cid, pos, length = self.png.read()
    792             except (struct.error, SyntaxError):
    793                 break

AttributeError: 'NoneType' object has no attribute 'read'

Hi @aderchox. Here are three options
1) Download from Pillow master using the 'Clone or download' at https://github.com/python-pillow/Pillow, and then install Pillow from source
2) Downgrade to Pillow 7.0.0 until a newer Pillow is released
3) Manually alter your current Pillow install by finding PIL/PngImagePlugin.py on your machine and making the changes described here - https://github.com/python-pillow/Pillow/pull/4528/files#diff-07cbf160cd247acb9b67060ff3d81ed9

Hmm, option # 3 above worked for me, but I had to leave this in the plugin file:

    @property
    def n_frames(self):
        if self._n_frames is None:
            return 1
        return self._n_frames

I did delete this:

    @property
    def is_animated(self):
        return self._n_frames is not None and self._n_frames > 1

Pillow 7.1.2 has now been released with the fix for this.

Excellent! When will it be up on conda forge?

We don't maintain the conda forge package, but see https://github.com/conda-forge/pillow-feedstock/pull/78.

try ti upgrade Pillow

apt update
apt install python3-pip -y
apt install libjpeg8-dev zlib1g-dev libtiff-dev libfreetype6 libfreetype6-dev libwebp-dev libopenjp2-7-dev libopenjp2-7-dev -y

pip3 install pillow --global-option="build_ext" --global-option="--enable-zlib" --global-option="--enable-jpeg" --global-option="--enable-tiff" --global-option="--enable-freetype" --global-option="--enable-webp" --global-option="--enable-webpmux" --global-option="--enable-jpeg2000"

Was this page helpful?
0 / 5 - 0 ratings