0

I'm reading someone's transform.py, and there is a code which really confuses me. Here it is:

np.concatenate(img_group, axis=2)

however, the img_group here is a sequence of <class 'PIL.Image.Image'>, and I've looked through the docs of np.concatenate(), it tells me that:enter link description here

numpy.concatenate((a1, a2, ...), axis=0, out=None) Join a sequence of arrays along an existing axis. Parameters: a1, a2, … : sequence of array_like The arrays must have the same shape, except in the dimension corresponding to axis (the first, by default).

and I have tried some samples like:

x = Image.open('flows/v_ApplyEyeMakeup_g08_c01/frame/img_00001.jpg').convert('RGB')
y = Image.open('flows/v_ApplyEyeMakeup_g08_c01/frame/img_00002.jpg').convert('RGB')
z = np.concatenate([x,y], axis=2)

then it worked! the z is a numpy.ndarray type whose size is (240,320,6). However, the <class 'PIL.Image.Image'> seems not the kinds of array that the np.concatenate() parameters need, so I wonder how it works?

DawnChou
  • 13
  • 3
  • `concatenate` works with compatible lists (or nested lists). I assume it's converting them to arrays before doing the concatenate (though the under-the-covers details may be different). – hpaulj Nov 21 '19 at 06:49

2 Answers2

1

Numpy operates on array-like objects. Here's a link to a question regarding what constitutes array-likeness. One way for a python object to be array-like is to implement the __array_interface__ method. That is precisely what PIL.Image does.

felixwege
  • 121
  • 1
  • 6
0

If you look in the source for PIL.Image, you will see that it has a property called __array_interface__ and the comment below it says:

# numpy array interface support.

My guess is that numpy is using this property to convert the image to an array and thus is able to manipulate it as one.

It is also this property that makes the Image object, "array_like". See __array_interface__.

smac89
  • 39,374
  • 15
  • 132
  • 179