[Image-SIG] Comparison of PIL and GD speed for setting pixels through python

John Barratt jb at langarson.com.au
Sun Feb 4 11:20:01 CET 2007

Christopher Barker wrote:
> John Barratt wrote:
>> 	I've had a need to do some optimisation of some low level pixel setting 
>> code, and have as such done some tests of different ways of doing this 
>> using PIL and alternatively with gd via ctypes.
> Have you tried using PIL + numpy -- you can convert a PIL image (some 
> eof them, anyway) to a numpy array, and manipulate it there. Numpy 
> provides a number if efficient ways to do such manipulations.
> In the most recent PIL, you should be able to convert to/from PIL/numpy 
> without any copying of the data, though I don't think it's been heavily 
> tested.

I haven't tried it, but did originally think of adding it to the list of 
things tested.  Given this specific example though, where the idea is 
that a specific method or piece of code is needed to be run for every 
pixel (not that it actually is in this abstract example), I would think 
that unless that specific code can be called from, and done in C, that 
it would perform similarly to the Image.load method.  This is because I 
am assuming the cost of accessing the raw data from Image.load to be 
similar to that of accessing an array type in numpy.  Having said all 
that though, I'll have a look at adding it to the mix...

I think where PIL & numpy would excel is where you have a cases of a 
number of images with calculations required that could be easily 
represented as array operations.  The examples I am thinking of don't 
lend themselves to this sort of solution.

It is perhaps also possible to reasonably efficiently access the PIL raw 
data under C as a standard type (eg. a 'flattened' unsigned char* for 
paletted images) through ctypes, in which case the result time-wise 
would hopefully be similar, or the same as using gd all the way.

I am also finishing a 'real world' example for this sort of problem 
which may help further clarify further options and their relative 
benefits & costs.



More information about the Image-SIG mailing list