Skip to content

Commit 3257d4b

Browse files
committed
Updated error message for invalid width or height
1 parent f0d8fd3 commit 3257d4b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Tests/test_file_webp.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ def test_write_encoding_error_message(self, tmp_path: Path) -> None:
157157
im.save(temp_file, method=0)
158158
assert str(e.value) == "encoding error 6"
159159

160+
def test_write_encoding_error_bad_dimension(self, tmp_path: Path) -> None:
161+
temp_file = str(tmp_path / "temp.webp")
162+
im = Image.new("L", (16384, 16384))
163+
with pytest.raises(ValueError) as e:
164+
im.save(temp_file)
165+
assert str(e.value) == "encoding error 5: Image size exceeds WebP limit"
166+
160167
def test_WebPEncode_with_invalid_args(self) -> None:
161168
"""
162169
Calling encoder functions with no arguments should result in an error.

src/_webp.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,12 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) {
672672

673673
WebPPictureFree(&pic);
674674
if (!ok) {
675-
PyErr_Format(PyExc_ValueError, "encoding error %d", (&pic)->error_code);
675+
int error_code = (&pic)->error_code;
676+
const char *message = "";
677+
if (error_code == VP8_ENC_ERROR_BAD_DIMENSION) {
678+
message = ": Image size exceeds WebP limit";
679+
}
680+
PyErr_Format(PyExc_ValueError, "encoding error %d%s", error_code, message);
676681
return NULL;
677682
}
678683
output = writer.mem;

0 commit comments

Comments
 (0)