Allow getpixel to accept a list

This commit is contained in:
Andrew Murray 2023-08-28 20:12:23 +10:00
parent 091ab74429
commit d8c3135b6b
2 changed files with 11 additions and 3 deletions

View File

@ -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)

View File

@ -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)) {