From cb1bc2aaf55d6e086b178f89a95f669d850713a8 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 27 Aug 2026 19:46:30 +1000 Subject: [PATCH 1/5] Remove ImagingConvert2 --- src/_imaging.c | 8 ++++++-- src/libImaging/Convert.c | 11 ++++------- src/libImaging/Imaging.h | 6 +++--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/_imaging.c b/src/_imaging.c index 04b5da3f68c..62eabbf1fc7 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -1023,7 +1023,11 @@ _convert(ImagingObject *self, PyObject *args) { const ModeID mode = findModeID(mode_name); return PyImagingNew(ImagingConvert( - self->image, mode, paletteimage ? paletteimage->image->palette : NULL, dither + NULL, + self->image, + mode, + paletteimage ? paletteimage->image->palette : NULL, + dither )); } @@ -1034,7 +1038,7 @@ _convert2(ImagingObject *self, PyObject *args) { return NULL; } - if (!ImagingConvert2(imagep->image, self->image)) { + if (!ImagingConvert(imagep->image, self->image, imagep->image->mode, NULL, 0)) { return NULL; } diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 0f962a9fcae..4e8e5249546 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -1645,13 +1645,10 @@ convert(Imaging imOut, Imaging imIn, ModeID mode, ImagingPalette palette, int di } Imaging -ImagingConvert(Imaging imIn, const ModeID mode, ImagingPalette palette, int dither) { - return convert(NULL, imIn, mode, palette, dither); -} - -Imaging -ImagingConvert2(Imaging imOut, Imaging imIn) { - return convert(imOut, imIn, imOut->mode, NULL, 0); +ImagingConvert( + Imaging imOut, Imaging imIn, const ModeID mode, ImagingPalette palette, int dither +) { + return convert(imOut, imIn, mode, palette, dither); } Imaging diff --git a/src/libImaging/Imaging.h b/src/libImaging/Imaging.h index 472bda5d0fd..72de8a894f7 100644 --- a/src/libImaging/Imaging.h +++ b/src/libImaging/Imaging.h @@ -303,7 +303,9 @@ ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha); extern Imaging ImagingCopy(Imaging im); extern Imaging -ImagingConvert(Imaging im, ModeID mode, ImagingPalette palette, int dither); +ImagingConvert( + Imaging imOut, Imaging imIn, ModeID mode, ImagingPalette palette, int dither +); extern Imaging ImagingConvertInPlace(Imaging im, ModeID mode); extern Imaging @@ -417,8 +419,6 @@ ImagingColorLUT3D_linear( extern Imaging ImagingCopy2(Imaging imOut, Imaging imIn); -extern Imaging -ImagingConvert2(Imaging imOut, Imaging imIn); /* Channel operations */ /* any mode, except "F" */ From 669cce6ab013de68f4e9c7b7770405ecebe3477b Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 27 Aug 2026 21:05:32 +1000 Subject: [PATCH 2/5] Rename convert2 to convert_into --- src/PIL/ImageTk.py | 2 +- src/_imaging.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/PIL/ImageTk.py b/src/PIL/ImageTk.py index a837cf77a51..ad7f841db25 100644 --- a/src/PIL/ImageTk.py +++ b/src/PIL/ImageTk.py @@ -183,7 +183,7 @@ def paste(self, im: Image.Image) -> None: image = im.im if not image.isblock() or im.mode != self.__mode: block = Image.core.new_block(self.__mode, im.size) - image.convert2(block) # convert directly between buffers + image.convert_into(block) # convert directly between buffers ptr = block.ptr _pyimagingtkcall("PyImagingPhoto", self.__photo, ptr) diff --git a/src/_imaging.c b/src/_imaging.c index 62eabbf1fc7..4f559a7f724 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -1032,7 +1032,7 @@ _convert(ImagingObject *self, PyObject *args) { } static PyObject * -_convert2(ImagingObject *self, PyObject *args) { +_convert_into(ImagingObject *self, PyObject *args) { ImagingObject *imagep; if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; @@ -3706,7 +3706,7 @@ static struct PyMethodDef methods[] = { /* Standard processing methods (Image) */ {"color_lut_3d", (PyCFunction)_color_lut_3d, METH_VARARGS}, {"convert", (PyCFunction)_convert, METH_VARARGS}, - {"convert2", (PyCFunction)_convert2, METH_VARARGS}, + {"convert_into", (PyCFunction)_convert_into, METH_VARARGS}, {"convert_matrix", (PyCFunction)_convert_matrix, METH_VARARGS}, {"convert_transparent", (PyCFunction)_convert_transparent, METH_VARARGS}, {"copy", (PyCFunction)_copy, METH_VARARGS}, From a8f9c37532e18b19c4e02c2120a5c3aa76968197 Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Thu, 27 Aug 2026 21:50:47 +1000 Subject: [PATCH 3/5] Add docstring Co-authored-by: Aarni Koskela --- src/libImaging/Convert.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 4e8e5249546..b7f0edac43a 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -1644,6 +1644,22 @@ convert(Imaging imOut, Imaging imIn, ModeID mode, ImagingPalette palette, int di return imOut; } +/** + * Convert imIn to `mode`. + * If imIn is already in `mode`, this performs a copy into imOut + * (or a newly allocated image if imOut is NULL). + * + * @param imOut Existing image to write into + * (must already be in `mode` and the same size as imIn), + * or NULL to allocate a new image for the result. + * @param imIn Source image to convert. + * @param mode Target mode. + * @param palette Target palette for conversions to "P"/"PA"; + * NULL to use/generate a default palette. + * @param dither Nonzero to dither when converting to "P"/"PA" or "1". + * @return The resulting Imaging object, + * or NULL with a Python exception set on failure. + */ Imaging ImagingConvert( Imaging imOut, Imaging imIn, const ModeID mode, ImagingPalette palette, int dither From 10f69c5480ca56162c19cdf7d926960f083de0de Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 27 Aug 2026 21:52:35 +1000 Subject: [PATCH 4/5] Remove convert() --- src/libImaging/Convert.c | 45 ++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index b7f0edac43a..dfc3807a0ee 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -1575,8 +1575,26 @@ static struct { {IMAGING_MODE_I_16B, IMAGING_MODE_F, I16B_F} }; -static Imaging -convert(Imaging imOut, Imaging imIn, ModeID mode, ImagingPalette palette, int dither) { +/** + * Convert imIn to `mode`. + * If imIn is already in `mode`, this performs a copy into imOut + * (or a newly allocated image if imOut is NULL). + * + * @param imOut Existing image to write into + * (must already be in `mode` and the same size as imIn), + * or NULL to allocate a new image for the result. + * @param imIn Source image to convert. + * @param mode Target mode. + * @param palette Target palette for conversions to "P"/"PA"; + * NULL to use/generate a default palette. + * @param dither Nonzero to dither when converting to "P"/"PA" or "1". + * @return The resulting Imaging object, + * or NULL with a Python exception set on failure. + */ +Imaging +ImagingConvert( + Imaging imOut, Imaging imIn, ModeID mode, ImagingPalette palette, int dither +) { ImagingSectionCookie cookie; ImagingShuffler convert; @@ -1644,29 +1662,6 @@ convert(Imaging imOut, Imaging imIn, ModeID mode, ImagingPalette palette, int di return imOut; } -/** - * Convert imIn to `mode`. - * If imIn is already in `mode`, this performs a copy into imOut - * (or a newly allocated image if imOut is NULL). - * - * @param imOut Existing image to write into - * (must already be in `mode` and the same size as imIn), - * or NULL to allocate a new image for the result. - * @param imIn Source image to convert. - * @param mode Target mode. - * @param palette Target palette for conversions to "P"/"PA"; - * NULL to use/generate a default palette. - * @param dither Nonzero to dither when converting to "P"/"PA" or "1". - * @return The resulting Imaging object, - * or NULL with a Python exception set on failure. - */ -Imaging -ImagingConvert( - Imaging imOut, Imaging imIn, const ModeID mode, ImagingPalette palette, int dither -) { - return convert(imOut, imIn, mode, palette, dither); -} - Imaging ImagingConvertTransparent(Imaging imIn, const ModeID mode, int r, int g, int b) { ImagingSectionCookie cookie; From c6057bc02bfd7229632340b772db413aa801822e Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Fri, 28 Aug 2026 10:11:08 +1000 Subject: [PATCH 5/5] Update docstring wording --- src/libImaging/Convert.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index dfc3807a0ee..cd5017ff358 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -1585,9 +1585,9 @@ static struct { * or NULL to allocate a new image for the result. * @param imIn Source image to convert. * @param mode Target mode. - * @param palette Target palette for conversions to "P"/"PA"; - * NULL to use/generate a default palette. - * @param dither Nonzero to dither when converting to "P"/"PA" or "1". + * @param palette Target palette for conversions to "P" or "PA"; + * NULL to use a default palette. + * @param dither Nonzero to dither when converting to "P", "PA" or "1". * @return The resulting Imaging object, * or NULL with a Python exception set on failure. */