Using the array interface
The array interface is a yet another mechanism to communicate with other Python applications. This protocol, as its name suggests, is only applicable to array-like objects. A demonstration is in order. Let's use PIL again, but without saving files.
Getting ready
We will be reusing part of the code from the previous recipe, so the prerequisites are similar. We will skip the first step of the previous step here, and assume it is already known.
How to do it...
The following steps will let us explore the array interface:
The PIL image array interface attribute.
The PIL image object has a
__array_interface__
attribute. Let's inspect its contents. The value of this attribute is adictionary
:array_interface = img.__array_interface__ print "Keys", array_interface.keys() print "Shape", array_interface['shape'] print "Typestr", array_interface['typestr']
This code prints the following information:
Keys ['shape', 'data', 'typestr'] Shape (512, 512, 4) Typestr |u1
The NumPy array...