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

Changed error type to allow for incremental WebP parsing #5404

Merged
merged 1 commit into from Dec 28, 2021

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Apr 14, 2021

Resolves #4745

Running code from the issue on a test image,

from PIL import ImageFile

p = ImageFile.Parser()
with open("Tests/images/hopper.webp", "rb") as f:
    new_data = f.read(1024)
    while not p.image and new_data:
        p.feed(new_data)

an error is triggered.

PyErr_SetString(PyExc_RuntimeError, "could not create decoder object");

This is effectively an error being raised because the the image is truncated, and most truncation errors are OSError. So if we change it to an OSError, then it is handled by feed().

Pillow/src/PIL/ImageFile.py

Lines 413 to 418 in 45003b7

try:
with io.BytesIO(self.data) as fp:
im = Image.open(fp)
except OSError:
# traceback.print_exc()
pass # not enough data

This fixes the problem.

@wiredfool
Copy link
Member

@wiredfool wiredfool commented Apr 14, 2021

We should really discourage using this interface. While it's faster in some cases, when it doesn't work, it doesn't work in O(n^2).

@radarhere
Copy link
Member Author

@radarhere radarhere commented Apr 14, 2021

That inefficiency should be apparent to the user though - our API doesn't do anything to obscure it.

Are you saying that we shouldn't improve Parser any further, closing the issue as a 'wontfix'?

@wiredfool
Copy link
Member

@wiredfool wiredfool commented Apr 14, 2021

I'm not sure.

I'm pretty sure for most of the more recent formats that use external libraries, like jpeg2k and webp, probably the avif are going to to require the whole image eventually. It's also not uncommon that tiff style images have IFDs that are at the end of the file.

In that case, you're calling the parser with the first 1k, then 2k, then 3k, then 4k, and so on.

Metadata only, maybe ok.

@hugovk
Copy link
Member

@hugovk hugovk commented Jun 28, 2021

We should really discourage using this interface. While it's faster in some cases, when it doesn't work, it doesn't work in O(n^2).

Shall we merge this improvement and then deprecate the API?

It can then be made private and/or removed in Pillow 10.0.0 (2023-01-02) (2023-07-01).

hugovk
hugovk approved these changes Dec 28, 2021
@radarhere radarhere merged commit d8f2fb5 into python-pillow:main Dec 28, 2021
52 checks passed
@radarhere radarhere deleted the feed branch Dec 28, 2021
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.

3 participants