[Image-SIG] Image warping in Python or PIL

Son Hua songuke at gmail.com
Sat May 8 05:58:03 CEST 2010


Can anybody show me some hints on how to implement an image warping 
function efficiently in Python or PIL? Suppose we have a function f(x, 
y) -> (x', y') that warps a pixel location (x, y) to (x', y'). Because 
(x', y') may end up to be non-integer, and many (x, y) can map to the 
same (x', y'), reverse mapping is used. That is at every destination 
pixel (x', y'), we go and find the original pixel location (x, y) by 
taking (x, y) = f_1(x', y'), where f_1 is the inverse function of f.

Suppose the inverse function f_1 is given. So, for each pixel in the 
destination image, we can map to a non-integer pixel in the source 
image. Therefore, we must bilinear interpolate at the source location 
for the color.

I know that doing this pixel per pixel in Python is very slow. For example:

for i in xrange(height):
     for j in xrange(width):
         (x, y) = f_1(j, i)
         # 1. Check if (x, y) is inside the source image. If out of 
boundary, skip.
         # 2. Bilinear sampling at (x, y) at the source image to obtain 
color C
         # 3. Paste the color C into (i, j) location in the destination 
         # next...

I'm doing this warping as the final stage of an image mosaicking 
program. The size of the image is quite big, about 8000x2000.

So, any ideas about how to make it fast, except implementing the whole 
thing in C? As I also used the dictionary data structure available in 
Python, so integrating C here may be painful.

Thank you in advance.

Best regards,

More information about the Image-SIG mailing list