python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.11k stars 2.22k forks source link

`ValueError: encoding error 5` during WEBP encoding should be replaced with something more informative #8321

Closed MikiP98 closed 4 weeks ago

MikiP98 commented 1 month ago

What did you do?

I tried to save an 4x upscaled image with WEBP format exceeding the WEBP image size limit

What did you expect to happen?

I expected the image to be saved The error message should be more informative, it should say something like The image is too big for the WEBP format

What actually happened?

Program crashed with:

...
File "C:\Program Files\Python312\Lib\site-packages\PIL\Image.py", line 2439, in save
    save_handler(self, fp, filename)
  File "C:\Program Files\Python312\Lib\site-packages\PIL\WebPImagePlugin.py", line 340, in _save
    data = _webp.WebPEncode(
           ^^^^^^^^^^^^^^^^^
ValueError: encoding error 5

What are your OS, Python and Pillow versions?

--------------------------------------------------------------------
Pillow 10.2.0
Python 3.12.1 (tags/v3.12.1:2305ca5, Dec  7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)]
--------------------------------------------------------------------
Python modules loaded from C:\Program Files\Python312\Lib\site-packages\PIL
Binary modules loaded from C:\Program Files\Python312\Lib\site-packages\PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 10.2.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.13.2
--- LITTLECMS2 support ok, loaded 2.16
--- WEBP support ok, loaded 1.3.2
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for libjpeg-turbo 3.0.1
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.0
--- ZLIB (PNG/ZIP) support ok, loaded 1.3
--- LIBTIFF support ok, loaded 4.6.0
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------
BLP
Extensions: .blp
Features: open, save, encode
--------------------------------------------------------------------
BMP image/bmp
Extensions: .bmp
Features: open, save
--------------------------------------------------------------------
BUFR
Extensions: .bufr
Features: open, save
--------------------------------------------------------------------
CUR
Extensions: .cur
Features: open
--------------------------------------------------------------------
DCX
Extensions: .dcx
Features: open
--------------------------------------------------------------------
DDS
Extensions: .dds
Features: open, save
--------------------------------------------------------------------
DIB image/bmp
Extensions: .dib
Features: open, save
--------------------------------------------------------------------
EPS application/postscript
Extensions: .eps, .ps
Features: open, save
--------------------------------------------------------------------
FITS
Extensions: .fit, .fits
Features: open
--------------------------------------------------------------------
FLI
Extensions: .flc, .fli
Features: open
--------------------------------------------------------------------
FTEX
Extensions: .ftc, .ftu
Features: open
--------------------------------------------------------------------
GBR
Extensions: .gbr
Features: open
--------------------------------------------------------------------
GIF image/gif
Extensions: .gif
Features: open, save, save_all
--------------------------------------------------------------------
GRIB
Extensions: .grib
Features: open, save
--------------------------------------------------------------------
HDF5
Extensions: .h5, .hdf
Features: open, save
--------------------------------------------------------------------
ICNS image/icns
Extensions: .icns
Features: open, save
--------------------------------------------------------------------
ICO image/x-icon
Extensions: .ico
Features: open, save
--------------------------------------------------------------------
IM
Extensions: .im
Features: open, save
--------------------------------------------------------------------
IMT
Features: open
--------------------------------------------------------------------
IPTC
Extensions: .iim
Features: open
--------------------------------------------------------------------
JPEG image/jpeg
Extensions: .jfif, .jpe, .jpeg, .jpg
Features: open, save
--------------------------------------------------------------------
JPEG2000 image/jp2
Extensions: .j2c, .j2k, .jp2, .jpc, .jpf, .jpx
Features: open, save
--------------------------------------------------------------------
MCIDAS
Features: open
--------------------------------------------------------------------
MPEG video/mpeg
Extensions: .mpeg, .mpg
Features: open
--------------------------------------------------------------------
MSP
Extensions: .msp
Features: open, save, decode
--------------------------------------------------------------------
PCD
Extensions: .pcd
Features: open
--------------------------------------------------------------------
PCX image/x-pcx
Extensions: .pcx
Features: open, save
--------------------------------------------------------------------
PIXAR
Extensions: .pxr
Features: open
--------------------------------------------------------------------
PNG image/png
Extensions: .apng, .png
Features: open, save, save_all
--------------------------------------------------------------------
PPM image/x-portable-anymap
Extensions: .pbm, .pgm, .pnm, .ppm
Features: open, save
--------------------------------------------------------------------
PSD image/vnd.adobe.photoshop
Extensions: .psd
Features: open
--------------------------------------------------------------------
QOI
Extensions: .qoi
Features: open
--------------------------------------------------------------------
SGI image/sgi
Extensions: .bw, .rgb, .rgba, .sgi
Features: open, save
--------------------------------------------------------------------
SPIDER
Features: open, save
--------------------------------------------------------------------
SUN
Extensions: .ras
Features: open
--------------------------------------------------------------------
TGA image/x-tga
Extensions: .icb, .tga, .vda, .vst
Features: open, save
--------------------------------------------------------------------
TIFF image/tiff
Extensions: .tif, .tiff
Features: open, save, save_all
--------------------------------------------------------------------
WEBP image/webp
Extensions: .webp
Features: open, save, save_all
--------------------------------------------------------------------
WMF
Extensions: .emf, .wmf
Features: open, save
--------------------------------------------------------------------
XBM image/xbm
Extensions: .xbm
Features: open, save
--------------------------------------------------------------------
XPM image/xpm
Extensions: .xpm
Features: open
--------------------------------------------------------------------
XVTHUMB
Features: open
--------------------------------------------------------------------
# coding=utf-8
import PIL.Image

if __name__ == "__main__":
    image = PIL.Image.open(r"..\..\input\webp_test.png")

    w, h = image.size
    scaled_image = image.resize((w * 4, h * 4), PIL.Image.Resampling.LANCZOS)

    optional_args = {
        'lossless': True,
        'method': 6,
        'optimize': True,
        'format': 'WEBP'
    }
    scaled_image.save(r"..\..\output\webp_test.webp", **optional_args)

webp_test

radarhere commented 1 month ago

You are trying to save an image that is 22880 x 16128.

https://developers.google.com/speed/webp/faq

The maximum pixel dimensions of a WebP image is 16383 x 16383.

MikiP98 commented 1 month ago

I'm sorry to waste your time then.

I still think the error message should be improved. It can be catched and rewriten as something like: The image is too big for the WEBP format

I will keep this open for now.

radarhere commented 1 month ago

I've created #8322 to update the error message to "encoding error 5: Image size exceeds WebP limit"