Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nabobalis opened this issue Apr 3, 2020 · 10 comments · Fixed by #4528
Closed

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

nabobalis opened this issue Apr 3, 2020 · 10 comments · Fixed by #4528
Labels
Anaconda Issues with Anaconda's Pillow Bug Any unexpected behavior, until confirmed feature.

Comments

@nabobalis
Copy link

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.

@pmrowla
Copy link
Contributor

pmrowla commented Apr 3, 2020

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

@hugovk
Copy link
Member

hugovk commented Apr 6, 2020

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!

@nabobalis
Copy link
Author

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

@hugovk hugovk added the Bug Any unexpected behavior, until confirmed feature. label Apr 6, 2020
@aderchox
Copy link

aderchox commented Apr 20, 2020

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'

@radarhere
Copy link
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

@pythonic2020
Copy link

pythonic2020 commented Apr 23, 2020

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

@radarhere
Copy link
Member

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

@pythonic2020
Copy link

Excellent! When will it be up on conda forge?

@hugovk
Copy link
Member

hugovk commented Apr 26, 2020

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

@TOUATI-MOHAMED
Copy link

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"

rosbo added a commit to Kaggle/docker-python that referenced this issue Jun 30, 2020
The base image includes Pillow 7.1.0 which has a known issue: python-pillow/Pillow#4518

This is causing the skimage test to fail.

BUG=160263325
mhsmith added a commit to chaquo/chaquopy that referenced this issue Jul 20, 2022
Previous version had a problem with scikit-image (python-pillow/Pillow#4518)
@aclark4life aclark4life added the Anaconda Issues with Anaconda's Pillow label May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Anaconda Issues with Anaconda's Pillow Bug Any unexpected behavior, until confirmed feature.
Projects
None yet
8 participants