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

Allow converting an image to a numpy array to raise errors #5379

Merged
merged 1 commit into from May 20, 2021

Conversation

radarhere
Copy link
Member

Resolves #5084

The following code runs without raising an error.

from PIL import Image
import numpy as np
im = Image.open("Tests/images/truncated_jpeg.jpg")
np.array(im)

Even though an OSError is raised if __array_interface__ is accessed directly.

from PIL import Image
im = Image.open("Tests/images/truncated_jpeg.jpg")
im.__array_interface__

This is a bug in numpy.

This PR works around that bug by replacing __array_interface__ with __array__.

@hugovk
Copy link
Member

hugovk commented Apr 3, 2021

Thanks for chasing up. Let's open an issue at NumPy as well?

https://gitter.im/numpy/numpy?at=6067b4ef3153ce63a3b6dcad says:

Its a bug, but I couldn't just rip the bandaid off it because of shapely. Should do that, but a PR to do a deprecation would be welcome also of course ;)

Maybe they won't have it deprecated and fixed by the next Pillow release in 3 months but let's at least give them a chance, and review this before release. I've added it to the 8.3.0 milestone.

@radarhere
Copy link
Member Author

Ok, raised at numpy/numpy#18723

@radarhere
Copy link
Member Author

radarhere commented May 19, 2021

NumPy 1.21 will deprecate the error catching, raising a warning in this situation.

It is expected that NumPy 1.23, a year later, would start to raise an error.

@hugovk
Copy link
Member

hugovk commented May 20, 2021

Thanks for following up!

@radarhere
Copy link
Member Author

numpy/numpy#20835 has been merged into NumPy, so NumPy 1.23.should raise an error.

@radarhere
Copy link
Member Author

NumPy 1.23 has been released, so I've created #6394 to revert this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

np.array(img) should not hide errors for corrupted images
2 participants