Skip to content

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

Closed

Description

@nabobalis

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.

Activity

  1. pmrowla commented on Apr 3, 2020

    @pmrowla
    Contributor

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

  2. added a commit that references this issue on Apr 6, 2020
  3. hugovk commented on Apr 6, 2020

    @hugovk
    Member

    We've merged a fix for this (#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!

  4. nabobalis commented on Apr 6, 2020

    @nabobalis
    Author

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

  5. added
    Bug
    on Apr 6, 2020
  6. aderchox commented on Apr 20, 2020

    @aderchox

    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'
    
  7. radarhere commented on Apr 20, 2020

    @radarhere
    Member

    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
  8. pythonic2020 commented on Apr 23, 2020

    @pythonic2020

    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
  9. radarhere commented on Apr 26, 2020

    @radarhere
    Member

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

  10. added a commit that references this issue on Apr 26, 2020
  11. pythonic2020 commented on Apr 26, 2020

    @pythonic2020

    Excellent! When will it be up on conda forge?

  12. hugovk commented on Apr 26, 2020

    @hugovk
    Member

    We don't maintain the conda forge package, but see conda-forge/pillow-feedstock#78.

  13. TOUATI-MOHAMED commented on Apr 29, 2020

    @TOUATI-MOHAMED

    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"

  14. added a commit that references this issue on Jun 30, 2020
  15. added a commit that references this issue on Jul 20, 2022
  16. added
    Anaconda
    on May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    AnacondaBugPNG

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions