mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-03 03:13:04 +03:00
Allow PixelAccess to use Python __int__ when parsing x and y
This commit is contained in:
parent
543fa2ceb7
commit
bdbf1694fc
|
@ -23,6 +23,11 @@ else:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
cffi = None
|
cffi = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
import numpy
|
||||||
|
except ImportError:
|
||||||
|
numpy = None
|
||||||
|
|
||||||
|
|
||||||
class AccessTest:
|
class AccessTest:
|
||||||
# initial value
|
# initial value
|
||||||
|
@ -109,6 +114,13 @@ class TestImagePutPixel(AccessTest):
|
||||||
|
|
||||||
assert_image_equal(im1, im2)
|
assert_image_equal(im1, im2)
|
||||||
|
|
||||||
|
@pytest.mark.skipif(numpy is None, reason="NumPy not installed")
|
||||||
|
def test_numpy(self):
|
||||||
|
im = hopper()
|
||||||
|
pix = im.load()
|
||||||
|
|
||||||
|
assert pix[numpy.int32(1), numpy.int32(2)] == (18, 20, 59)
|
||||||
|
|
||||||
|
|
||||||
class TestImageGetPixel(AccessTest):
|
class TestImageGetPixel(AccessTest):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -1108,18 +1108,28 @@ _getxy(PyObject *xy, int *x, int *y) {
|
||||||
*x = PyLong_AS_LONG(value);
|
*x = PyLong_AS_LONG(value);
|
||||||
} else if (PyFloat_Check(value)) {
|
} else if (PyFloat_Check(value)) {
|
||||||
*x = (int)PyFloat_AS_DOUBLE(value);
|
*x = (int)PyFloat_AS_DOUBLE(value);
|
||||||
|
} else {
|
||||||
|
PyObject *int_value = PyObject_CallMethod(value, "__int__", NULL);
|
||||||
|
if (int_value != NULL && PyLong_Check(int_value)) {
|
||||||
|
*x = PyLong_AS_LONG(int_value);
|
||||||
} else {
|
} else {
|
||||||
goto badval;
|
goto badval;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
value = PyTuple_GET_ITEM(xy, 1);
|
value = PyTuple_GET_ITEM(xy, 1);
|
||||||
if (PyLong_Check(value)) {
|
if (PyLong_Check(value)) {
|
||||||
*y = PyLong_AS_LONG(value);
|
*y = PyLong_AS_LONG(value);
|
||||||
} else if (PyFloat_Check(value)) {
|
} else if (PyFloat_Check(value)) {
|
||||||
*y = (int)PyFloat_AS_DOUBLE(value);
|
*y = (int)PyFloat_AS_DOUBLE(value);
|
||||||
|
} else {
|
||||||
|
PyObject *int_value = PyObject_CallMethod(value, "__int__", NULL);
|
||||||
|
if (int_value != NULL && PyLong_Check(int_value)) {
|
||||||
|
*y = PyLong_AS_LONG(int_value);
|
||||||
} else {
|
} else {
|
||||||
goto badval;
|
goto badval;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user