Skip to content

Commit 683affa

Browse files
authored
Merge pull request #5206 from radarhere/numpy
2 parents 7785931 + 7b4b356 commit 683affa

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

Tests/test_image_access.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
except ImportError:
2424
cffi = None
2525

26+
try:
27+
import numpy
28+
except ImportError:
29+
numpy = None
30+
2631

2732
class AccessTest:
2833
# initial value
@@ -66,6 +71,10 @@ def test_sanity(self):
6671
pix1 = im1.load()
6772
pix2 = im2.load()
6873

74+
for x, y in ((0, "0"), ("0", 0)):
75+
with pytest.raises(TypeError):
76+
pix1[x, y]
77+
6978
for y in range(im1.size[1]):
7079
for x in range(im1.size[0]):
7180
pix2[x, y] = pix1[x, y]
@@ -109,6 +118,13 @@ def test_sanity_negative_index(self):
109118

110119
assert_image_equal(im1, im2)
111120

121+
@pytest.mark.skipif(numpy is None, reason="NumPy not installed")
122+
def test_numpy(self):
123+
im = hopper()
124+
pix = im.load()
125+
126+
assert pix[numpy.int32(1), numpy.int32(2)] == (18, 20, 59)
127+
112128

113129
class TestImageGetPixel(AccessTest):
114130
@staticmethod

src/_imaging.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,12 @@ _getxy(PyObject *xy, int *x, int *y) {
11091109
} else if (PyFloat_Check(value)) {
11101110
*x = (int)PyFloat_AS_DOUBLE(value);
11111111
} else {
1112-
goto badval;
1112+
PyObject *int_value = PyObject_CallMethod(value, "__int__", NULL);
1113+
if (int_value != NULL && PyLong_Check(int_value)) {
1114+
*x = PyLong_AS_LONG(int_value);
1115+
} else {
1116+
goto badval;
1117+
}
11131118
}
11141119

11151120
value = PyTuple_GET_ITEM(xy, 1);
@@ -1118,7 +1123,12 @@ _getxy(PyObject *xy, int *x, int *y) {
11181123
} else if (PyFloat_Check(value)) {
11191124
*y = (int)PyFloat_AS_DOUBLE(value);
11201125
} else {
1121-
goto badval;
1126+
PyObject *int_value = PyObject_CallMethod(value, "__int__", NULL);
1127+
if (int_value != NULL && PyLong_Check(int_value)) {
1128+
*y = PyLong_AS_LONG(int_value);
1129+
} else {
1130+
goto badval;
1131+
}
11221132
}
11231133

11241134
return 0;

0 commit comments

Comments
 (0)