Skip to content

Cannot convert high resolution jpg file to webp #5461

Closed

Description

@ma-tongji

What did you do?

I read in a high resolution jpg image, and convert it to webp format.

What did you expect to happen?

I expected to get a image file with webp format

What actually happened?

I have a image with resolution 4500*6500 and when I direct save this image as webp format, Pillow gives me the error:

Traceback (most recent call last):
  File "/Users/martin/software/miniconda3/envs/py36/lib/python3.6/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<console>", line 1, in <module>
  File "/Users/martin/software/miniconda3/envs/py36/lib/python3.6/site-packages/PIL/Image.py", line 2172, in save
    save_handler(self, fp, filename)
  File "/Users/martin/software/miniconda3/envs/py36/lib/python3.6/site-packages/PIL/WebPImagePlugin.py", line 337, in _save
    xmp,
ValueError: encoding error

I also tried to crop this image to 4500*4500 and save it to webp format. And I get the same "ValueError: encoding error" error.

I tried to crop this image to 4000*4000 and save it to webp format. This time, there's no error and I get the result webp file.

What are your OS, Python and Pillow versions?

OS: Mac OS X El Capitan (10.11.6)
Python: 3.6.4
Pillow: 8.2.0

features.version('webp') returns '1.2.0'

This is my code:

from PIL import Image as PILimage

infile = 'tests/xxxxx.jpg'
outfile = 'tests/webp.webp'

p = PILimage.open(infile)
p2 = p.crop((0,0, 4300, 4300))
p2.save(outfile, format='webp', quality=90, optimize=True, progressive=True)

Activity

  1. radarhere commented on May 2, 2021

    @radarhere
    Member

    Could we get a copy of "xxxxx.jpg"?

  2. ma-tongji commented on May 3, 2021

    @ma-tongji
    Author

    Could we get a copy of "xxxxx.jpg"?

    I upload a copy at: http://47.74.156.104/89218229_p0.jpg.zip

  3. radarhere commented on May 3, 2021

    @radarhere
    Member

    Strangely enough, I find that if I specify method to a value greater than 1 in the last line of your code,

    p2.save(outfile, format='webp', quality=90, optimize=True, progressive=True, method=4)

    it works. In the next version of Pillow, #5450 will mean that the new default will be 4 - so you will no longer have to add on method to fix this issue.

  4. ma-tongji commented on May 3, 2021

    @ma-tongji
    Author

    @radarhere Thanks, I will try the method argument first for now.

  5. radarhere commented on May 3, 2021

    @radarhere
    Member

    Looking at the error that is thrown, it is error code 6 from webp, PARTITION0_OVERFLOW - "partition is bigger than 512k". So it makes sense that a larger image is triggering this.

    This might just be how webp chooses to behave? I'm not personally in favour of automatically restricting the quality of the image to fit that. The only amends we could do might be to show a clearer error message.

  6. radarhere commented on May 4, 2021

    @radarhere
    Member

    @ma-tongji to check, are you satisfied with this as a solution for your problem?

  7. radarhere commented on May 6, 2021

    @radarhere
    Member

    I've created PR #5471 to show the error code - 6 in this case - to the user, so that this would be clearer in future Pillow versions.

  8. ma-tongji commented on May 9, 2021

    @ma-tongji
    Author

    @radarhere This make sense. I also tried to save as webp in my windows 10 computer with mode=4 and it also gave me the xmp error. But if I reduce the quality from 90 to 85, this process will work fine.

  9. SLAPaper commented on Aug 23, 2023

    @SLAPaper

    Hi, if I get error 6 when saving webp (tried through method from 0 to 6), what can I do to avoid it?
    It seems that this issue only improve error reporting, rather than giving a way to fix it

  10. radarhere commented on Aug 23, 2023

    @radarhere
    Member

    I suspect you could reduce the quality when saving. Could you attach a copy of the image?

  11. SLAPaper commented on Aug 23, 2023

    @SLAPaper

    well, it's hard to get the exactly the original image, since I'm not using Pillow directly, but getting the error when generating a big xyz plot in A1111 stable diffusion webui

    according to cwebp's document about -partition_limit, I would suspect that webp not only has a hard coded size limit of 16383 pixels, but also has a soft limit of "Partition0 Size" when:

    1. total pixels go near 16383*16383,
    2. and/or has complex content
    3. and/or set to high quality setting

    at the same time

    I add a detection to change the file format to jpeg or png when total pixels > 16383**2//2 to workaround that.

    maybe Pillow can expose other cwebp parameters so that it can be further diagnosing?

  12. radarhere commented on Aug 23, 2023

    @radarhere
    Member

    If you're not using Pillow directly, how would you plan to address this with whatever advice we might provide?

  13. SLAPaper commented on Aug 23, 2023

    @SLAPaper

    Just found this issue that have related errorcode reporting and wonder whether there is something worth discuss.
    Sorry for disturbing

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions