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

Cannot convert high resolution jpg file to webp #5461

Closed
ma-tongji opened this issue May 2, 2021 · 13 comments · Fixed by #5471
Closed

Cannot convert high resolution jpg file to webp #5461

ma-tongji opened this issue May 2, 2021 · 13 comments · Fixed by #5471
Labels

Comments

@ma-tongji
Copy link

ma-tongji commented May 2, 2021

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

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

@ma-tongji
Copy link
Author

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

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

@radarhere
Copy link
Member

radarhere commented May 3, 2021

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.

@ma-tongji
Copy link
Author

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

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

@radarhere
Copy link
Member

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

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

@ma-tongji
Copy link
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.

@SLAPaper
Copy link

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

@radarhere
Copy link
Member

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

@SLAPaper
Copy link

SLAPaper commented Aug 23, 2023

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?

@radarhere
Copy link
Member

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

@SLAPaper
Copy link

SLAPaper commented Aug 23, 2023

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
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants