Pillow
Pillow copied to clipboard
'None or too many quantization tables' ValueError on some images
What did you do?
from io import BytesIO
from PIL import Image
image = Image.open(fp)
buf = BytesIO()
image.save(buf, format='JPEG', quality='keep', optimize=True)
What did you expect to happen?
Successful image saving.
What actually happened?
File ".../python2.7/site-packages/PIL/Image.py", line 2084, in save
save_handler(self, fp, filename)
File "../python2.7/site-packages/PIL/JpegImagePlugin.py", line 699, in _save
qtables = validate_qtables(qtables)
File "../python2.7/site-packages/PIL/JpegImagePlugin.py", line 683, in validate_qtables
raise ValueError("None or too many quantization tables")
ValueError: None or too many quantization tables
What are your OS, Python and Pillow versions?
- OS: ArchLinux 5.7.10-arch1-1 x86_64 GNU/Linux
- Python: 2.7.17, 3.8.2
- Pillow: 6.2.2, 7.1.2
Bug can be reproduced only with some images (see attached one), I'm not sure what's "wrong" with this image, it has progressive encoding and seems to lack a color profile, but most tools (ffmpeg, ImageMagic) work with it without any issues.
Bug can be reproduced on both Python 2 (Pillow 6.2.2) and Python 3 (Pillow 7.1.2).
My workaround is to set quality=100 instead of quality='keep'.
The file has 16-bit quantization tables, which are not yet supported: https://github.com/python-pillow/Pillow/blob/e0eec1eb566f10695077cb6f6b700956db0c8df8/src/PIL/JpegImagePlugin.py#L254 https://github.com/python-pillow/Pillow/blob/e0eec1eb566f10695077cb6f6b700956db0c8df8/src/libImaging/JpegEncode.c#L162
Thanks to #4918, the error is no longer raised.
@gofr in it's final form, was #4918 still just a "partial fix" for this, or do you think this is fully resolved?
@radarhere It was a full fix. I think this is fully resolved.
When I started on it I didn't fully understand the C code/libjpeg yet. I expected that to need more changes. But it didn't.
Thanks