From d8c3135b6bcff0105453346dfa35886615bf0505 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 28 Aug 2023 20:12:23 +1000 Subject: [PATCH] Allow getpixel to accept a list --- Tests/test_image_access.py | 4 ++++ src/_imaging.c | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index c9db3aee7..f80bc9c10 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -213,6 +213,10 @@ class TestImageGetPixel(AccessTest): def test_basic(self, mode): self.check(mode) + def test_list(self): + im = hopper() + assert im.getpixel([0, 0]) == (20, 20, 70) + @pytest.mark.parametrize("mode", ("I;16", "I;16B")) @pytest.mark.parametrize( "expected_color", (2**15 - 1, 2**15, 2**15 + 1, 2**16 - 1) diff --git a/src/_imaging.c b/src/_imaging.c index 95da2772d..736f347a3 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -1146,11 +1146,15 @@ static inline int _getxy(PyObject *xy, int *x, int *y) { PyObject *value; - if (!PyTuple_Check(xy) || PyTuple_GET_SIZE(xy) != 2) { + int tuple = PyTuple_Check(xy); + if ( + !(tuple && PyTuple_GET_SIZE(xy) == 2) && + !(PyList_Check(xy) && PyList_GET_SIZE(xy) == 2) + ) { goto badarg; } - value = PyTuple_GET_ITEM(xy, 0); + value = tuple ? PyTuple_GET_ITEM(xy, 0) : PyList_GET_ITEM(xy, 0); if (PyLong_Check(value)) { *x = PyLong_AS_LONG(value); } else if (PyFloat_Check(value)) { @@ -1164,7 +1168,7 @@ _getxy(PyObject *xy, int *x, int *y) { } } - value = PyTuple_GET_ITEM(xy, 1); + value = tuple ? PyTuple_GET_ITEM(xy, 1) : PyList_GET_ITEM(xy, 1); if (PyLong_Check(value)) { *y = PyLong_AS_LONG(value); } else if (PyFloat_Check(value)) {