@@ -67,6 +67,51 @@ def test_array_ufunc_out_rejected(self):
6767 ):
6868 np .ceil (im , out = im )
6969
70+ def test_array_dunder_asarray (self ):
71+ """Regression test: np.asarray(img) used to silently coerce to a
72+ useless 0-d object array (no __array__ implemented) instead of
73+ exposing the pixel data -- same bug class found in Kernel's
74+ missing __array__."""
75+ im = Image ([[1 , 2 ], [3 , 4 ]], dtype = "uint8" )
76+
77+ arr = np .asarray (im )
78+
79+ self .assertEqual (arr .shape , im .array .shape )
80+ nt .assert_array_equal (arr , im .array )
81+
82+ def test_array_dunder_scipy_interop (self ):
83+ """Regression test: functions that aren't ufunc-/array-function-
84+ aware (e.g. scipy.signal.convolve2d) call np.asarray() internally
85+ and used to receive a useless 0-d object array."""
86+ from scipy .signal import convolve2d
87+
88+ im = Image .Random (size = (10 , 10 ), dtype = "float64" )
89+
90+ out = convolve2d (im , np .ones ((3 , 3 )))
91+
92+ self .assertEqual (out .ndim , 2 )
93+
94+ def test_array_dunder_dtype_conversion (self ):
95+ im = Image ([[1 , 2 ], [3 , 4 ]], dtype = "uint8" )
96+
97+ arr = np .asarray (im , dtype = "float32" )
98+
99+ self .assertEqual (arr .dtype , np .float32 )
100+
101+ def test_array_dunder_returns_readonly_view_by_default (self ):
102+ im = Image ([[1 , 2 ], [3 , 4 ]], dtype = "uint8" )
103+
104+ arr = np .asarray (im )
105+
106+ self .assertFalse (arr .flags .writeable )
107+
108+ def test_array_dunder_copy_true_returns_writeable (self ):
109+ im = Image ([[1 , 2 ], [3 , 4 ]], dtype = "uint8" )
110+
111+ arr = np .array (im , copy = True )
112+
113+ self .assertTrue (arr .flags .writeable )
114+
70115 def test_pixel (self ):
71116 im = Image (np .arange (80 ).reshape ((10 , 8 )), dtype = "int64" ) # 8x10 image
72117 pix = im .pixel (5 , 6 )
0 commit comments