Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Tests/test_color_lut.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_correct_args(
def test_wrong_mode(
self, image_mode: str, lut_mode: str, table_channels: int, table_size: int
) -> None:
with pytest.raises(ValueError, match="wrong mode"):
with pytest.raises(ValueError, match="bands"):
im = Image.new(image_mode, (10, 10), 0)
im.im.color_lut_3d(
lut_mode,
Expand Down
4 changes: 3 additions & 1 deletion Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,9 @@ def test_merge_pa(self) -> None:
def test_merge_i(self) -> None:
i = Image.new("I", (1, 1))
a = Image.new("L", (1, 1))
with pytest.raises(ValueError, match="image has wrong mode"):
with pytest.raises(
ValueError, match="only modes 1, L or P can be combined with other images"
):
Image.merge("PA", (i, a))

def test_constants(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_image_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ def test_matrix_wrong_mode() -> None:
im = hopper("L")

# Act / Assert
with pytest.raises(ValueError, match="image has wrong mode"):
with pytest.raises(ValueError, match="only modes RGB, YCbCr, LAB or HSV supported"):
im.convert(mode="L", matrix=rgb2xyz_matrix[:4])


Expand Down
8 changes: 7 additions & 1 deletion Tests/test_image_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,13 @@ def test_blur_i16(

@pytest.mark.parametrize("mode", ("I;16", "I;16L", "I;16B", "I;16N"))
def test_unsharp_mask_i16(mode: str) -> None:
with pytest.raises(ValueError, match="image has wrong mode"):
with pytest.raises(
ValueError,
match=(
"only modes 1, L, LA, La, P, PA, RGB, RGBX, RGBA, CMYK, YCbCr, LAB or "
"HSV supported"
),
):
hopper(mode).filter(ImageFilter.UnsharpMask)


Expand Down
10 changes: 8 additions & 2 deletions Tests/test_image_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,19 @@ def histogram(mode: str) -> tuple[int, int, int]:
@pytest.mark.parametrize("mode", ("I", "I;16", "F"))
def test_histogram_masked_unsupported_mode(mode: str) -> None:
mask = Image.new("1", (128, 128))
with pytest.raises(ValueError, match="image has wrong mode"):
with pytest.raises(
ValueError,
match=(
"only modes 1, L, LA, La, P, PA, RGB, RGBX, RGBA, CMYK, YCbCr, LAB or "
"HSV supported"
),
):
hopper(mode).histogram(mask)


def test_histogram_mask_size_mismatch() -> None:
mask = Image.new("1", (1, 1))
with pytest.raises(ValueError, match="images do not match"):
with pytest.raises(ValueError, match="mask must have the same size as the image"):
hopper("L").histogram(mask)


Expand Down
2 changes: 1 addition & 1 deletion Tests/test_image_paste.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def test_overflow(self, box: tuple[int, int, int, int]) -> None:
im = Image.new("1", (1, 1))
im.paste(1, box)

with pytest.raises(ValueError, match="images do not match"):
with pytest.raises(ValueError, match="box must match image size"):
im.paste(im.copy(), box)

def test_incorrect_abbreviated_form(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_image_putalpha.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_promote() -> None:
assert im.mode == "RGBA"
assert im.getpixel((0, 0)) == (1, 2, 3, 4)

with pytest.raises(ValueError, match="image has wrong mode"):
with pytest.raises(ValueError, match="only modes RGB or RGBX supported"):
im.im.setalpha()


Expand Down
2 changes: 1 addition & 1 deletion Tests/test_image_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def split_open(mode: str) -> int:


def test_merge_single_band_mode_mismatch() -> None:
with pytest.raises(ValueError, match="images do not match"):
with pytest.raises(ValueError, match="images must have the same mode and size"):
Image.merge("F", [Image.new("L", (1, 1))])


Expand Down
2 changes: 1 addition & 1 deletion Tests/test_imagepalette.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_wrong_mode() -> None:
im.palette = ImagePalette.ImagePalette()
im.palette.mode = "L"
im.palette.dirty = 1
with pytest.raises(ValueError, match="image has wrong mode"):
with pytest.raises(ValueError, match="only modes RGB, RGBA or CMYK supported"):
im.load()


Expand Down
2 changes: 1 addition & 1 deletion Tests/test_imagewin.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_dib_paste_bbox(self) -> None:
# Assert
assert dib.size == (128, 128)

with pytest.raises(ValueError, match="images do not match"):
with pytest.raises(ValueError, match="box must match image size"):
dib.paste(im, (0, 0, 1, 1))

with pytest.raises(ValueError, match="left box co-ordinate cannot be negative"):
Expand Down
7 changes: 7 additions & 0 deletions docs/releasenotes/13.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,10 @@ GIMP gradients
:py:class:`~PIL.GimpGradientFile.GimpGradientFile` can now read gradient segments that
blend in HSV, as several of the gradients included with GIMP do, and segments that use
the step blending function added in GIMP 2.10.8. Previously, these raised errors.

More descriptive errors
^^^^^^^^^^^^^^^^^^^^^^^

Some errors raised by Pillow now describe the problem, rather than only saying
"image has wrong mode" or "images do not match". Code that matches on those messages
may need to be updated.
10 changes: 5 additions & 5 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,14 @@ ImagingError_MemoryError(void) {
}

void *
ImagingError_Mismatch(void) {
PyErr_SetString(PyExc_ValueError, "images do not match");
ImagingError_Mismatch(const char *message) {
PyErr_SetString(PyExc_ValueError, message);
return NULL;
}

void *
ImagingError_ModeError(void) {
PyErr_SetString(PyExc_ValueError, "image has wrong mode");
ImagingError_ModeError(const char *message) {
PyErr_SetString(PyExc_ValueError, message);
return NULL;
}

Expand Down Expand Up @@ -2380,7 +2380,7 @@ static PyObject *
im_setalpha_impl(ImagingObject *self, PyObject *args) {
Imaging im = self->image;
if (im->mode != IMAGING_MODE_RGB && im->mode != IMAGING_MODE_RGBX) {
return ImagingError_ModeError();
return ImagingError_ModeError("only modes RGB or RGBX supported");
}
im->mode = IMAGING_MODE_RGBA;
im->bands = 4;
Expand Down
4 changes: 2 additions & 2 deletions src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ _paste(ImagingDisplayObject *display, PyObject *args) {
if (xy[2] <= xy[0]) {
xy[2] = xy[0] + im->xsize;
} else if (xy[2] - xy[0] != im->xsize) {
return ImagingError_Mismatch();
return ImagingError_Mismatch("box must match image size");
}
if (xy[0] < 0) {
PyErr_SetString(PyExc_ValueError, "left box co-ordinate cannot be negative");
Expand All @@ -153,7 +153,7 @@ _paste(ImagingDisplayObject *display, PyObject *args) {
if (xy[3] <= xy[1]) {
xy[3] = xy[1] + im->ysize;
} else if (xy[3] - xy[1] != im->ysize) {
return ImagingError_Mismatch();
return ImagingError_Mismatch("box must match image size");
}
if (xy[1] < 0) {
PyErr_SetString(PyExc_ValueError, "upper box co-ordinate cannot be negative");
Expand Down
4 changes: 2 additions & 2 deletions src/libImaging/AlphaComposite.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ ImagingAlphaComposite(Imaging imDst, Imaging imSrc) {
}

if (imDst->mode != IMAGING_MODE_RGBA && imDst->mode != IMAGING_MODE_LA) {
return ImagingError_ModeError();
return ImagingError_ModeError("images must have mode RGBA or LA");
}

if (imDst->mode != imSrc->mode || imDst->xsize != imSrc->xsize ||
imDst->ysize != imSrc->ysize) {
return ImagingError_Mismatch();
return ImagingError_Mismatch("images must have the same mode and size");
}

imOut = ImagingNewDirty(imDst->mode, imDst->xsize, imDst->ysize);
Expand Down
47 changes: 36 additions & 11 deletions src/libImaging/Bands.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ImagingGetBand(Imaging imIn, int band) {
}

if (imIn->type != IMAGING_TYPE_UINT8) {
return (Imaging)ImagingError_ModeError();
return (Imaging)ImagingError_ModeError("only 8-bit images supported");
}

/* Special case for LXXA etc */
Expand Down Expand Up @@ -93,8 +93,13 @@ ImagingSplit(Imaging imIn, Imaging bands[4]) {
int i, j, x, y;

/* Check arguments */
if (!imIn || imIn->type != IMAGING_TYPE_UINT8) {
(void)ImagingError_ModeError();
if (!imIn) {
(void)ImagingError_ValueError(NULL);
return -1;
}

if (imIn->type != IMAGING_TYPE_UINT8) {
(void)ImagingError_ModeError("only 8-bit images supported");
return -1;
}

Expand Down Expand Up @@ -203,8 +208,12 @@ ImagingPutBand(Imaging imOut, Imaging imIn, int band) {
int x, y;

/* Check arguments */
if (!imIn || imIn->bands != 1 || !imOut) {
return (Imaging)ImagingError_ModeError();
if (!imIn || !imOut) {
return (Imaging)ImagingError_ValueError(NULL);
}

if (imIn->bands != 1) {
return (Imaging)ImagingError_ModeError("source image must have exactly 1 band");
}

if (band < 0 || band >= imOut->bands) {
Expand All @@ -213,7 +222,9 @@ ImagingPutBand(Imaging imOut, Imaging imIn, int band) {

if (imIn->type != imOut->type || imIn->xsize != imOut->xsize ||
imIn->ysize != imOut->ysize) {
return (Imaging)ImagingError_Mismatch();
return (Imaging)ImagingError_Mismatch(
"images must have the same type and size"
);
}

/* Shortcuts */
Expand Down Expand Up @@ -252,8 +263,15 @@ ImagingFillBand(Imaging imOut, int band, int color) {
int x, y;

/* Check arguments */
if (!imOut || imOut->type != IMAGING_TYPE_UINT8) {
return (Imaging)ImagingError_ModeError();
if (!imOut) {
return (Imaging)ImagingError_ValueError(NULL);
}

if (imOut->type != IMAGING_TYPE_UINT8) {
return (Imaging)ImagingError_ModeError(
"only modes 1, L, LA, La, P, PA, RGB, RGBX, RGBA, CMYK, YCbCr, LAB or HSV "
"supported"
);
}

if (band < 0 || band >= imOut->bands) {
Expand Down Expand Up @@ -304,12 +322,19 @@ ImagingMerge(const ModeID mode, Imaging bands[4]) {
}
}
for (i = 0; i < bandsCount; ++i) {
if (bands[i]->bands != 1 || (bandsCount != 1 && bands[i]->pixelsize == 4)) {
return (Imaging)ImagingError_ModeError();
if (bands[i]->bands != 1) {
return (Imaging)ImagingError_ModeError(
"images to be merged must be single-band"
);
}
if (bandsCount != 1 && bands[i]->pixelsize == 4) {
return (Imaging)ImagingError_ModeError(
"only modes 1, L or P can be combined with other images"
);
}
if (bands[i]->xsize != firstBand->xsize ||
bands[i]->ysize != firstBand->ysize) {
return (Imaging)ImagingError_Mismatch();
return (Imaging)ImagingError_Mismatch("images must have the same size");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libImaging/Blend.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha) {

if ((imIn1->type != IMAGING_TYPE_UINT8 && imIn1->type != IMAGING_TYPE_I16) ||
imIn1->palette || imIn1->mode == IMAGING_MODE_1) {
return ImagingError_ModeError();
return ImagingError_ModeError("modes 1, P, PA, I and F not supported");
}

if (imIn1->mode != imIn2->mode || imIn1->xsize != imIn2->xsize ||
imIn1->ysize != imIn2->ysize) {
return ImagingError_Mismatch();
return ImagingError_Mismatch("images must have the same mode and size");
}

/* Shortcuts */
Expand Down
7 changes: 5 additions & 2 deletions src/libImaging/BoxBlur.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ ImagingBoxBlur(Imaging imOut, Imaging imIn, float xradius, float yradius, int n)
if (imIn->mode != imOut->mode || imIn->type != imOut->type ||
imIn->bands != imOut->bands || imIn->xsize != imOut->xsize ||
imIn->ysize != imOut->ysize) {
return ImagingError_Mismatch();
return ImagingError_Mismatch("images must have the same mode and size");
}

if (!(imIn->type == IMAGING_TYPE_UINT8 &&
Expand All @@ -355,7 +355,10 @@ ImagingBoxBlur(Imaging imOut, Imaging imIn, float xradius, float yradius, int n)
imIn->mode == IMAGING_MODE_CMYK || imIn->mode == IMAGING_MODE_L ||
imIn->mode == IMAGING_MODE_LA || imIn->mode == IMAGING_MODE_La)) &&
imIn->type != IMAGING_TYPE_I16) {
return ImagingError_ModeError();
return ImagingError_ModeError(
"only modes L, LA, La, RGB, RGBA, RGBa, RGBX, CMYK or I;16 variants "
"supported"
);
}

/* Apply blur in one dimension.
Expand Down
9 changes: 6 additions & 3 deletions src/libImaging/Chops.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
int x, y; \
if (imMode == IMAGING_MODE_1 && \
(imIn1->mode != IMAGING_MODE_1 || imIn2->mode != IMAGING_MODE_1)) { \
return ImagingError_ModeError(); \
return ImagingError_ModeError("images must have mode 1"); \
} \
Imaging imOut = create(imIn1, imIn2); \
if (!imOut) { \
Expand All @@ -74,10 +74,13 @@ create(Imaging im1, Imaging im2) {
}

if (im1->type != IMAGING_TYPE_UINT8) {
return (Imaging)ImagingError_ModeError();
return (Imaging)ImagingError_ModeError(
"only modes 1, L, LA, La, P, PA, RGB, RGBX, RGBA, CMYK, YCbCr, LAB or HSV "
"supported"
);
}
if (im1->type != im2->type || im1->bands != im2->bands) {
return (Imaging)ImagingError_Mismatch();
return (Imaging)ImagingError_Mismatch("images must have compatible modes");
}

int xsize = im1->xsize < im2->xsize ? im1->xsize : im2->xsize;
Expand Down
22 changes: 18 additions & 4 deletions src/libImaging/ColorLUT.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,28 @@ ImagingColorLUT3D_linear(
return NULL;
}

if (imIn->type != IMAGING_TYPE_UINT8 || imOut->type != IMAGING_TYPE_UINT8 ||
imIn->bands < 3 || imOut->bands < table_channels) {
return (Imaging)ImagingError_ModeError();
if (imIn->type != IMAGING_TYPE_UINT8 || imOut->type != IMAGING_TYPE_UINT8) {
return (Imaging)ImagingError_ModeError(
"only modes 1, L, LA, La, P, PA, RGB, RGBX, RGBA, CMYK, YCbCr, LAB or "
"HSV supported"
);
}

if (imIn->bands < 3) {
return (Imaging)ImagingError_ModeError("image must have at least 3 bands");
}

if (imOut->bands < table_channels) {
return (Imaging)ImagingError_ModeError(
"mode needs at least as many bands as the number of channels"
);
}

/* In case we have one extra band in imOut and don't have in imIn.*/
if (imOut->bands > table_channels && imOut->bands > imIn->bands) {
return (Imaging)ImagingError_ModeError();
return (Imaging)ImagingError_ModeError(
"mode has more bands than the image and the number of channels"
);
}

ImagingSectionEnter(&cookie);
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/Copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Imaging
ImagingCopyInto(Imaging imOut, Imaging imIn) {
if (imOut->mode != imIn->mode || imOut->xsize != imIn->xsize ||
imOut->ysize != imIn->ysize) {
return ImagingError_Mismatch();
return ImagingError_Mismatch("images must have the same mode and size");
}

_copy(imOut, imIn);
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/Dib.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ImagingNewDIB(const ModeID mode, int xsize, int ysize) {

/* Check mode */
if (mode != IMAGING_MODE_1 && mode != IMAGING_MODE_L && mode != IMAGING_MODE_RGB) {
return (ImagingDIB)ImagingError_ModeError();
return (ImagingDIB)ImagingError_ModeError("only modes 1, L or RGB supported");
}

const int pixelsize = mode == IMAGING_MODE_RGB ? 3 : 1;
Expand Down
Loading
Loading