[Python-Dev] PEP: Extending the buffer protocol to share array information.

Terry Reedy tjreedy at udel.edu
Wed Nov 1 01:24:34 CET 2006

""Martin v. Löwis"" <martin at v.loewis.de> wrote in message 
news:4547BF86.6070806 at v.loewis.de...
> Paul Moore (IIRC) gave the example of equalising the green values
> and maximizing the red values in a PIL image by passing it to NumPy:
> Is that a realistic (even though not-yet real-world) example? If
> so, what algorithms of NumPy would I use to perform this image
> manipulation

The use of surfarrays manipulated by Numeric has been an optional but 
important part of PyGame for years.
Surfarray Introduction
Pygame uses the Numeric python module to allow efficient per pixel effects 
on images. Using the surface arrays is an advanced feature that allows 
custom effects and filters. This also examines some of the simple effects 
from the Pygame example, arraydemo.py.
The Examples section of the linked page
has code snippets for generating, resizing, recoloring, filtering, and 
cross-fading images.

>(and why would I use NumPy for it if I could just
> write a for loop that does that in pure Python, given PIL's
> getpixel/setdata)?

Why does anyone use Numeric/NumArray/NumPy?  Faster,easier coding and much 
faster execution, which is especially important when straining for an 
acceptible framerate.

I believe that at present PyGame can only work with external images that it 
is programmed to know how to import.  My guess is that if image source 
program X (such as PIL) described its data layout in a way that NumPy could 
read and act on, the import/copy step could be eliminated.  But perhaps 
Travis can clarify this.

Terry Jan Reedy

More information about the Python-Dev mailing list